目录
// namespace \Illuminate\Console\Scheduling\Schedulepublic function command($command, array $parameters = []){if (class_exists($command)) {$command = Container::getInstance()->make($command)->getName(); }return $this->exec(Application::formatCommandString($command), $parameters); }public function exec($command, array $parameters = []){if (count($parameters)) {$command .= ' '.$this->compileParameters($parameters); }$this->events[] = $event = new Event($this->eventMutex, $command, $this->timezone); return $event; }
// namespace \Illuminate\Console\Scheduling\Eventprotected function runCommandInForeground(Container $container){$this->callBeforeCallbacks($container); $this->exitCode = Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); $this->callAfterCallbacks($container); }protected function runCommandInBackground(Container $container){$this->callBeforeCallbacks($container); Process::fromShellCommandline($this->buildCommand(), base_path(), null, null, null)->run(); }public function buildCommand(){return (new CommandBuilder)->buildCommand($this); }// namespace Illuminate\Console\Scheduling\CommandBuilderpublic function buildCommand(Event $event){if ($event->runInBackground) {return $this->buildBackgroundCommand($event); }return $this->buildForegroundCommand($event); }protected function buildForegroundCommand(Event $event){$output = ProcessUtils::escapeArgument($event->output); return $this->ensureCorrectUser($event, $event->command.($event->shouldAppendOutput ? ' >> ' : ' > ').$output.' 2>&1'); }protected function buildBackgroundCommand(Event $event){$output = ProcessUtils::escapeArgument($event->output); $redirect = $event->shouldAppendOutput ? ' >> ' : ' > '; $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"'; if (windows_os()) {return 'start /b cmd /c "('.$event->command.' & '.$finished.' "%errorlevel%")'.$redirect.$output.' 2>&1"'; }return $this->ensureCorrectUser($event,'('.$event->command.$redirect.$output.' 2>&1 ; '.$finished.' "$?") > '.ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'); }
// namespace Illuminate\Console\Scheduling\Eventpublic function withoutOverlapping($expiresAt = 1440){$this->withoutOverlapping = true; $this->expiresAt = $expiresAt; return $this->then(function () {$this->mutex->forget($this); })->skip(function () {return $this->mutex->exists($this); }); }public function run(Container $container){if ($this->withoutOverlapping &&! $this->mutex->create($this)) {return; }$this->runInBackground? $this->runCommandInBackground($container): $this->runCommandInForeground($container); }
// namespace \Illuminate\Console\Scheduling\Schedule$this->eventMutex = $container->bound(EventMutex::class)? $container->make(EventMutex::class): $container->make(CacheEventMutex::class);
// namespace \Illuminate\Foundation\Console\Kernelprotected function defineConsoleSchedule(){$this->app->singleton(Schedule::class, function ($app) {return tap(new Schedule($this->scheduleTimezone()), function ($schedule) {$this->schedule($schedule->useCache($this->scheduleCache())); }); }); }protected function scheduleCache(){return Env::get('SCHEDULE_CACHE_DRIVER'); }// namespace \Illuminate\Console\Scheduling\Schedulepublic function useCache($store){if ($this->eventMutex instanceof CacheEventMutex) {$this->eventMutex->useStore($store); }/* ... ... */return $this; }// namespace \Illuminate\Console\Scheduling\CacheEventMutexpublic function create(Event $event){return $this->cache->store($this->store)->add($event->mutexName(), true, $event->expiresAt * 60); }// namespace \Illuminate\Cache\CacheManagerpublic function store($name = null){$name = $name ?: $this->getDefaultDriver(); return $this->stores[$name] = $this->get($name); }public function getDefaultDriver(){return $this->app['config']['cache.default']; }protected function get($name){return $this->stores[$name] ?? $this->resolve($name); }protected function resolve($name){$config = $this->getConfig($name); if (is_null($config)) {throw new InvalidArgumentException("Cache store [{$name}] is not defined."); }if (isset($this->customCreators[$config['driver']])) {return $this->callCustomCreator($config); } else {$driverMethod = 'create'.ucfirst($config['driver']).'Driver'; if (method_exists($this, $driverMethod)) {return $this->{$driverMethod}($config); } else {throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); }}}protected function getConfig($name){return $this->app['config']["cache.stores.{$name}"]; }protected function createFileDriver(array $config){return $this->repository(new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null)); }
// namespace \Illuminate\Cache\Repositorypublic function add($key, $value, $ttl = null){if ($ttl !== null) {if ($this->getSeconds($ttl) <= 0) {return false; }if (method_exists($this->store, 'add')) {$seconds = $this->getSeconds($ttl); return $this->store->add($this->itemKey($key), $value, $seconds); }}if (is_null($this->get($key))) {return $this->put($key, $value, $ttl); }return false; }public function get($key, $default = null){if (is_array($key)) {return $this->many($key); }$value = https://www.it610.com/article/$this->store->get($this->itemKey($key)); if (is_null($value)) {$this->event(new CacheMissed($key)); $value = https://www.it610.com/article/value($default); } else {$this->event(new CacheHit($key, $value)); }return $value; }// namespace \Illuminate\Cache\FileStorepublic function get($key){return $this->getPayload($key)['data'] ?? null; }protected function getPayload($key){$path = $this->path($key); try {$expire = substr($contents = $this->files->get($path, true), 0, 10); } catch (Exception $e) {return $this->emptyPayload(); }if ($this->currentTime() >= $expire) {$this->forget($key); return $this->emptyPayload(); }try {$data = https://www.it610.com/article/unserialize(substr($contents, 10)); } catch (Exception $e) {$this->forget($key); return $this->emptyPayload(); }$time = $expire - $this->currentTime(); return compact('data', 'time'); }
// namespace \Illuminate\Cache\Repositorypublic function put($key, $value, $ttl = null){/* ... ... */$seconds = $this->getSeconds($ttl); if ($seconds <= 0) {return $this->forget($key); }$result = $this->store->put($this->itemKey($key), $value, $seconds); if ($result) {$this->event(new KeyWritten($key, $value, $seconds)); }return $result; }// namespace \Illuminate\Cache\FileStorepublic function put($key, $value, $seconds){$this->ensureCacheDirectoryExists($path = $this->path($key)); $result = $this->files->put($path, $this->expiration($seconds).serialize($value), true); if ($result !== false && $result > 0) {$this->ensureFileHasCorrectPermissions($path); return true; }return false; }protected function path($key){$parts = array_slice(str_split($hash = sha1($key), 2), 0, 2); return $this->directory.'/'.implode('/', $parts).'/'.$hash; }// namespace \Illuminate\Console\Scheduling\Schedulepublic function mutexName(){return 'framework'.DIRECTORY_SEPARATOR.'schedule-'.sha1($this->expression.$this->command); }
// namespace \Illuminate\Console\Scheduling\ScheduleRunCommandpublic function handle(Schedule $schedule, Dispatcher $dispatcher){$this->schedule = $schedule; $this->dispatcher = $dispatcher; 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->info('No scheduled commands are ready to run.'); }}// namespace \Illuminate\Console\Scheduling\Schedulepublic function dueEvents($app){return collect($this->events)->filter->isDue($app); }// namespace \Illuminate\Console\Scheduling\Eventpublic function isDue($app){/* ... ... */return $this->expressionPasses() &&$this->runsInEnvironment($app->environment()); }protected function expressionPasses(){$date = Carbon::now(); /* ... ... */return CronExpression::factory($this->expression)->isDue($date->toDateTimeString()); }// namespace \Cron\CronExpressionpublic function isDue($currentTime = 'now', $timeZone = null){/* ... ... */try {return $this->getNextRunDate($currentTime, 0, true)->getTimestamp() === $currentTime->getTimestamp(); } catch (Exception $e) {return false; }}public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false, $timeZone = null){return $this->getRunDate($currentTime, $nth, false, $allowCurrentDate, $timeZone); }