ScheduleRunCommand.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Illuminate\Console\Scheduling;
  3. use Illuminate\Support\Carbon;
  4. use Illuminate\Console\Command;
  5. class ScheduleRunCommand extends Command
  6. {
  7. /**
  8. * The console command name.
  9. *
  10. * @var string
  11. */
  12. protected $name = 'schedule:run';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Run the scheduled commands';
  19. /**
  20. * The schedule instance.
  21. *
  22. * @var \Illuminate\Console\Scheduling\Schedule
  23. */
  24. protected $schedule;
  25. /**
  26. * The 24 hour timestamp this scheduler command started running.
  27. *
  28. * @var \Illuminate\Support\Carbon;
  29. */
  30. protected $startedAt;
  31. /**
  32. * Check if any events ran.
  33. *
  34. * @var bool
  35. */
  36. protected $eventsRan = false;
  37. /**
  38. * Create a new command instance.
  39. *
  40. * @param \Illuminate\Console\Scheduling\Schedule $schedule
  41. * @return void
  42. */
  43. public function __construct(Schedule $schedule)
  44. {
  45. $this->schedule = $schedule;
  46. $this->startedAt = Carbon::now();
  47. parent::__construct();
  48. }
  49. /**
  50. * Execute the console command.
  51. *
  52. * @return void
  53. */
  54. public function handle()
  55. {
  56. foreach ($this->schedule->dueEvents($this->laravel) as $event) {
  57. if (! $event->filtersPass($this->laravel)) {
  58. continue;
  59. }
  60. if ($event->onOneServer) {
  61. $this->runSingleServerEvent($event);
  62. } else {
  63. $this->runEvent($event);
  64. }
  65. $this->eventsRan = true;
  66. }
  67. if (! $this->eventsRan) {
  68. $this->info('No scheduled commands are ready to run.');
  69. }
  70. }
  71. /**
  72. * Run the given single server event.
  73. *
  74. * @param \Illuminate\Console\Scheduling\Event $event
  75. * @return void
  76. */
  77. protected function runSingleServerEvent($event)
  78. {
  79. if ($this->schedule->serverShouldRun($event, $this->startedAt)) {
  80. $this->runEvent($event);
  81. } else {
  82. $this->line('<info>Skipping command (has already run on another server):</info> '.$event->getSummaryForDisplay());
  83. }
  84. }
  85. /**
  86. * Run the given event.
  87. *
  88. * @param \Illuminate\Console\Scheduling\Event $event
  89. * @return void
  90. */
  91. protected function runEvent($event)
  92. {
  93. $this->line('<info>Running scheduled command:</info> '.$event->getSummaryForDisplay());
  94. $event->run($this->laravel);
  95. $this->eventsRan = true;
  96. }
  97. }