Scheduling.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Encore\Admin\Scheduling;
  3. use Encore\Admin\Admin;
  4. use Encore\Admin\Extension;
  5. use Illuminate\Console\Scheduling\CallbackEvent;
  6. use Illuminate\Support\Str;
  7. class Scheduling extends Extension
  8. {
  9. /**
  10. * @var string out put file for command.
  11. */
  12. protected $sendOutputTo;
  13. /**
  14. * Get all events in console kernel.
  15. *
  16. * @return array
  17. */
  18. protected function getKernelEvents()
  19. {
  20. app()->make('Illuminate\Contracts\Console\Kernel');
  21. return app()->make('Illuminate\Console\Scheduling\Schedule')->events();
  22. }
  23. /**
  24. * Get all formatted tasks.
  25. *
  26. * @throws \Exception
  27. *
  28. * @return array
  29. */
  30. public function getTasks()
  31. {
  32. $tasks = [];
  33. foreach ($this->getKernelEvents() as $event) {
  34. $tasks[] = [
  35. 'task' => $this->formatTask($event),
  36. 'expression' => $event->expression,
  37. 'nextRunDate' => $event->nextRunDate()->format('Y-m-d H:i:s'),
  38. 'description' => $event->description,
  39. 'readable' => CronSchedule::fromCronString($event->expression)->asNaturalLanguage(),
  40. ];
  41. }
  42. return $tasks;
  43. }
  44. /**
  45. * Format a giving task.
  46. *
  47. * @param $event
  48. *
  49. * @return array
  50. */
  51. protected function formatTask($event)
  52. {
  53. if ($event instanceof CallbackEvent) {
  54. return [
  55. 'type' => 'closure',
  56. 'name' => 'Closure',
  57. ];
  58. }
  59. if (Str::contains($event->command, '\'artisan\'')) {
  60. $exploded = explode(' ', $event->command);
  61. return [
  62. 'type' => 'artisan',
  63. 'name' => 'artisan '.implode(' ', array_slice($exploded, 2)),
  64. ];
  65. }
  66. return [
  67. 'type' => 'command',
  68. 'name' => $event->command,
  69. ];
  70. }
  71. /**
  72. * Run specific task.
  73. *
  74. * @param int $id
  75. *
  76. * @return string
  77. */
  78. public function runTask($id)
  79. {
  80. set_time_limit(0);
  81. /** @var \Illuminate\Console\Scheduling\Event $event */
  82. $event = $this->getKernelEvents()[$id - 1];
  83. $event->sendOutputTo($this->getOutputTo());
  84. $event->run(app());
  85. return $this->readOutput();
  86. }
  87. /**
  88. * @return string
  89. */
  90. protected function getOutputTo()
  91. {
  92. if (!$this->sendOutputTo) {
  93. $this->sendOutputTo = storage_path('app/task-schedule.output');
  94. }
  95. return $this->sendOutputTo;
  96. }
  97. /**
  98. * Read output info from output file.
  99. *
  100. * @return string
  101. */
  102. protected function readOutput()
  103. {
  104. return file_get_contents($this->getOutputTo());
  105. }
  106. /**
  107. * Bootstrap this package.
  108. *
  109. * @return void
  110. */
  111. public static function boot()
  112. {
  113. static::registerRoutes();
  114. Admin::extend('scheduling', __CLASS__);
  115. }
  116. /**
  117. * Register routes for laravel-admin.
  118. *
  119. * @return void
  120. */
  121. protected static function registerRoutes()
  122. {
  123. parent::routes(function ($router) {
  124. /* @var \Illuminate\Routing\Router $router */
  125. $router->get('scheduling', 'Encore\Admin\Scheduling\SchedulingController@index')->name('scheduling-index');
  126. $router->post('scheduling/run', 'Encore\Admin\Scheduling\SchedulingController@runEvent')->name('scheduling-run');
  127. });
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public static function import()
  133. {
  134. parent::createMenu('Scheduling', 'scheduling', 'fa-clock-o');
  135. parent::createPermission('Scheduling', 'ext.scheduling', 'scheduling*');
  136. }
  137. }