src/Service/Cron.php
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;
    }
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;
    }
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 ; }
schedule cron jobs

@return self
schedule cron jobs