public function testCronRegistry() { Registry::register('test', '* * * * *', array($this, 'dummy'), array()); $expectedCronRegistry = array('test' => array('frequency' => '* * * * *', 'callback' => array($this, 'dummy'), 'args' => array())); $cronRegistry = Registry::getCronRegistry(); $this->assertSame($expectedCronRegistry, $cronRegistry); }
/** * schedule cron jobs * * @return self */ public function schedule() { $em = $this->getEm(); $pending = $this->getPending(); $exists = array(); foreach ($pending as $job) { $identifier = $job->getCode(); $identifier .= $job->getScheduleTime()->getTimeStamp(); $exists[$identifier] = true; } $scheduleAhead = $this->getScheduleAhead() * 60; $cronRegistry = Registry::getCronRegistry(); foreach ($cronRegistry as $code => $item) { $now = time(); $timeAhead = $now + $scheduleAhead; for ($time = $now; $time < $timeAhead; $time += 60) { $scheduleTime = new \DateTime(); $scheduleTime->setTimestamp($time); $scheduleTime->setTime($scheduleTime->format('H'), $scheduleTime->format('i')); $scheduleTimestamp = $scheduleTime->getTimestamp(); $identifier = $code . $scheduleTimestamp; if (isset($exists[$identifier])) { //already scheduled continue; } $job = new Entity\Job(); if (Parser::matchTime($scheduleTimestamp, $item['frequency'])) { $job->setCode($code)->setStatus(Repository\Job::STATUS_PENDING)->setCreateTime(new \DateTime())->setScheduleTime($scheduleTime); $em->persist($job); $exists[$identifier] = true; } } } $em->flush(); return $this; }