示例#1
0
 public function OnStartForking()
 {
     // start cron, which runs scripts by it's schedule queue
     try {
         $db = \Scalr::getDb();
         // set status to "finished" for active tasks, which ended or executed once
         $db->Execute("UPDATE scheduler SET `status` = ? WHERE\n                `status` = ? AND (\n                    CONVERT_TZ(`end_time`,'SYSTEM',`timezone`) < CONVERT_TZ(NOW(),'SYSTEM',`timezone`) OR\n                    (CONVERT_TZ(`last_start_time`,'SYSTEM',`timezone`) < CONVERT_TZ(NOW(),'SYSTEM',`timezone`) AND `restart_every` = 0)\n                )", array(Scalr_SchedulerTask::STATUS_FINISHED, Scalr_SchedulerTask::STATUS_ACTIVE));
         // get active tasks: first run (condition and last_start_time is null), others (condition and last_start_time + interval * 0.9 < now())
         $taskList = $db->GetAll("SELECT *\n                FROM scheduler\n                WHERE\n                    `status` = ? AND\n                    (`end_time` IS NULL OR CONVERT_TZ(`end_time`,'SYSTEM',`timezone`) > CONVERT_TZ(NOW(),'SYSTEM',`timezone`)) AND\n                    (`start_time` IS NULL OR CONVERT_TZ(`start_time`,'SYSTEM',`timezone`) <= CONVERT_TZ(NOW(),'SYSTEM',`timezone`)) AND\n                    (\n                        `last_start_time` IS NULL OR\n                        `last_start_time` IS NOT NULL AND `start_time` IS NULL AND (CONVERT_TZ(last_start_time + INTERVAL restart_every MINUTE, 'SYSTEM', `timezone`) < CONVERT_TZ(NOW(),'SYSTEM',`timezone`)) OR\n                        `last_start_time` IS NOT NULL AND `start_time` IS NOT NULL AND (CONVERT_TZ(last_start_time + INTERVAL (restart_every * 0.9) MINUTE, 'SYSTEM', `timezone`) < CONVERT_TZ(NOW(),'SYSTEM',`timezone`))\n                    )\n                ORDER BY IF (last_start_time, last_start_time, start_time), order_index ASC\n            ", array(Scalr_SchedulerTask::STATUS_ACTIVE));
         if (!$taskList) {
             $this->Logger->info(_("There is no tasks to execute in scheduler table"));
             exit;
         }
         foreach ($taskList as $task) {
             // check account status (active or inactive)
             try {
                 if (Scalr_Account::init()->loadById($task['account_id'])->status != Scalr_Account::STATUS_ACTIVE) {
                     continue;
                 }
             } catch (Exception $e) {
                 $this->Logger->info("Invalid scheduler task #{$task['id']}: {$e->getMessage()}");
             }
             if ($task['last_start_time'] && $task['start_time']) {
                 // try to auto-align time to start time
                 $startTime = new DateTime($task['start_time']);
                 $startTime->setTimezone(new DateTimeZone($task['timezone']));
                 $currentTime = new DateTime('now', new DateTimeZone($task['timezone']));
                 $offset = $startTime->getOffset() - $currentTime->getOffset();
                 $num = ($currentTime->getTimestamp() - $startTime->getTimestamp() - $offset) / ($task['restart_every'] * 60);
                 // num should be less than 0.5
                 if (floor($num) != round($num, 0, PHP_ROUND_HALF_UP)) {
                     continue;
                 }
             }
             $taskObj = new Scalr_SchedulerTask();
             $taskObj->loadById($task['id']);
             if ($taskObj->execute()) {
                 // convert_tz
                 $taskObj->updateLastStartTime();
                 $this->Logger->info(sprintf("Task {$taskObj->id} successfully sent"));
             }
         }
     } catch (Exception $e) {
         $this->Logger->warn(sprintf("Can't execute task {$task['id']}. Error message: %s", $e->getMessage()));
     }
 }
示例#2
0
 /**
  * @param JsonData $tasksIds
  */
 public function xExecuteAction(JsonData $tasksIds)
 {
     $this->request->restrictAccess(Acl::RESOURCE_GENERAL_SCHEDULERTASKS, Acl::PERM_GENERAL_SCHEDULERTASKS_MANAGE);
     $executed = [];
     $processed = [];
     foreach ($tasksIds as $taskId) {
         $task = new Scalr_SchedulerTask();
         $task->loadById($taskId);
         $this->user->getPermissions()->validate($task);
         if ($task->execute(true)) {
             $executed[] = $task->name;
             $processed[] = $taskId;
         }
     }
     if (count($executed)) {
         $this->response->data(['processed' => $processed]);
         $this->response->success("Task(s): " . implode($executed, ', ') . " successfully executed.");
     } else {
         $this->response->warning('Target of task(s) could not be found.');
     }
 }
示例#3
0
 public function xExecuteAction()
 {
     $this->request->defineParams(array('tasks' => array('type' => 'json')));
     $executed = array();
     foreach ($this->getParam('tasks') as $taskId) {
         $task = new Scalr_SchedulerTask();
         $task->loadById($taskId);
         $this->user->getPermissions()->validate($task);
         if ($task->status != Scalr_SchedulerTask::STATUS_ACTIVE) {
             continue;
         }
         if ($task->execute(true)) {
             $executed[] = $task->name;
         }
     }
     if (count($executed)) {
         $this->response->success("Task(s): " . implode($executed, ', ') . " successfully executed");
     } else {
         $this->response->warning('Target of task could not be found');
     }
 }