コード例 #1
0
 /**
  * Construct a new AutoCrontabJob
  *
  * @param string $identifier Hopefully unique!
  * @param string $script Path to data collection script (probably `__FILE__`)
  * @param string|TiBeN\CrontabManager\CrontabJob Cron schedule or a complete
  *		`TiBeN\CrontabManager\CrontabJob`
  * @param string (Optional) path to log file (defaults to same directory and
  *		name as the script file) e.g. a script at `/var/www/foo/bar.php` would
  *		have a log at `/var/www/foo/bar.log`
  *
  * @throws AutoCrontabJob_Exception CONSTRUCTOR_ERROR If parameters are not validated
  **/
 public function __construct($identifier, $script, $schedule, $log = null)
 {
     /* Make sure the scheduled script file exists... */
     if (file_exists($script)) {
         /* try to make the identifier truly unique to this instance */
         $_identifier = $identifier . '.' . md5($identifier . __FILE__ . __CLASS__);
         /* ensure that we're working with a valid Cron job */
         $newJob = null;
         if (is_string($schedule)) {
             $newJob = \TiBeN\CrontabManager\CrontabJob::createFromCrontabLine($schedule . " php {$script}");
         } elseif ($schedule instanceof \TiBeN\CrontabManager\CrontabJob) {
             $newJob = $schedule;
         } else {
             throw new CanvasDataCollector_Exception('Expected a string or TiBeN\\CrontabManager\\CrontabJob, received ' . print_r($schedule, true), CanvasDataCollector_Exception::CONSTRUCTOR_ERROR);
         }
         $newJob->comments = implode(' ', array($newJob->comments, "Created by {$script}:" . get_class($this) . ' ' . date('Y-m-d h:ia') . " (Job ID {$_identifier})"));
         /* update cron if this job already exists */
         $this->cron = new \TiBeN\CrontabManager\CrontabRepository(new \TiBeN\CrontabManager\CrontabAdapter());
         $jobs = $this->cron->getJobs();
         $jobExists = false;
         // TODO Rewrite whenever this is fixed: https://github.com/TiBeN/CrontabManager/issues/3
         foreach ($jobs as $job) {
             if (preg_match("/{$_identifier}/", $job->formatCrontabLine())) {
                 $jobExists = true;
                 $job->minutes = $newJob->minutes;
                 $job->hours = $newJob->hours;
                 $job->dayOfMonth = $newJob->dayOfMonth;
                 $job->months = $newJob->months;
                 $job->dayOfWeek = $newJob->dayOfWeek;
                 break;
             }
         }
         /* ... or add this as a new job if it doesn't exist */
         if (!$jobExists) {
             $this->cron->addJob($newJob);
         }
         /* set up log file */
         if (empty($log)) {
             $log = dirname($script) . '/' . basename($script, '.php') . '.log';
         }
         $this->log = \Log::singleton('file', $log);
         /* update cron to enable scheduled jobs */
         $this->cron->persist();
     } else {
         throw new CanvasDataCollector("PHP script '{$script}' does not exist", CanvasDataCollector_Exception::CONSTRUCTOR_ERROR);
     }
 }
コード例 #2
0
 /**
  * @expectedException InvalidArgumentException
  * @expectedExceptionMessage CrontabJob contain's no task command line
  */
 public function testNoTaskSetException()
 {
     $crontabJob = new CrontabJob();
     $crontabJob->minutes = '30';
     $crontabJob->hours = '23';
     $crontabJob->formatCrontabLine();
 }
コード例 #3
0
 /**
  * Test remove an unknown Job
  * @expectedException LogicException
  * @expectedExceptionMessage This job is not part of this crontab
  */
 public function testRemoveAnUnknownJob()
 {
     $fakeCrontabAdapter = $this->getMock('TiBeN\\CrontabManager\\CrontabAdapter');
     $fakeCrontabAdapter->expects($this->any())->method('readCrontab')->will($this->returnValue(file_get_contents($this->fixturesPath . 'simple_crontab.txt')));
     $crontabRepository = new CrontabRepository($fakeCrontabAdapter);
     $job = CrontabJob::createFromCrontabLine('30 23 * * * launch -param mycommand');
     $crontabRepository->removeJob($job);
 }