Ejemplo n.º 1
0
 /**
  * Register a schedule (job)
  *
  * @param   string    $name         Job name (unique)
  * @param   string    $task         Target task
  * @param   string    $expression   A valid cron expression
  * @param   string    $description  (optional) A brief description for the job
  * @param   bool      $parameters   (optional) array of parameters to pass to task
  *
  * @return  bool
  */
 public final function addSchedule($name, $task, $expression, $description = null, $parameters = array())
 {
     if (empty($name) or empty($task) or empty($expression)) {
         return false;
     }
     try {
         Scheduler::validateExpression($expression);
     } catch (Exception $e) {
         return false;
     }
     list($min, $hour, $dayofmonth, $month, $dayofweek, $year) = explode(" ", trim($expression));
     array_push($this->schedules, array("name" => $name, "task" => $task, "description" => is_null($description) ? '' : $description, "min" => $min, "hour" => $hour, "dayofmonth" => $dayofmonth, "month" => $month, "dayofweek" => $dayofweek, "year" => $year, "params" => $parameters));
     return true;
 }
 private static function uploadJobs($jobs)
 {
     $imported = 0;
     foreach ($jobs as $job) {
         $expression = $job["min"] . " " . $job["hour"] . " " . $job["dayofmonth"] . " " . $job["month"] . " " . $job["dayofweek"] . " " . $job["year"];
         try {
             $parameters = unserialize($job["params"]);
             list($id, $next_calculated_run) = Scheduler::addSchedule($expression, $job["name"], $job["task"], $job["description"], $parameters);
             if ($job["enabled"]) {
                 Scheduler::enableSchedule($job["name"]);
             }
         } catch (Exception $e) {
             throw $e;
         }
         $imported++;
     }
     return $imported;
 }
Ejemplo n.º 3
0
 public function testScheduler()
 {
     $result = Scheduler::addSchedule("* * * * *", "test", "task", "description", array());
     $this->assertInternalType('array', $result);
     $this->assertInternalType('integer', $result[0]);
     $get = $result[0];
     $result = Scheduler::getSchedule("test");
     $this->assertInternalType('array', $result);
     $this->assertEquals($get, $result["id"]);
     $time = time();
     $result = Scheduler::updateSchedule("test", $time);
     $this->assertNull($result);
     $result = Scheduler::getSchedule("test");
     $this->assertEquals($time, $result["lastrun"]);
     $result = Scheduler::enableSchedule("test");
     $this->assertTrue($result);
     $result = Scheduler::disableSchedule("test");
     $this->assertTrue($result);
     $result = Scheduler::removeSchedule("test");
     $this->assertTrue($result);
 }
Ejemplo n.º 4
0
 private function cycle()
 {
     // fire extender ready event
     $this->events->fire("extender", "VOID", $this);
     // dispatch signals (if multithread active)
     if ($this->getMultithreadMode()) {
         pcntl_signal_dispatch();
     }
     // if extender is paused (SIGINT), skip to extend
     if ($this->paused) {
         return;
     }
     // fix relative timestamp
     $this->timestamp = microtime(true);
     // fire tasktable event
     $this->tasks = $this->events->fire("extender.tasks", "TASKSTABLE", $this->tasks);
     // get the next planned activity interval
     $plans = Planner::get();
     if (!is_null($plans) and $this->timestamp < $plans) {
         // nothing to do right now, still waiting if in daemon mode
         $this->logger->info("Next planned job: " . date('c', $plans));
         $this->logger->notice("Extender completed\n");
         if ($this->getDaemonMode() === false) {
             $this->shutdown(true);
             self::end(0);
         }
         return;
     }
     // if no plan is retrieved, try to retrieve it from scheduler
     try {
         // get schedules and dispatch schedule event
         list($schedules, $planned) = Scheduler::getSchedules($this->logger, $this->timestamp);
         // write next planned activity interval
         if (!is_null($planned) and $planned != 0) {
             Planner::set($planned);
         }
         $scheduled = new Schedule();
         $scheduled->setSchedules($schedules);
         // expose the current shcedule via events
         $scheduled = $this->events->fire("extender.schedule", "SCHEDULE", $scheduled);
         // if no jobs in queue, exit gracefully
         if ($scheduled->howMany() == 0) {
             $this->logger->info("No jobs to process right now, exiting");
             $this->logger->notice("Extender completed\n");
             if ($this->getDaemonMode() === false) {
                 $this->shutdown(true);
                 self::end(0);
             }
             return;
         }
         // compose jobs
         foreach ($scheduled->getSchedules() as $schedule) {
             if ($this->tasks->isRegistered($schedule['task'])) {
                 $job = new Job();
                 $job->setName($schedule['name'])->setId($schedule['id'])->setParameters(unserialize($schedule['params']))->setTask($schedule['task'])->setClass($this->tasks->getClass($schedule['task']));
                 $this->runner->addJob($job);
             } else {
                 $this->logger->warning("Skipping job due to unknown task", array("ID" => $schedule['id'], "NAME" => $schedule['name'], "TASK" => $schedule['task']));
             }
         }
         // lauch runner
         $result = $this->runner->run();
         // free runner for next cycle
         $this->runner->free();
         // compose results
         $results = new JobsResult($result);
         // update schedules
         Scheduler::updateSchedules($this->logger, $result);
         // increment counters
         foreach ($result as $r) {
             if ($r[2]) {
                 $this->completed_processes++;
             } else {
                 $this->failed_processes++;
             }
         }
     } catch (Exception $e) {
         $this->logger->error($e->getMessage());
         if ($this->getDaemonMode() === false) {
             self::end(1);
         }
     }
     // fire result event
     $this->events->fire("extender.result", "VOID", $results);
     $this->logger->notice("Extender completed\n");
     // show summary (if -s)
     if ($this->summary_mode) {
         self::showSummary($this->timestamp, $result, $this->color);
     }
     Status::dump($this->timestamp_absolute, $this->parent_pid, $this->completed_processes, $this->failed_processes, $this->paused);
     if ($this->getDaemonMode() === false) {
         $this->shutdown(true);
         self::end(0);
     }
 }
 public static function addToScheduler($expression, $name, $task, $description, $parameters)
 {
     try {
         list($id, $next_calculated_run) = Scheduler::addSchedule($expression, $name, $task, $description, $parameters);
     } catch (Exception $e) {
         throw $e;
     }
     return array("id" => $id, "nextrun" => $next_calculated_run, "enabled" => false);
 }