Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
Строк: 265
<?php

namespace IlluminateConsoleScheduling;

use 
IlluminateConsoleApplication;
use 
IlluminateConsoleCommand;
use 
IlluminateConsoleEventsScheduledTaskFailed;
use 
IlluminateConsoleEventsScheduledTaskFinished;
use 
IlluminateConsoleEventsScheduledTaskSkipped;
use 
IlluminateConsoleEventsScheduledTaskStarting;
use 
IlluminateContractsDebugExceptionHandler;
use 
IlluminateContractsEventsDispatcher;
use 
IlluminateSupportCarbon;
use 
IlluminateSupportFacadesDate;
use 
SymfonyComponentConsoleAttributeAsCommand;
use 
Throwable;

#[AsCommand(name: 'schedule:run')]
class ScheduleRunCommand extends Command
{
    
/**
     * The console command name.
     *
     * @var string
     */
    
protected $name 'schedule:run';

    
/**
     * The name of the console command.
     *
     * This name is used to identify the command during lazy loading.
     *
     * @var string|null
     *
     * @deprecated
     */
    
protected static $defaultName 'schedule:run';

    
/**
     * The console command description.
     *
     * @var string
     */
    
protected $description 'Run the scheduled commands';

    
/**
     * The schedule instance.
     *
     * @var IlluminateConsoleSchedulingSchedule
     */
    
protected $schedule;

    
/**
     * The 24 hour timestamp this scheduler command started running.
     *
     * @var IlluminateSupportCarbon
     */
    
protected $startedAt;

    
/**
     * Check if any events ran.
     *
     * @var bool
     */
    
protected $eventsRan false;

    
/**
     * The event dispatcher.
     *
     * @var IlluminateContractsEventsDispatcher
     */
    
protected $dispatcher;

    
/**
     * The exception handler.
     *
     * @var IlluminateContractsDebugExceptionHandler
     */
    
protected $handler;

    
/**
     * The PHP binary used by the command.
     *
     * @var string
     */
    
protected $phpBinary;

    
/**
     * Create a new command instance.
     *
     * @return void
     */
    
public function __construct()
    {
        
$this->startedAt Date::now();

        
parent::__construct();
    }

    
/**
     * Execute the console command.
     *
     * @param  IlluminateConsoleSchedulingSchedule  $schedule
     * @param  IlluminateContractsEventsDispatcher  $dispatcher
     * @param  IlluminateContractsDebugExceptionHandler  $handler
     * @return void
     */
    
public function handle(Schedule $scheduleDispatcher $dispatcherExceptionHandler $handler)
    {
        
$this->schedule $schedule;
        
$this->dispatcher $dispatcher;
        
$this->handler $handler;
        
$this->phpBinary Application::phpBinary();

        
$this->newLine();

        foreach (
$this->schedule->dueEvents($this->laravel) as $event) {
            if (! 
$event->filtersPass($this->laravel)) {
                
$this->dispatcher->dispatch(new ScheduledTaskSkipped($event));

                continue;
            }

            if (
$event->onOneServer) {
                
$this->runSingleServerEvent($event);
            } else {
                
$this->runEvent($event);
            }

            
$this->eventsRan true;
        }

        if (! 
$this->eventsRan) {
            
$this->components->info('No scheduled commands are ready to run.');
        } else {
            
$this->newLine();
        }
    }

    
/**
     * Run the given single server event.
     *
     * @param  IlluminateConsoleSchedulingEvent  $event
     * @return void
     */
    
protected function runSingleServerEvent($event)
    {
        if (
$this->schedule->serverShouldRun($event$this->startedAt)) {
            
$this->runEvent($event);
        } else {
            
$this->components->info(sprintf(
                
'Skipping [%s], as command already run on another server.'$event->getSummaryForDisplay()
            ));
        }
    }

    
/**
     * Run the given event.
     *
     * @param  IlluminateConsoleSchedulingEvent  $event
     * @return void
     */
    
protected function runEvent($event)
    {
        
$summary $event->getSummaryForDisplay();

        
$command $event instanceof CallbackEvent
            
$summary
            
trim(str_replace($this->phpBinary''$event->command));

        
$description sprintf(
            
'<fg=gray>%s</> Running [%s]%s',
            
Carbon::now()->format('Y-m-d H:i:s'),
            
$command,
            
$event->runInBackground ' in background' '',
        );

        
$this->components->task($description, function () use ($event) {
            
$this->dispatcher->dispatch(new ScheduledTaskStarting($event));

            
$start microtime(true);

            try {
                
$event->run($this->laravel);

                
$this->dispatcher->dispatch(new ScheduledTaskFinished(
                    
$event,
                    
round(microtime(true) - $start2)
                ));

                
$this->eventsRan true;
            } catch (
Throwable $e) {
                
$this->dispatcher->dispatch(new ScheduledTaskFailed($event$e));

                
$this->handler->report($e);
            }

            return 
$event->exitCode == 0;
        });

        if (! 
$event instanceof CallbackEvent) {
            
$this->components->bulletList([
                
$event->getSummaryForDisplay(),
            ]);
        }
    }
}
Онлайн: 0
Реклама