Repository navigation
Expand file tree
/
Copy pathExecutor.php
More file actions
212 lines (186 loc) · 7.61 KB
/
Copy pathExecutor.php
File metadata and controls
212 lines (186 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Crontab\Strategy;
use Carbon\Carbon;
use Closure;
use Hyperf\Contract\ApplicationInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Coordinator\Timer;
use Hyperf\Crontab\Crontab;
use Hyperf\Crontab\Event\AfterExecute;
use Hyperf\Crontab\Event\BeforeExecute;
use Hyperf\Crontab\Event\FailToExecute;
use Hyperf\Crontab\Exception\InvalidArgumentException;
use Hyperf\Crontab\LoggerInterface;
use Hyperf\Crontab\Mutex\ServerMutex;
use Hyperf\Crontab\Mutex\TaskMutex;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface as PsrLoggerInterface;
use RuntimeException;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Throwable;
use function Hyperf\Support\make;
class Executor
{
protected ?PsrLoggerInterface $logger = null;
protected ?TaskMutex $taskMutex = null;
protected ?ServerMutex $serverMutex = null;
protected ?EventDispatcherInterface $dispatcher = null;
protected Timer $timer;
public function __construct(protected ContainerInterface $container)
{
$this->logger = match (true) {
$container->has(LoggerInterface::class) => $container->get(LoggerInterface::class),
$container->has(StdoutLoggerInterface::class) => $container->get(StdoutLoggerInterface::class),
default => null,
};
if ($container->has(EventDispatcherInterface::class)) {
$this->dispatcher = $container->get(EventDispatcherInterface::class);
}
$this->timer = new Timer($this->logger);
}
public function execute(Crontab $crontab)
{
try {
$diff = Carbon::now()->diffInRealSeconds($crontab->getExecuteTime(), false);
$runnable = null;
switch ($crontab->getType()) {
case 'closure':
$runnable = $crontab->getCallback();
break;
case 'callback':
[$class, $method] = $crontab->getCallback();
$parameters = $crontab->getCallback()[2] ?? null;
if ($class && $method && class_exists($class) && method_exists($class, $method)) {
$runnable = function () use ($class, $method, $parameters) {
$instance = make($class);
if ($parameters && is_array($parameters)) {
$instance->{$method}(...$parameters);
} else {
$instance->{$method}();
}
};
}
break;
case 'command':
$input = make(ArrayInput::class, [$crontab->getCallback()]);
$output = make(NullOutput::class);
/** @var Application */
$application = $this->container->get(ApplicationInterface::class);
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$runnable = function () use ($application, $input, $output) {
if ($application->run($input, $output) !== 0) {
throw new RuntimeException('Crontab task failed to execute.');
}
};
break;
case 'eval':
$runnable = fn () => eval($crontab->getCallback());
break;
default:
throw new InvalidArgumentException(sprintf('Crontab task type [%s] is invalid.', $crontab->getType()));
}
$runnable = function ($isClosing) use ($crontab, $runnable) {
if ($isClosing) {
$crontab->close();
$this->logResult($crontab, false);
return;
}
try {
$runnable = $this->catchToExecute($crontab, $runnable);
$this->decorateRunnable($crontab, $runnable)();
} finally {
$crontab->complete();
}
};
$this->timer->after(max($diff, 0), $runnable);
} catch (Throwable $exception) {
$crontab->close();
throw $exception;
}
}
protected function runInSingleton(Crontab $crontab, Closure $runnable): Closure
{
return function () use ($crontab, $runnable) {
$taskMutex = $this->getTaskMutex();
if ($taskMutex->exists($crontab) || ! $taskMutex->create($crontab)) {
$this->logger?->info(sprintf('Crontab task [%s] skipped execution at %s caused by task mutex.', $crontab->getName(), date('Y-m-d H:i:s')));
return;
}
try {
$runnable();
} finally {
$taskMutex->remove($crontab);
}
};
}
protected function getTaskMutex(): TaskMutex
{
return $this->taskMutex ??= $this->container->get(TaskMutex::class);
}
protected function runOnOneServer(Crontab $crontab, Closure $runnable): Closure
{
return function () use ($crontab, $runnable) {
$taskMutex = $this->getServerMutex();
if (! $taskMutex->attempt($crontab)) {
$this->logger?->info(sprintf('Crontab task [%s] skipped execution at %s caused by server mutex.', $crontab->getName(), date('Y-m-d H:i:s')));
return;
}
$runnable();
};
}
protected function getServerMutex(): ServerMutex
{
return $this->serverMutex ??= $this->container->get(ServerMutex::class);
}
protected function decorateRunnable(Crontab $crontab, Closure $runnable): Closure
{
if ($crontab->isSingleton()) {
$runnable = $this->runInSingleton($crontab, $runnable);
}
if ($crontab->isOnOneServer()) {
$runnable = $this->runOnOneServer($crontab, $runnable);
}
return $runnable;
}
protected function catchToExecute(Crontab $crontab, ?Closure $runnable): Closure
{
return function () use ($crontab, $runnable) {
try {
$this->dispatcher?->dispatch(new BeforeExecute($crontab));
$result = true;
if (! $runnable) {
throw new InvalidArgumentException('The crontab task is invalid.');
}
$runnable();
$this->dispatcher?->dispatch(new AfterExecute($crontab));
} catch (Throwable $throwable) {
$result = false;
$this->dispatcher?->dispatch(new FailToExecute($crontab, $throwable));
} finally {
$this->logResult($crontab, $result, $throwable ?? null);
}
};
}
protected function logResult(Crontab $crontab, bool $isSuccess, ?Throwable $throwable = null)
{
if ($isSuccess) {
$this->logger?->info(sprintf('Crontab task [%s] executed successfully at %s.', $crontab->getName(), date('Y-m-d H:i:s')));
} else {
$this->logger?->error(sprintf('Crontab task [%s] failed execution at %s.', $crontab->getName(), date('Y-m-d H:i:s')));
$throwable && $this->logger?->error((string) $throwable);
}
}
}