Exemplo n.º 1
0
 public function run()
 {
     if (fnmatch('*cli*', php_sapi_name())) {
         $dir = Config::get('dir.schedules', APPLICATION_PATH . DS . 'schedules');
         if (is_dir($dir)) {
             Timer::start();
             Cli::show("Start of execution", 'COMMENT');
             $files = glob($dir . DS . '*.php');
             foreach ($files as $file) {
                 require_once $file;
                 $object = str_replace('.php', '', Arrays::last(explode(DS, $file)));
                 $class = 'Thin\\' . ucfirst(Inflector::camelize($object . '_schedule'));
                 $instance = lib('app')->make($class);
                 $methods = get_class_methods($instance);
                 Cli::show("Start schedule '{$object}'", 'COMMENT');
                 foreach ($methods as $method) {
                     $when = $this->getWhen($instance, $method);
                     $isDue = $this->isDue($object, $method, $when);
                     if (true === $isDue) {
                         Cli::show("Execution of {$object}->{$method}", 'INFO');
                         $instance->{$method}();
                     } else {
                         Cli::show("No need to execute {$object}->{$method}", 'QUESTION');
                     }
                 }
             }
             Cli::show("Time of execution [" . Timer::get() . " s.]", 'SUCCESS');
             Cli::show("end of execution", 'COMMENT');
         }
     }
 }
Exemplo n.º 2
0
 public function run($name, $when, $event, $args = [])
 {
     Timer::start();
     Cli::show("Start of execution", 'SUCCESS');
     $db = rdb('cron', 'task');
     $dbCron = $db->firstOrCreate(['name' => $name]);
     $nextDb = $dbCron->next;
     $cron = CronExpression::factory($when);
     $next = $cron->getNextRunDate()->format('Y-m-d-H-i-s');
     list($y, $m, $d, $h, $i, $s) = explode('-', $next, 6);
     $timestamp = mktime($h, $i, $s, $m, $d, $y);
     if ($nextDb) {
         if ($nextDb < $timestamp) {
             Cli::show("Execution {$name}", 'COMMENT');
             call_user_func_array($event, $args);
             $dbCron->setNext($timestamp)->save();
         }
     } else {
         $dbCron->setNext($timestamp)->save();
     }
     Cli::show('Elapsed time: ' . Timer::get() . ' s.', 'INFO');
     Cli::show("End of execution", 'SUCCESS');
 }