public function setCronStatus($jobKey, $status) { $dba = $this->getAdapter(); if ($jobKey == 'all') { $register = new Ot_Cron_JobRegister(); $jobs = $register->getJobs(); foreach ($jobs as $j) { $data = array('status' => $status); $job = $this->find($j->getKey()); if (!is_null($job)) { $where = $dba->quoteInto('jobKey = ?', $j->getKey()); $this->update($data, $where); } else { $data['jobKey'] = $j->getKey(); $this->insert($data); } } } else { $data = array('status' => $status); $job = $this->find($jobKey); if (!is_null($job)) { $where = $dba->quoteInto('jobKey = ?', $jobKey); $this->update($data, $where); } else { $data['jobKey'] = $jobKey; $this->insert($data); } } }
/** * shows all the cron jobs * */ public function indexAction() { $this->view->acl = array('add' => false, 'edit' => false, 'toggle' => $this->_helper->hasAccess('toggle'), 'acl' => $this->_helper->hasAccess('index', 'ot_acl')); $role = new Ot_Model_DbTable_Role(); $statusModel = new Ot_Model_DbTable_CronStatus(); $statusMarkers = $statusModel->fetchAll(); $cjStatus = array(); foreach ($statusMarkers as $s) { $cjStatus[$s->jobKey] = array('status' => $s->status, 'lastRunDt' => $s->lastRunDt); } $jobs = array(); $cjr = new Ot_Cron_JobRegister(); $registeredJobs = $cjr->getJobs(); foreach ($registeredJobs as $j) { $cschedule = $j->getSchedule(); if (count(explode(' ', $cschedule)) == 5) { $cschedule .= ' *'; } $parts = explode(' ', $cschedule); $parts[0] = preg_replace('/\\*\\//', '0-59/', $parts[0]); $parts[1] = preg_replace('/\\*\\//', '0-23/', $parts[1]); $parts[2] = preg_replace('/\\*\\//', '1-31/', $parts[2]); $parts[3] = preg_replace('/\\*\\//', '1-12/', $parts[3]); $parts[4] = preg_replace('/\\*\\//', '0-6/', $parts[4]); $cschedule = implode($parts, ' '); try { $schedule = Ot_Cron_Schedule::fromCronString($cschedule); } catch (Exception $e) { $schedule = null; } $jobs[] = array('job' => $j, 'isEnabled' => isset($cjStatus[$j->getKey()]) && $cjStatus[$j->getKey()]['status'] == 'enabled', 'lastRunDt' => isset($cjStatus[$j->getKey()]) ? $cjStatus[$j->getKey()]['lastRunDt'] : 0, 'schedule' => is_null($schedule) ? $cschedule : $schedule->asNaturalLanguage()); } $this->view->assign(array('defaultRole' => $role->find($this->_helper->configVar('defaultRole')), 'guestHasAccess' => $this->_helper->hasAccess('index', 'ot_cronjob', $this->_helper->configVar('defaultRole')), 'cronjobs' => $jobs)); $this->_helper->pageTitle('ot-cron-index:title'); }
/** * Dispatches the cron jobs * * @param int $ */ public function dispatch($jobKey = null) { $register = new Ot_Cron_JobRegister(); $cs = new Ot_Model_DbTable_CronStatus(); if (!is_null($jobKey)) { $thisJob = $register->getJob($jobKey); if (is_null($jobKey)) { throw new Exception('Job not found'); } if (!$cs->isEnabled($thisJob->getKey())) { throw new Ot_Exception('Job is disabled and cannot be run'); } $lastRunDt = $cs->getLastRunDt($thisJob->getKey()); $jobObj = $thisJob->getJobObj(); // make sure job isn't already running if (($pid = Ot_Cron_Lock::lock($thisJob->getKey())) == FALSE) { return; } $jobObj->execute($lastRunDt); Ot_Cron_Lock::unlock($thisJob->getKey()); $cs->executed($thisJob->getKey(), time()); return; } $now = time(); $jobs = $register->getJobs(); foreach ($jobs as $job) { if ($this->shouldExecute($job->getSchedule(), $now)) { if (!$cs->isEnabled($job->getKey())) { continue; } $lastRunDt = $cs->getLastRunDt($job->getKey()); $jobObj = $job->getJobObj(); if (($pid = Ot_Cron_Lock::lock($job->getKey())) == FALSE) { continue; } $jobObj->execute($lastRunDt); Ot_Cron_Lock::unlock($job->getKey()); $cs->executed($job->getKey(), time()); } } }