Schedule.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Illuminate\Console\Scheduling;
  3. use DateTimeInterface;
  4. use Illuminate\Console\Application;
  5. use Illuminate\Container\Container;
  6. use Illuminate\Support\ProcessUtils;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. class Schedule
  9. {
  10. /**
  11. * All of the events on the schedule.
  12. *
  13. * @var \Illuminate\Console\Scheduling\Event[]
  14. */
  15. protected $events = [];
  16. /**
  17. * The event mutex implementation.
  18. *
  19. * @var \Illuminate\Console\Scheduling\EventMutex
  20. */
  21. protected $eventMutex;
  22. /**
  23. * The scheduling mutex implementation.
  24. *
  25. * @var \Illuminate\Console\Scheduling\SchedulingMutex
  26. */
  27. protected $schedulingMutex;
  28. /**
  29. * Create a new schedule instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $container = Container::getInstance();
  36. $this->eventMutex = $container->bound(EventMutex::class)
  37. ? $container->make(EventMutex::class)
  38. : $container->make(CacheEventMutex::class);
  39. $this->schedulingMutex = $container->bound(SchedulingMutex::class)
  40. ? $container->make(SchedulingMutex::class)
  41. : $container->make(CacheSchedulingMutex::class);
  42. }
  43. /**
  44. * Add a new callback event to the schedule.
  45. *
  46. * @param string|callable $callback
  47. * @param array $parameters
  48. * @return \Illuminate\Console\Scheduling\CallbackEvent
  49. */
  50. public function call($callback, array $parameters = [])
  51. {
  52. $this->events[] = $event = new CallbackEvent(
  53. $this->eventMutex, $callback, $parameters
  54. );
  55. return $event;
  56. }
  57. /**
  58. * Add a new Artisan command event to the schedule.
  59. *
  60. * @param string $command
  61. * @param array $parameters
  62. * @return \Illuminate\Console\Scheduling\Event
  63. */
  64. public function command($command, array $parameters = [])
  65. {
  66. if (class_exists($command)) {
  67. $command = Container::getInstance()->make($command)->getName();
  68. }
  69. return $this->exec(
  70. Application::formatCommandString($command), $parameters
  71. );
  72. }
  73. /**
  74. * Add a new job callback event to the schedule.
  75. *
  76. * @param object|string $job
  77. * @param string|null $queue
  78. * @return \Illuminate\Console\Scheduling\CallbackEvent
  79. */
  80. public function job($job, $queue = null)
  81. {
  82. return $this->call(function () use ($job, $queue) {
  83. $job = is_string($job) ? resolve($job) : $job;
  84. if ($job instanceof ShouldQueue) {
  85. dispatch($job)->onQueue($queue);
  86. } else {
  87. dispatch_now($job);
  88. }
  89. })->name(is_string($job) ? $job : get_class($job));
  90. }
  91. /**
  92. * Add a new command event to the schedule.
  93. *
  94. * @param string $command
  95. * @param array $parameters
  96. * @return \Illuminate\Console\Scheduling\Event
  97. */
  98. public function exec($command, array $parameters = [])
  99. {
  100. if (count($parameters)) {
  101. $command .= ' '.$this->compileParameters($parameters);
  102. }
  103. $this->events[] = $event = new Event($this->eventMutex, $command);
  104. return $event;
  105. }
  106. /**
  107. * Compile parameters for a command.
  108. *
  109. * @param array $parameters
  110. * @return string
  111. */
  112. protected function compileParameters(array $parameters)
  113. {
  114. return collect($parameters)->map(function ($value, $key) {
  115. if (is_array($value)) {
  116. $value = collect($value)->map(function ($value) {
  117. return ProcessUtils::escapeArgument($value);
  118. })->implode(' ');
  119. } elseif (! is_numeric($value) && ! preg_match('/^(-.$|--.*)/i', $value)) {
  120. $value = ProcessUtils::escapeArgument($value);
  121. }
  122. return is_numeric($key) ? $value : "{$key}={$value}";
  123. })->implode(' ');
  124. }
  125. /**
  126. * Determine if the server is allowed to run this event.
  127. *
  128. * @param \Illuminate\Console\Scheduling\Event $event
  129. * @param \DateTimeInterface $time
  130. * @return bool
  131. */
  132. public function serverShouldRun(Event $event, DateTimeInterface $time)
  133. {
  134. return $this->schedulingMutex->create($event, $time);
  135. }
  136. /**
  137. * Get all of the events on the schedule that are due.
  138. *
  139. * @param \Illuminate\Contracts\Foundation\Application $app
  140. * @return \Illuminate\Support\Collection
  141. */
  142. public function dueEvents($app)
  143. {
  144. return collect($this->events)->filter->isDue($app);
  145. }
  146. /**
  147. * Get all of the events on the schedule.
  148. *
  149. * @return \Illuminate\Console\Scheduling\Event[]
  150. */
  151. public function events()
  152. {
  153. return $this->events;
  154. }
  155. /**
  156. * Specify the cache store that should be used to store mutexes.
  157. *
  158. * @param string $store
  159. * @return $this
  160. */
  161. public function useCache($store)
  162. {
  163. if ($this->eventMutex instanceof CacheEventMutex) {
  164. $this->eventMutex->useStore($store);
  165. }
  166. if ($this->schedulingMutex instanceof CacheSchedulingMutex) {
  167. $this->schedulingMutex->useStore($store);
  168. }
  169. return $this;
  170. }
  171. }