From 0a236f6541d7034b6a09ec97ab049e8c499867e4 Mon Sep 17 00:00:00 2001 From: hecht Date: Tue, 31 Jan 2012 21:44:27 +0000 Subject: [PATCH] draft implementation of the tasking toolkit --- ag/include/tasking.inc.php | 167 +++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 ag/include/tasking.inc.php diff --git a/ag/include/tasking.inc.php b/ag/include/tasking.inc.php new file mode 100644 index 0000000..bbcc2c8 --- /dev/null +++ b/ag/include/tasking.inc.php @@ -0,0 +1,167 @@ + "value) style + * @param long $execution_time the posix timestamp + * @param int $repeat_mode something like "TASKING_NO_RESCEDULE" or TASKING_5_MIN ... + * @param bool $catchUpOn should missed tasks be catched up on later? If unsure what you are doing do not use this ;) + * @param int $task_id the task id that should be updated (if one only wants to update an existant job) + */ +function schedule($script_name, array $parameters, $execution_time, $repeat_mode, $catchUpOn = false, $task_id = NULL) { + if($task_id === NULL) { + db_query('INSERT INTO tasking(script_name, parameter, schedule_time, mode, catchup) values(\''.$script_name.'\', \''.implode(',', parameters).'\', \''.$execution_time.'\', \''.$repeat_mode.'\', \''.$catchUpOn.'\')'); + } else { + db_query('UPDATE tasking SET script_name = \''.$script_name.'\' , parameter = \''.implode(',', parameters).'\', schedule_time = \''.$execution_time.'\', mode = \''.$repeat_mode.'\', catchup = \''.$catchUpOn.'\' WHERE task_id = ' . $task_id); + } +} + + +/** + * Get the task ids of all tasks + */ +function getAllTaskIds() { + $qry = db_query('SELECT task_id FROM tasking'); + if(!$qry) + return array(); + $result = array(); + while($row = mysql_fetch_row($qry)) { + $result[] = $row[0]; + } + return $result; +} + +/** + * Get the task ids of tasks that should have been executed + */ +function getPassedTaskIds($timestamp = NULL) { + if($timestamp === NULL) { + $timestamp = time(); + } + $qry = db_query('SELECT task_id FROM tasking WHERE schedule_time <= ' . $timestamp); + if(!$qry) + return array(); + $result = array(); + while($row = mysql_fetch_row($qry)) { + $result[] = $row[0]; + } + return $result; +} + +/** + * Get the task array + * @param int $task_id the id of the task + */ +function getTask($task_id) { + $qry = db_query('SELECT * FROM tasking WHERE task_id = ' . $task_id); + if(!$qry) + return null; + return mysql_fetch_assoc($qry); +} + +/** + * removes the task + * @param int $task_id the id of the task + */ +function deleteTask($task_id) { + db_query('DELETE FROM tasking WHERE task_id = ' . $task_id); +} + + +function executeScript($task_id) { + $task = getTask($task_id); + if($task === NULL) { + // the task has been deleted ... do nothing + return; + } + + $time = $task['schedule_time']; + + switch($task['mode']) + { + case TASKING_NO_RESCEDULE: + deleteTask($task_id); + return; + case TASKING_5_MIN: + $time = $time + 300; // offset of 300 sec (5 min) + break; + case TASKING_10_MIN: + $time = $time + 600; // offset of 600 sec (10 min) + break; + case TASKING_15_MIN: + $time = $time + 900; // offset of 900 sec (15 min) + break; + case TASKING_30_MIN: + $time = $time + 1800; // offset of 1800 sec (30 min) + break; + case TASKING_1_HOUR: + $time = $time + 3600; // offset of 3600 sec (1 h) + break; + case TASKING_2_HOUR: + $time = $time + 7200; // offset of 7200 sec (2 h) + break; + case TASKING_6_HOUR: + $time = $time + 21600; // offset of 21600 sec (6 h) + break; + case TASKING_12_HOUR: + $time = $time + 43200; // offset of 43200 sec (12 h) + break; + case TASKING_DAYLY: + $time = $time + 86400; // offset of 86400 sec (24 h) + break; + case TASKING_DAY_OF_WEEK: + $time = $time + 604800; // offset of 604800 sec (7 days) + break; + case TASKING_DAY_OF_MONTH: + // the way of calculation is somehow more complex ;) + $time = mktime(date("H", $time), date("i", $time), date("s", $time), date("m", $time)-1, date("d", $time), date("Y", $time)); + break; + } + + // reschedule ! + schedule($task['script_name'], $task['parameter'], $time, $task['mode'], $task['catchup']); +} + +?> \ No newline at end of file