How to create code available as a task (for developers)


The following documentation is actual for Task Scheduler 1.0.0 - 1.4.x. Newer versions may have differences.

1. Basic usage

If you would like to install some code as a task and run it periodically you need to have specific functions in the model class. The 'Task Scheduler' scans all model files in the store and fetches models with specific functions defined.

There is just one function required by the 'Task Scheduler' for the module to appear as a task module:

public function runSchedulerOperation($operation, $params, &$stat) {
...
}

PARAMETERS:
$operation - text parameter. Operation type. It is defined by the model class in advanced implementation. Empty by default.
$params   - array. It is defined by the model class in advanced implementation. Empty by default.
&$stat      - array of values which can be returned by the function. Empty by default.

RETURNS:
  $return - text code. String or array. String function returns a return code which can be:
'finished'      - operation is complete
'not_finished'  - still working (additional calls needed)
  if the $return is array then it contains:
array(
  'return_code' => 'finished' or 'not finished',
  'next_task_id' => next task identifier. It is used when the return_code is 'finished'. The list of tasks can be received by $this->model_tool_ka_tasks->getTasks();
);

Below is an example of the sample task:

<?php
class ModelToolSampleTask extends Model {

    public function runSchedulerOperation($operation, $params, &$stat) {
            $return_code = 'finished';
           
            /* put your code below this line */       

            /* some statistics can be returned in the stat array */
            $stat = array(
                'Number of processed products' => 10,
            );
           
            return $return_code;
    }
}

?>

1.1. Important details

- your task should not work longer than 25 seconds because the process is usually killed after 30 seconds by the server. If you need to run time-consuming operations then you should save all required parameters in session and fetch them on the next call. The 'not_finished' return code should be used in this case.

- avoid making a loop with tasks, i.e. you should prevent the user from selecting the same task in the list of 'Next tasks'. Also you should pass the 'next_task_id' parameter on successful completion of the current task only.

2. Advanced usage

Your module can offer different task operations. Their list is declared by this function:
  requestSchedulerOperations()

You can ask customer to define some parameters for each task. They are defined in this function:
  requestSchedulerOperationParams($operation)

Please have a look at the download_file.php script to see how they can be used.

Please Wait!

Please wait... it will take a second!