public function run($argv) { // fetch task info $taskId = $argv[0]; $task = $this->taskDao->find($taskId); if ($task === false) { $this->logger->error('Task not found.'); return; } // TODO update pid $jobId = $task['job_id']; $developTaskId = $this->jobDao->getDevelopTaskId($task['job_id']); // open process $taskLogsDir = Config::get('taskLogsDir'); $desc = [1 => ['file', $taskLogsDir . '/scheduler_task_' . $taskId . '.out', 'w'], 2 => ['file', $taskLogsDir . '/scheduler_task_' . $taskId . '.err', 'w']]; $process = proc_open('php ' . __DIR__ . '/launcher.php develop_run.php ' . $developTaskId, $desc, $pipes); $this->logger->info("Run [task_id: {$taskId}, job_id: {$jobId}, develop_task_id: {$developTaskId}]"); // wait while (!$this->taskDao->isInterrupted($taskId)) { $processStatus = proc_get_status($process); if (!$processStatus['running']) { if ($processStatus['exitcode'] == 0) { $taskStatus = SchedulerTaskDao::STATUS_SUCCESS; } else { $taskStatus = SchedulerTaskDao::STATUS_FAILURE; } $this->taskDao->updateStatus($taskId, $taskStatus); $this->jobDao->updateTaskStatus($jobId, $taskStatus); if ($taskStatus == SchedulerTaskDao::STATUS_SUCCESS) { $this->jobDao->insertSignal($jobId, $this->now->format('Y-m-d')); } $this->logger->info('Task finished, exitcode ' . $processStatus['exitcode']); proc_close($process); return; } sleep(1); } $this->logger->warn('Task is interrupted, send SIGTERM.'); proc_terminate($process, SIGTERM); // stop gracefully $countDown = 5; while ($countDown > 0) { $processStatus = proc_get_status($process); if (!$processStatus['running']) { break; } sleep(1); --$countDown; } if ($countDown == 0) { $this->logger->warn('Send SIGKILL.'); proc_terminate($process, SIGKILL); } $this->taskDao->updateStatus($taskId, SchedulerTaskDao::STATUS_FAILURE); $this->jobDao->updateTaskStatus($jobId, SchedulerTaskDao::STATUS_FAILURE); $this->logger->info('Task killed.'); proc_close($process); }
private function tryRunTask($taskId) { // check limit if ($this->taskDao->countRunning() >= self::TASK_POOL_SIZE) { $this->logger->warn('Task pool is full.'); return; } $task = $this->taskDao->find($taskId); $jobId = $task['job_id']; // check signal $jobIds = $this->jobDao->getDependencies($jobId); if (count($jobIds) > 0) { if (!$this->jobDao->checkSignals($jobIds, $this->now->format('Y-m-d'))) { $this->logger->debug("Task {$taskId} is waiting for signal."); return; } } // run task $this->taskDao->updateStatus($taskId, SchedulerTaskDao::STATUS_RUNNING); $this->jobDao->updateTaskStatus($jobId, SchedulerTaskDao::STATUS_RUNNING); shell_exec('php ' . __DIR__ . '/launcher.php scheduler_run.php ' . $taskId . ' >/dev/null 2>&1 &'); $this->logger->info("Run task [id: {$taskId}, job_id: {$jobId}]"); }