public function run($data) { if (array_key_exists('search', $data) && !empty($data['search'])) { $search = $data['search']; $this->Twitter = ConnectionManager::getDataSource('twitter'); switch ($search) { default: $this->getSearchResults($search); $nextUpdate = '+30 Minutes'; break; case '*global*': $this->getGlobal($search); $nextUpdate = '+5 Minutes'; break; } //check if there is already a task for this term. $findConf = array('conditions' => array('fetched' => null, 'data LIKE' => '%' . $search . '%')); $alreadyPresent = $this->QueuedTask->find('count', $findConf); if ($alreadyPresent == false) { if ($this->QueuedTask->createJob('twitterscrape', array('search' => $search), $nextUpdate)) { $this->out('Searchterm update Queued'); } else { $this->err('Could not create Twitterscrape Job.'); } } else { $this->err('There seems to be another job queued for this term, job not requeued.'); } return true; } else { $this->out('No Search term found, Cancelling'); // return true so the task does NOT get requeued. return true; } }
/** * Display Some statistics about Finished Jobs. * @return null */ public function stats() { $this->out('Jobs currenty in the Queue:'); $types = $this->QueuedTask->getTypes(); foreach ($types as $type) { $this->out("\t " . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ": " . $this->QueuedTask->getLength($type)); } $this->hr(); $this->out('Total unfinished Jobs : ' . $this->QueuedTask->getLength()); $this->hr(); $this->out('Finished Job Statistics:'); $data = $this->QueuedTask->getStats(); debug($data); foreach ($data as $item) { if (!array_key_exists('runtime', $item[0])) { $t_item = array_values($item[0]); $item[0]['jobtype'] = $t_item[0]; $item[0]['num'] = $t_item[1]; $item[0]['alltime'] = $t_item[2]; $item[0]['fetchdelay'] = $t_item[3]; $item[0]['runtime'] = $t_item[4]; } $this->out(" " . $item[0]['jobtype'] . ": "); $this->out(" Finished Jobs in Database: " . $item[0]['num']); $this->out(" Average Job existence\t: " . $item[0]['alltime'] . 's'); $this->out(" Average Execution delay\t: " . $item[0]['fetchdelay'] . 's'); $this->out(" Average Execution time\t: " . $item[0]['runtime'] . 's'); } }
function get_queued_tasks() { $qTask = mysql_query("SELECT id\n FROM control_task_queue\n WHERE control_unit_id = {$this->unit_id} AND\n start = 0\n ORDER BY id LIMIT 1") or die(mysql_error()); $tasks = array(); while ($task = mysql_fetch_object($qTask)) { $tasks[] = QueuedTask::FromId($task->id); } return $tasks; }
/** * Add task * * @param Task $task * @return $this */ public function addTask(Task $task) { $queuedTask = new QueuedTask($task); if ($this->firstTask === null) { $this->firstTask = $queuedTask; } if ($this->lastTask === null) { $this->lastTask = $queuedTask; } else { $this->lastTask->setNext($queuedTask); $this->lastTask = $queuedTask; } $this->queueSize++; if ($this->status === self::STATUS_RUNNING || $this->autoRun === true) { $this->status = self::STATUS_RUNNING; $this->fillAndStartProcessing(); } return $this; }
/** * Example run function. * This function is executed, when a worker is executing a task. * The return parameter will determine, if the task will be marked completed, or be requeued. * * @param array $data The array passed to QueuedTask->createJob() * @param int $id The id of the QueuedTask * @return bool Success * @throws RuntimeException when seconds are 0; */ public function run($data, $id = null) { $this->hr(); $this->out('CakePHP Queue LongExample task.'); $seconds = (int) $data; if (!$seconds) { throw new RuntimeException('Seconds need to be > 0'); } $this->out('A total of ' . $seconds . ' seconds need to pass...'); for ($i = 0; $i < $seconds; $i++) { sleep(1); $this->QueuedTask->updateProgress($id, ($i + 1) / $seconds); } $this->hr(); $this->out(' ->Success, the LongExample Job was run.<-'); $this->out(' '); $this->out(' '); return true; }
/** * Add functionality. * Will create one example job in the queue, which later will be executed using run(); * * @return void */ public function add() { $this->out('CakePHP Queue Execute task.'); $this->hr(); if (count($this->args) < 2) { $this->out('This will run an shell command on the Server.'); $this->out('The task is mainly intended to serve as a kind of buffer for programm calls from a cakephp application.'); $this->out(' '); $this->out('Call like this:'); $this->out(' cake queue add execute *command* *param1* *param2* ...'); $this->out(' '); } else { $data = ['command' => $this->args[1], 'params' => array_slice($this->args, 2)]; if ($this->QueuedTask->createJob('Execute', $data)) { $this->out('Job created'); } else { $this->err('Could not create Job'); } } }
/** * Destructor, removes pid-file * */ public function __destruct() { if ($pidFilePath = Configure::read('Queue.pidfilepath')) { if (function_exists('posix_getpid')) { $pid = posix_getpid(); } else { $pid = $this->QueuedTask->key(); } $file = $pidFilePath . 'queue_' . $pid . '.pid'; if (file_exists($file)) { unlink($file); } } }
/** * Example add functionality. * Will create one example job in the queue, which later will be executed using run(); */ public function add() { $this->out('CakePHP Queue Example task.'); $this->hr(); $this->out('This is a very simple example of a queueTask.'); $this->out('I will now add an example Job into the Queue.'); $this->out('This job will only produce some console output on the worker that it runs on.'); $this->out(' '); $this->out('To run a Worker use:'); $this->out(' cake queue runworker'); $this->out(' '); $this->out('You can find the sourcecode of this task in: '); $this->out(__FILE__); $this->out(' '); /** * Adding a task of type 'example' with no additionally passed data */ if ($this->QueuedTask->createJob('example', null)) { $this->out('OK, job created, now run the worker'); } else { $this->err('Could not create Job'); } }
/** * Fetch the status from the queue database and put data in an array the different output functions can use. * * @return array Keyed array of metrics we want from the queue. */ private function getStatus() { $info['queue_size'] = $this->QueuedTask->getPending(); $lastTask = $this->QueuedTask->getLastCompleted(); if (!empty($lastTask)) { $info['last_task_completed'] = $lastTask['QueuedTask']['completed']; } else { $info['last_task_completed'] = null; } $info['status'] = 'good'; if ($info['queue_size'] > self::ALERT_SIZE) { $info['status'] = 'bad'; } return $info; }
/** * Display Some statistics about Finished Jobs. * @return null */ public function stats() { $this->out('Jobs currenty in the Queue:'); $types = $this->QueuedTask->getTypes(); foreach ($types as $type) { $this->out(" " . str_pad($type, 20, ' ', STR_PAD_RIGHT) . ": " . $this->QueuedTask->getLength($type)); } $this->hr(); $this->out('Total unfinished Jobs : ' . $this->QueuedTask->getLength()); $this->hr(); $this->out('Finished Job Statistics:'); $data = $this->QueuedTask->getStats(); foreach ($data as $item) { $this->out(" " . $item['QueuedTask']['jobtype'] . ": "); $this->out(" Finished Jobs in Database: " . $item[0]['num']); $this->out(" Average Job existence : " . $item[0]['alltime'] . 's'); $this->out(" Average Execution delay : " . $item[0]['fetchdelay'] . 's'); $this->out(" Average Execution time : " . $item[0]['runtime'] . 's'); } }
require_once "lib/DB/TaskQueue.php"; require_once "lib/DB/QueuedTask.php"; require_once "lib/Slack.php"; init_database(); if ($unit = GET_int("unit")) { $queue = new TaskQueue($unit); if ($queue->has_active_task()) { Slack::log("requesting new task, while old task is still running!"); $task = $queue->get_active_task(); $task->reportError("Requested new task, while this task was still running."); } if (!$queue->has_queued_tasks()) { if (!RetriggerController::fillQueue($unit)) { die("No tasks to schedule"); } } $task = $queue->get_oldest_available_queued_task(); if (!$task) { echo json_encode(array("task" => "sleep 60", "id" => 0)); die; } $task->setStarted(); echo json_encode(array("task" => $task->task(), "id" => $task->id)); die; } else { if ($task_id = GET_int("finish")) { $task = new QueuedTask($task_id); $task->setFinished(); die; } }
} $etime = $reference - $ptime; if ($etime < 1) { return '0 seconds'; } $interval = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second'); foreach ($interval as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . ' ' . $str . ($r > 1 ? 's' : ''); } } } if ($task_id = GET_int("start")) { $task = QueuedTask::FromId($task_id); if ($task && $task->finish_time() == 0) { $task->set_available_time(0); } } echo "<table width='100%'>"; echo "<thead>"; echo "<th>Control unit id</th>"; echo "<th>Machine and mode"; echo "<th>Status"; echo "<th>Queue"; echo "<th>Last finished task"; echo "</thead>"; $qUnits = mysql_query("SELECT * FROM control_unit"); while ($unit = mysql_fetch_object($qUnits)) { echo "<tr><td>" . $unit->id;