public function setUp() { parent::setUp(); $this->sm = Bootstrap::getServiceManager(); $this->em = $this->sm->get('doctrine.entitymanager.orm_default'); $cronData1 = new Cronjob(); $cronData1->setCode('TestCode1'); $cronData1->setStatus('pending'); $cronData1->setCreateTime(new \DateTime('2013-11-4')); $cronData1->setScheduleTime(new \DateTime('2013-11-4')); $cronData2 = new Cronjob(); $cronData2->setCode('TestCode2'); $cronData2->setStatus('pending'); $cronData2->setCreateTime(new \DateTime('2013-11-5')); $cronData2->setScheduleTime(new \DateTime('2013-11-5')); $this->cronData[] = $cronData1; $this->cronData[] = $cronData2; }
/** * 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 = $this->getCronjobs(); 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\Cronjob(); if ($this->matchTime($scheduleTimestamp, $item['frequency'])) { $job->setCode($code)->setStatus(Mapper\Cronjob::STATUS_PENDING)->setCreateTime(new \DateTime())->setScheduleTime($scheduleTime); $em->persist($job); $exists[$identifier] = true; } } } $em->flush(); return $this; }