|
1
|
|
|
<?php namespace Indatus\Dispatcher\Services; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Dispatcher |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Ben Kuhl <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
use App; |
|
13
|
|
|
use Illuminate\Contracts\Console\Kernel; |
|
14
|
|
|
use Indatus\Dispatcher\Debugger; |
|
15
|
|
|
use Indatus\Dispatcher\Scheduling\Schedulable; |
|
16
|
|
|
use Indatus\Dispatcher\Scheduling\ScheduledCommandInterface; |
|
17
|
|
|
|
|
18
|
|
|
abstract class ScheduleService |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Kernel */ |
|
21
|
|
|
protected $console; |
|
22
|
|
|
|
|
23
|
11 |
|
public function __construct(Kernel $console) |
|
24
|
|
|
{ |
|
25
|
11 |
|
$this->console = $console; |
|
26
|
11 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Determine if a command is due to be run |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Indatus\Dispatcher\Scheduling\Schedulable $scheduler |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract public function isDue(Schedulable $scheduler); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get all commands that are scheduled |
|
39
|
|
|
* |
|
40
|
|
|
* @return \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface[] |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function getScheduledCommands() |
|
43
|
|
|
{ |
|
44
|
1 |
|
$scheduledCommands = []; |
|
45
|
1 |
|
foreach ($this->console->all() as $command) { |
|
46
|
1 |
|
if ($command instanceof ScheduledCommandInterface) { |
|
47
|
1 |
|
$scheduledCommands[] = $command; |
|
48
|
1 |
|
} |
|
49
|
1 |
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
return $scheduledCommands; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get all commands that are due to be run |
|
56
|
|
|
* |
|
57
|
|
|
* @param Debugger $debugger |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \InvalidArgumentException |
|
60
|
|
|
* @return \Indatus\Dispatcher\Queue |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getQueue(Debugger $debugger) |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var \Indatus\Dispatcher\Queue $queue */ |
|
65
|
3 |
|
$queue = App::make('Indatus\Dispatcher\Queue'); |
|
66
|
|
|
|
|
67
|
|
|
/** @var \Indatus\Dispatcher\Scheduling\ScheduledCommandInterface $command */ |
|
68
|
3 |
|
foreach ($this->getScheduledCommands() as $command) { |
|
69
|
|
|
/** @var \Indatus\Dispatcher\Scheduling\Schedulable $scheduler */ |
|
70
|
3 |
|
$scheduler = App::make('Indatus\Dispatcher\Scheduling\Schedulable'); |
|
71
|
|
|
|
|
72
|
|
|
//could be multiple schedules based on arguments |
|
73
|
3 |
|
$schedules = $command->schedule($scheduler); |
|
74
|
3 |
|
if (!is_array($schedules)) { |
|
75
|
2 |
|
$schedules = [$schedules]; |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
$willBeRun = false; |
|
79
|
3 |
|
foreach ($schedules as $schedule) { |
|
80
|
3 |
|
if (($schedule instanceof Schedulable) === false) { |
|
81
|
1 |
|
$msg = 'Schedule for "'.$command->getName().'" is not an instance of Schedulable'; |
|
82
|
1 |
|
throw new \InvalidArgumentException($msg); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
if ($this->isDue($schedule)) { |
|
86
|
|
|
/** @var \Indatus\Dispatcher\QueueItem $queueItem */ |
|
87
|
1 |
|
$queueItem = App::make('Indatus\Dispatcher\QueueItem'); |
|
88
|
|
|
|
|
89
|
1 |
|
$queueItem->setCommand($command); |
|
90
|
1 |
|
$queueItem->setScheduler($schedule); |
|
91
|
|
|
|
|
92
|
1 |
|
if ($queue->add($queueItem)) { |
|
93
|
1 |
|
$willBeRun = true; |
|
94
|
1 |
|
} |
|
95
|
1 |
|
} |
|
96
|
2 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
//it didn't run, so record that it didn't run |
|
99
|
2 |
|
if ($willBeRun === false) { |
|
100
|
1 |
|
$debugger->commandNotRun($command, 'No schedules were due'); |
|
101
|
1 |
|
} |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
return $queue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Review scheduled commands schedule, active status, etc. |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
|
|
abstract public function printSummary(); |
|
112
|
|
|
} |
|
113
|
|
|
|