Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Console\Scheduling;
4
 
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Carbon;
621 lars 7
use Illuminate\Support\ProcessUtils;
148 lars 8
use Symfony\Component\Console\Attribute\AsCommand;
621 lars 9
use Symfony\Component\Console\Output\OutputInterface;
148 lars 10
use Symfony\Component\Process\Process;
11
 
12
#[AsCommand(name: 'schedule:work')]
13
class ScheduleWorkCommand extends Command
14
{
15
    /**
621 lars 16
     * The name and signature of the console command.
148 lars 17
     *
18
     * @var string
19
     */
621 lars 20
    protected $signature = 'schedule:work {--run-output-file= : The file to direct <info>schedule:run</info> output to}';
148 lars 21
 
22
    /**
23
     * The name of the console command.
24
     *
25
     * This name is used to identify the command during lazy loading.
26
     *
27
     * @var string|null
28
     *
29
     * @deprecated
30
     */
31
    protected static $defaultName = 'schedule:work';
32
 
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Start the schedule worker';
39
 
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return void
44
     */
45
    public function handle()
46
    {
621 lars 47
        $this->components->info(
48
            'Running scheduled tasks every minute.',
49
            $this->getLaravel()->isLocal() ? OutputInterface::VERBOSITY_NORMAL : OutputInterface::VERBOSITY_VERBOSE
50
        );
148 lars 51
 
52
        [$lastExecutionStartedAt, $executions] = [null, []];
53
 
621 lars 54
        $command = implode(' ', array_map(fn ($arg) => ProcessUtils::escapeArgument($arg), [
55
            PHP_BINARY,
56
            defined('ARTISAN_BINARY') ? ARTISAN_BINARY : 'artisan',
57
            'schedule:run',
58
        ]));
59
 
60
        if ($this->option('run-output-file')) {
61
            $command .= ' >> '.ProcessUtils::escapeArgument($this->option('run-output-file')).' 2>&1';
62
        }
63
 
148 lars 64
        while (true) {
65
            usleep(100 * 1000);
66
 
67
            if (Carbon::now()->second === 0 &&
68
                ! Carbon::now()->startOfMinute()->equalTo($lastExecutionStartedAt)) {
621 lars 69
                $executions[] = $execution = Process::fromShellCommandline($command);
148 lars 70
 
71
                $execution->start();
72
 
73
                $lastExecutionStartedAt = Carbon::now()->startOfMinute();
74
            }
75
 
76
            foreach ($executions as $key => $execution) {
77
                $output = $execution->getIncrementalOutput().
621 lars 78
                    $execution->getIncrementalErrorOutput();
148 lars 79
 
80
                $this->output->write(ltrim($output, "\n"));
81
 
82
                if (! $execution->isRunning()) {
83
                    unset($executions[$key]);
84
                }
85
            }
86
        }
87
    }
88
}