General developer forum

Execute code at specific time

 
Picture of jeroen van bennekum
Execute code at specific time
 

Hi,


I need to run some code at a specific datetime set by a user. I have an adhock class and try to set its next runtime 30 seconds in the future (for now to test) but it doesn't seem to be ran and also not queued based on:

\core\task\manager::get_all_scheduled_tasks();

Here is my adhock class:

<?php

class event_deadline_warning extends \core\task\adhoc_task {
public function execute() {
error_log('custom ABOUT TO SEND NOTIF IN ADHOC TASK');
send_notification(44, 'SUBJECT', 'TEXT', 'TEXT');
}
}
Which has a error log to see if it is executed and some (test) code to be ran. (This part is never ran)


Here is my code to trigger all this:

require_once('classes/event_deadline_warning.php');
$deadline = new event_deadline_warning();
$run_at = time() + 30;//Set
error_log('custom NOW = ' . $run_at);

$deadline->set_next_run_time($run_at);
\core\task\manager::queue_adhoc_task($deadline);

error_log('custom QUEUED: ' . $deadline->get_next_run_time());
Which does output the error logs so I am sure this is actually ran.

Is there something I am doing wrong? How can I schedule a task at a specific time to run some code only once?

 
Average of ratings: -
Picture of Darko Miletić
Re: Execute code at specific time
Core developersParticularly helpful Moodlers
Scheduled task execution depends on the cron being executed regularly.



Setup Moodle cron to be executed frequently.



https://docs.moodle.org/35/en/Cron





El lun., 24 sept. 2018 8:04 a. m., jeroen van bennekum (via Moodle.org) <
 
Average of ratings: -