diff --git a/src/Commands/Work.php b/src/Commands/Work.php new file mode 100644 index 0000000..77666ae --- /dev/null +++ b/src/Commands/Work.php @@ -0,0 +1,76 @@ + 'Le fichier vers lequel diriger la sortie de `tasks:run`.', + ]; + + /** + * {@inheritDoc} + */ + protected $description = 'Démarre le planificateur de tâche et l\'exécute localement.'; + + /** + * {@inheritDoc} + */ + protected $required = [ + 'symfony/process:^7.2', + ]; + + /** + * {@inheritDoc} + */ + public function execute(array $params) + { + $this->info('Exéctutions des tâches programmées.'); + + [$lastExecutionStartedAt, $executions] = [Date::now()->subMinutes(10), []]; + + $command = sprintf( + '%s %s %s', + escapeshellarg(PHP_BINARY), + escapeshellarg(base_path('klinge')), + 'tasks:run' + ); + + if ($this->option('run-output-file')) { + $command .= ' >> ' . escapeshellarg($this->option('run-output-file')) . ' 2>&1'; + } + + while (true) { + usleep(100 * 1000); + + if (intval(Date::now()->getSecond()) === 0 && ! Date::now()->startOfMinute()->equalTo($lastExecutionStartedAt)) { + $executions[] = $execution = Process::fromShellCommandline($command); + + $execution->start(); + + $lastExecutionStartedAt = Date::now()->startOfMinute(); + } + + foreach ($executions as $key => $execution) { + $output = $execution->getIncrementalOutput(). $execution->getIncrementalErrorOutput(); + + $this->write(ltrim($output, "\n"))->eol(); + + if (! $execution->isRunning()) { + unset($executions[$key]); + } + } + } + } +}