public static function loadClass(ReflectionClass $refl, Reader $reader, Scheduler $scheduler) { /** @var \Destiny\Common\Annotation\Schedule $annotation */ $annotation = $reader->getClassAnnotation($refl, 'Destiny\\Common\\Annotation\\Schedule'); if (!empty($annotation)) { $scheduler->addTask(array('action' => $refl->getShortName(), 'frequency' => $annotation->frequency, 'period' => $annotation->period)); } }
/** * @Route ("/admin/cron") * @Secure ({"ADMIN"}) * @Transactional * * @param array $params * @throws Exception */ public function adminCron(array $params) { if (!isset($params['id']) || empty($params['id'])) { throw new Exception('Action id required.'); } set_time_limit(180); $log = Application::instance()->getLogger(); $response = array(); $scheduler = new Scheduler(Config::$a['scheduler']); $scheduler->setLogger($log); $scheduler->loadSchedule(); $scheduler->executeTaskByName($params['id']); $response['message'] = sprintf('Execute %s', $params['id']); $response = new Response(Http::STATUS_OK, json_encode($response)); $response->addHeader(Http::HEADER_CONTENTTYPE, MimeType::JSON); return $response; }
<?php use Destiny\Common\Application; use Destiny\Common\Scheduler; require __DIR__ . '/../lib/boot.php'; $app = Application::instance(); $scheduler = new Scheduler(); $scheduler->setLogger($app->getLogger()); $scheduler->loadSchedule(); $scheduler->execute();
<?php use Destiny\Common\Application; use Destiny\Common\Exception; use Destiny\Common\Scheduler; use Destiny\Common\Config; ini_set('mysql.connect_timeout', 10); ini_set('max_execution_time', 60); $context = new \stdClass(); $context->log = 'cron'; require __DIR__ . '/../lib/boot.php'; $app = Application::instance(); // Cron is run every 60 seconds. // There can be a time where actions are executed before they have ended $log = $app->getLogger(); $scheduler = new Scheduler(Config::$a['scheduler']); $scheduler->setLogger($log); $scheduler->loadSchedule(); $stime = microtime(true); try { echo PHP_EOL . 'Scheduler starting'; $scheduler->executeShedule(false); echo PHP_EOL . 'Scheduler completed'; } catch (Exception $e) { $log->error($e->getMessage()); echo PHP_EOL . 'Scheduler completed with errors'; } catch (\Exception $e) { $log->critical($e->getMessage()); echo PHP_EOL . 'Scheduler completed with errors'; } echo PHP_EOL . 'Completed in ' . (microtime(true) - $stime) . ' seconds';