|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jellyfish\Scheduler; |
|
6
|
|
|
|
|
7
|
|
|
use Jellyfish\Process\ProcessFactoryInterface; |
|
8
|
|
|
use Jellyfish\Scheduler\Command\RunSchedulerCommand; |
|
9
|
|
|
use Pimple\Container; |
|
10
|
|
|
use Pimple\ServiceProviderInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SchedulerServiceProvider implements ServiceProviderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param \Pimple\Container $pimple |
|
16
|
|
|
* |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function register(Container $pimple): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->registerScheduler($pimple) |
|
22
|
|
|
->registerCommands($pimple) |
|
23
|
|
|
->registerJobFactory($pimple); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \Pimple\Container $container |
|
28
|
|
|
* |
|
29
|
|
|
* @return \Jellyfish\Scheduler\SchedulerServiceProvider |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function registerScheduler(Container $container): SchedulerServiceProvider |
|
32
|
|
|
{ |
|
33
|
|
|
$container->offsetSet(SchedulerConstants::CONTAINER_KEY_SCHEDULER, function () { |
|
34
|
|
|
return new Scheduler(); |
|
35
|
|
|
}); |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \Pimple\Container $container |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Jellyfish\Scheduler\SchedulerServiceProvider |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function registerCommands(Container $container): SchedulerServiceProvider |
|
46
|
|
|
{ |
|
47
|
|
|
$container->extend('commands', static function (array $commands, Container $container) { |
|
48
|
|
|
$commands[] = new RunSchedulerCommand( |
|
49
|
|
|
$container->offsetGet(SchedulerConstants::CONTAINER_KEY_SCHEDULER), |
|
50
|
|
|
$container->offsetGet('lock_factory'), |
|
51
|
|
|
$container->offsetGet('logger') |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
return $commands; |
|
55
|
|
|
}); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Pimple\Container $container |
|
62
|
|
|
* |
|
63
|
|
|
* @return \Jellyfish\Scheduler\SchedulerServiceProvider |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function registerJobFactory(Container $container): SchedulerServiceProvider |
|
66
|
|
|
{ |
|
67
|
|
|
$self = $this; |
|
68
|
|
|
|
|
69
|
|
|
$container->offsetSet(SchedulerConstants::CONTAINER_KEY_JOB_FACTORY, static function (Container $container) use ($self) { |
|
70
|
|
|
$processFactory = $self->getProcessFactory($container); |
|
71
|
|
|
if ($processFactory === null) { |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
return new JobFactory($processFactory, $self->createCronExpressionFactory()); |
|
75
|
|
|
}); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \Pimple\Container $container |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Jellyfish\Process\ProcessFactoryInterface|null |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function getProcessFactory(Container $container): ?ProcessFactoryInterface |
|
86
|
|
|
{ |
|
87
|
|
|
if ($container->offsetExists('process_factory') === false) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $container->offsetGet('process_factory'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return \Jellyfish\Scheduler\CronExpressionFactoryInterface |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function createCronExpressionFactory(): CronExpressionFactoryInterface |
|
98
|
|
|
{ |
|
99
|
|
|
return new CronExpressionFactory(); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|