Create custom scheduled task in Totara

| By Webner

How to create custom scheduled task in Totara using code in the custom plugin

Scheduled tasks are very useful if we want to perform anything in the background. Here I am going to explain the steps to create a custom scheduled task in Totara if you are interacting with it using your custom plugin.

Steps :

1. For this, first, you will need to create a subclass of the main scheduled task
“\core\task\scheduled_task” by creating a file say “my_custom_task.php” within /classes/task/my_custom_task.php:

Here you will need to define the task which you want to perform in the background.

For example, code in this file will be like below :

namespace plugintype_pluginname\task;

class my_custom_task extends \core\task\scheduled_task {
public function get_name() {
// Shown on admin screens
return get_string(my_custom_task, 'auth_myplugin');

public function execute() {
//you code here

Note: if there is no subdirectory task under classes directory then you can create this one.

2. Then you will need to create a file task.php under db folder of your plugin as /db/task.php”. Here you will define the schedule of your scheduled task as below :

$tasks = array(
'classname' => 'auth_myplugin\task\my_custom_task,
'blocking' => 0,
'minute' => '*/2',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 0

Following is the description of these various fields/parameters/keywords in this file :

* classname – This is the full classname of your task which contains plugintype_pluginname\task\task_class_name.

* blocking – if this is set to 1, no other scheduled task will run at the same time as this task. This is recommended to not to set this to 1 unless you really need it as it will impact the performance of the other tasks in queue.

* minute, hour, day, dayofweek, month – This is the default schedule for running the task like if you will give the value as below :

'minute' => '*/30',
'hour' => '*/1',

then your task will run after every 1 hour 30 minutes. So, if you want to run it every few minutes and not hours then you can skip setting the value for hour parameter. Similarly, you can set the values for other time parameters like days if you want to run it on some specific days or months accordingly.

3. After doing above changes, you will need to increase the version number of your plugin in version.php file of your plugin to see the effect. (Note that every time you make any change under your db folder, you will need to increase the version number to see the changes).

4. Then give your task an appropriate name that you want to see on the screen. For this, you will need to make an entry in the language file of your plugin (myplugin/lang/en/auth_myplugin.php) like below :

$string[my_custom_task] = 'Perform My Custom Task';

After doing so, you will be able to see this task in the list of scheduled tasks in Totara using the path Site administration>> Server>> Scheduled tasks. It will look like below :


Note that you can change the schedule/time of your task using this user interface as well i.e if you will click on the pencil icon shown here, you will see the page like below :


Using this screen, you can change the time of your scheduled task if you will need to do so.

Leave a Reply

Your email address will not be published. Required fields are marked *