Queue API

Are you new to the eduTrac SIS online user's manual? If so, click here for a starting point. If you are looking for technical documentation, then check out the Wiki.

 

The queue API lets you manage tasks easily without using the command line and also without having to create new cronjobs. With the Queue API, you can write tasks to clear error logs more frequently, clear old cookies, clear old sessions, and more.

Examples

use app\src\Core\etsis_Queue as Queue;
$app = \Liten\Liten::getInstance();

/**
 * Create a task worker for the deletion of error logs older than 10 days old.
 */
function delete_error_logs_task_worker_example()
{
    $queue = new Queue();
    $task = [];
    $task['task_worker'] = [
        /**
         * Unique processing id. Must be an integer.
         */
        'pid' => 7615,
        /**
         * Unique name of this task.
         */
        'name' => 'Delete Error Logs',
        /**
         * task_callback is the name of your custom function which is associated
         * with the action_hook.
         */
        'task_callback' => '_delete_error_log_queue',
        /**
         * action_hook is the hook that should be fired when the queue runs. This
         * is a custom action hook which you define. Do not use one that is already
         * defined by the system. It could cause adverse reactions.
         */
        'action_hook' => '_delete_error_log_hook',
        /**
         * The Cronjob schedule. This example is every minute. To learn more
         * about cronjob schedule format, check out
         * https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
         */
        'schedule' => '* * * * *',
        /**
         * If your task is not working, set this to true for debugging.
         */
        'debug' => (bool) false,
        /**
         * How long the processing is expected to take in seconds. After this
         * expires, the item will be reset and another consumer can claim the item.
         */
        'max_runtime' => 30,
        /**
         * When you want to disable a task from running for a while, set this to
         * false.
         */
        'enabled' => (bool) true
    ];
    $queue->enqueue($task);
}

/**
 * Logs should delete themselves after 10 days. This will ensure that those old
 * files are cleaned up and deleted after 10 days.
 */
function _delete_error_log_queue()
{
    $logs = glob(APP_PATH . 'tmp/logs/*.txt');
    if (is_array($logs)) {
        foreach ($logs as $log) {
            if (file_exists($log) && file_mod_time($log) > 10 * 24 * 3600) {
                unlink($log);
            }
        }
    }
}
$app->hook->add_action('_delete_error_log_hook', '_delete_error_log_queue', 10);
$app->hook->add_action('etsis_task_worker_cron', 'delete_error_logs_task_worker_example', 10);

In following the example above, you will need to first create a task worker (i.e. delete_error_logs_task_worker_example). The task runner will be added to the queue and will run every minute. Our second function (_delete_error_log_queue) is the function that will carry out the task of deleting old error logs. This function will be called every time the action hook _delete_error_log_hook runs.

This example as well as another example is attached to this article in the form of a dropin. Unzip, use the example to create your own tasks and then drop it into the dropins folder. To disable a task(s), change the ‘enabled’ option to (bool) false and the tasks will not run. This will also release the item from the queue. To re-enable, do vice versa.

Last Modified: Oct 12, 2017 @ 8:20 AM

Was this article helpful?

Related Articles