MyBB Community Forums

Full Version: Tasks system modification to allow pass data between runs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
if you can pass data between runs of particular task, then you can distribute heavy work between runs.

idea is to have additional column 'taskdata' in tasks table, which is hidden from UI, but provided to task and stored in database if task returns data.

then you can write task like this

<?php

function task_example(&$task)
{
        global $db,$mybb;

        $wait_time = TIME_NOW - $mybb->settings['example_wait_time'];
        $limit   = $mybb->settings['example_limit'];
        
        $taskdata = array(
                'lastrun' => 0,
                'data'    => array(),
        );

        if (strlen($task['taskdata']))
        {
                // so, it is not a very first run
                $taskdata = unserialize($task['taskdata']);
        }

        if (!count($taskdata['data']) && ($taskdata['lastrun'] < $wait_time) )
        {
                // if no data to process and we finish waiting
                // search for data, but not for more that 10*$limit
                $query = $db->query( "SELECT 'heavy things' LIMIT {$limit}0" );

                while ($item = $db->fetch_array($query))
                {
                        $taskdata['data'][] = $item;
                }

                // even if there is no data to process we remember when we scan last time
                $taskdata['lastrun'] = TIME_NOW;
        }

        if (count($taskdata['data']))
        {
                // if there is data to process
                // process limited part 
                while ($limit)
                {
                        --$limit;
                        $item = array_shift($taskdata['data']);

                        // do something with item
                        // ..

                        if (!count($taskdata['data']))
                        {
                                break;
                        }
                }
                $taskdata['lastrun'] = TIME_NOW;
        }

        add_task_log($task,'some good message');
        
        return serialize($taskdata);
}
?>

(remember - it is just illustration to idea Wink )

you see - this particular example allows you to perform heavy database scans not every time you run task, and distribute processing of results between several runs.

off course you can use another technique to pass data between runs, examples:
  • store file in cache directory,
  • create mentioned column by plugin installation, column value will be passed to task anyway, and instead of returning data - store it directly in database.
but for me this seems better.

comments?

P.S. A quite simple modification to functions_task.php to implement this suggestion. No any changes to existing tasks required
[attachment=19818]