2010-09-09, 09:20 AM
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
(remember - it is just illustration to idea )
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:
comments?
P.S. A quite simple modification to functions_task.php to implement this suggestion. No any changes to existing tasks required
[attachment=19818]
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 )
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.
comments?
P.S. A quite simple modification to functions_task.php to implement this suggestion. No any changes to existing tasks required
[attachment=19818]