Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | 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\Application;
6
use Illuminate\Console\Command;
7
use Illuminate\Console\Events\ScheduledTaskFailed;
8
use Illuminate\Console\Events\ScheduledTaskFinished;
9
use Illuminate\Console\Events\ScheduledTaskSkipped;
10
use Illuminate\Console\Events\ScheduledTaskStarting;
11
use Illuminate\Contracts\Debug\ExceptionHandler;
12
use Illuminate\Contracts\Events\Dispatcher;
13
use Illuminate\Support\Carbon;
14
use Illuminate\Support\Facades\Date;
15
use Symfony\Component\Console\Attribute\AsCommand;
16
use Throwable;
17
 
18
#[AsCommand(name: 'schedule:run')]
19
class ScheduleRunCommand extends Command
20
{
21
    /**
22
     * The console command name.
23
     *
24
     * @var string
25
     */
26
    protected $name = 'schedule:run';
27
 
28
    /**
29
     * The name of the console command.
30
     *
31
     * This name is used to identify the command during lazy loading.
32
     *
33
     * @var string|null
34
     *
35
     * @deprecated
36
     */
37
    protected static $defaultName = 'schedule:run';
38
 
39
    /**
40
     * The console command description.
41
     *
42
     * @var string
43
     */
44
    protected $description = 'Run the scheduled commands';
45
 
46
    /**
47
     * The schedule instance.
48
     *
49
     * @var \Illuminate\Console\Scheduling\Schedule
50
     */
51
    protected $schedule;
52
 
53
    /**
54
     * The 24 hour timestamp this scheduler command started running.
55
     *
56
     * @var \Illuminate\Support\Carbon
57
     */
58
    protected $startedAt;
59
 
60
    /**
61
     * Check if any events ran.
62
     *
63
     * @var bool
64
     */
65
    protected $eventsRan = false;
66
 
67
    /**
68
     * The event dispatcher.
69
     *
70
     * @var \Illuminate\Contracts\Events\Dispatcher
71
     */
72
    protected $dispatcher;
73
 
74
    /**
75
     * The exception handler.
76
     *
77
     * @var \Illuminate\Contracts\Debug\ExceptionHandler
78
     */
79
    protected $handler;
80
 
81
    /**
82
     * The PHP binary used by the command.
83
     *
84
     * @var string
85
     */
86
    protected $phpBinary;
87
 
88
    /**
89
     * Create a new command instance.
90
     *
91
     * @return void
92
     */
93
    public function __construct()
94
    {
95
        $this->startedAt = Date::now();
96
 
97
        parent::__construct();
98
    }
99
 
100
    /**
101
     * Execute the console command.
102
     *
103
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
104
     * @param  \Illuminate\Contracts\Events\Dispatcher  $dispatcher
105
     * @param  \Illuminate\Contracts\Debug\ExceptionHandler  $handler
106
     * @return void
107
     */
108
    public function handle(Schedule $schedule, Dispatcher $dispatcher, ExceptionHandler $handler)
109
    {
110
        $this->schedule = $schedule;
111
        $this->dispatcher = $dispatcher;
112
        $this->handler = $handler;
113
        $this->phpBinary = Application::phpBinary();
114
 
115
        $this->newLine();
116
 
117
        foreach ($this->schedule->dueEvents($this->laravel) as $event) {
118
            if (! $event->filtersPass($this->laravel)) {
119
                $this->dispatcher->dispatch(new ScheduledTaskSkipped($event));
120
 
121
                continue;
122
            }
123
 
124
            if ($event->onOneServer) {
125
                $this->runSingleServerEvent($event);
126
            } else {
127
                $this->runEvent($event);
128
            }
129
 
130
            $this->eventsRan = true;
131
        }
132
 
133
        if (! $this->eventsRan) {
134
            $this->components->info('No scheduled commands are ready to run.');
135
        } else {
136
            $this->newLine();
137
        }
138
    }
139
 
140
    /**
141
     * Run the given single server event.
142
     *
143
     * @param  \Illuminate\Console\Scheduling\Event  $event
144
     * @return void
145
     */
146
    protected function runSingleServerEvent($event)
147
    {
148
        if ($this->schedule->serverShouldRun($event, $this->startedAt)) {
149
            $this->runEvent($event);
150
        } else {
151
            $this->components->info(sprintf(
152
                'Skipping [%s], as command already run on another server.', $event->getSummaryForDisplay()
153
            ));
154
        }
155
    }
156
 
157
    /**
158
     * Run the given event.
159
     *
160
     * @param  \Illuminate\Console\Scheduling\Event  $event
161
     * @return void
162
     */
163
    protected function runEvent($event)
164
    {
165
        $summary = $event->getSummaryForDisplay();
166
 
167
        $command = $event instanceof CallbackEvent
168
            ? $summary
169
            : trim(str_replace($this->phpBinary, '', $event->command));
170
 
171
        $description = sprintf(
172
            '<fg=gray>%s</> Running [%s]%s',
173
            Carbon::now()->format('Y-m-d H:i:s'),
174
            $command,
175
            $event->runInBackground ? ' in background' : '',
176
        );
177
 
178
        $this->components->task($description, function () use ($event) {
179
            $this->dispatcher->dispatch(new ScheduledTaskStarting($event));
180
 
181
            $start = microtime(true);
182
 
183
            try {
184
                $event->run($this->laravel);
185
 
186
                $this->dispatcher->dispatch(new ScheduledTaskFinished(
187
                    $event,
188
                    round(microtime(true) - $start, 2)
189
                ));
190
 
191
                $this->eventsRan = true;
192
            } catch (Throwable $e) {
193
                $this->dispatcher->dispatch(new ScheduledTaskFailed($event, $e));
194
 
195
                $this->handler->report($e);
196
            }
197
 
198
            return $event->exitCode == 0;
199
        });
200
 
201
        if (! $event instanceof CallbackEvent) {
202
            $this->components->bulletList([
203
                $event->getSummaryForDisplay(),
204
            ]);
205
        }
206
    }
207
}