Example #1
0
File: Cron.php Project: iamfat/gini
 public function actionRun($name = null)
 {
     $job = \Gini\Config::get('cron')[$name];
     if (!$job) {
         return false;
     }
     $command_args = \Gini\Util::parseArgs($job['command']);
     ob_start();
     \Gini\CLI::dispatch($command_args);
     $output = ob_get_contents();
     ob_end_clean();
     return $output;
 }
Example #2
0
File: Cron.php Project: iamfat/gini
 public function actionSchedule()
 {
     // read cron cache
     $cron_cache_file = sys_get_temp_dir() . '/cron_cache_' . sha1(APP_PATH);
     $fh = fopen($cron_cache_file, 'c+');
     if ($fh) {
         if (flock($fh, LOCK_EX | LOCK_NB)) {
             $cron_cache = @json_decode(fread($fh), true) ?: [];
             $cron_config = (array) \Gini\Config::get('cron');
             foreach ($cron_config as $name => $job) {
                 $schedule = $job['schedule'] ?: $job['interval'];
                 $cron = \Cron\CronExpression::factory($schedule);
                 $cache =& $cron_cache[$name];
                 if (isset($cache)) {
                     $next = date_create($cache['next']);
                     $now = date_create('now');
                     if ($next <= $now) {
                         // we have to run it
                         $cache['last_run_at'] = $now->format('c');
                         \Gini\Logger::of('cron')->info('cron run {command}', ['command' => $job['command']]);
                         $pid = pcntl_fork();
                         if ($pid == -1) {
                             continue;
                         } elseif ($pid == 0) {
                             $command_args = \Gini\Util::parseArgs($job['command']);
                             \Gini\CLI::dispatch($command_args);
                             exit;
                         }
                     }
                 }
                 $cache['next'] = $cron->getNextRunDate()->format('c');
             }
             while (pcntl_wait($status) > 0) {
             }
             ftruncate($fh, 0);
             fwrite($fh, J($cron_cache));
             flock($fh, LOCK_UN);
         }
         fclose($fh);
     }
 }
Example #3
0
 public static function tearDownAfterClass()
 {
     \Gini\CLI::shutdown();
 }