draft implementation of the tasking toolkit
							parent
							
								
									2917f26236
								
							
						
					
					
						commit
						0a236f6541
					
				| @ -0,0 +1,167 @@ | |||||||
|  | <?php | ||||||
|  | /* | ||||||
|  |  * | ||||||
|  |  * @copyright (c) 2012 animegame.eu | ||||||
|  |  * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public Licence | ||||||
|  |  * @author hecht | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | include_once(ROOT_PATH.'/include/semaphore.inc.php'); | ||||||
|  | include_once(ROOT_PATH . '/include/sqlwrapper.inc.php'); | ||||||
|  | 
 | ||||||
|  | define(TASKING_NO_RESCEDULE, 0); | ||||||
|  | define(TASKING_5_MIN, 0); | ||||||
|  | define(TASKING_10_MIN, 0); | ||||||
|  | define(TASKING_15_MIN, 0); | ||||||
|  | define(TASKING_30_MIN, 0); | ||||||
|  | define(TASKING_1_HOUR, 0); | ||||||
|  | define(TASKING_2_HOUR, 0); | ||||||
|  | define(TASKING_6_HOUR, 0); | ||||||
|  | define(TASKING_12_HOUR, 0); | ||||||
|  | define(TASKING_DAYLY, 0); | ||||||
|  | define(TASKING_DAY_OF_WEEK, 0); | ||||||
|  | define(TASKING_DAY_OF_MONTH, 0); | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * The method that has to be called by a cronjob! | ||||||
|  |  */ | ||||||
|  | function tick() { | ||||||
|  | 	$resource = 'cronjob'; | ||||||
|  | 	if(semaphoreUP($resource)) { // do not do in parallel | ||||||
|  | 		return; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	$ids = getPassedTaskIds(); | ||||||
|  | 	foreach ($ids as $id) { | ||||||
|  | 		executeScript($id); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	semaphoreDown($resource); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Schedule a task | ||||||
|  |  * | ||||||
|  |  * @param string $script_name the name of the script e.g. "generic_tournament.php") | ||||||
|  |  * @param array $parameters the parameters to use array("key" => "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']); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | ?> | ||||||
					Loading…
					
					
				
		Reference in New Issue
	
	 hecht
						hecht