1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Traits; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Swoole\Timer\CronJob; |
6
|
|
|
|
7
|
|
|
trait TimerTrait |
8
|
|
|
{ |
9
|
|
|
use ProcessTitleTrait; |
10
|
|
|
use LaravelTrait; |
11
|
|
|
use LogTrait; |
12
|
|
|
|
13
|
|
|
public function addTimerProcess(\swoole_server $swoole, array $config, array $laravelConfig) |
14
|
|
|
{ |
15
|
|
|
if (empty($config['enable']) || empty($config['jobs'])) { |
16
|
|
|
return; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$startTimer = function () use ($swoole, $config, $laravelConfig) { |
20
|
|
|
// Inject the global variables |
21
|
|
|
$_SERVER = $laravelConfig['_SERVER']; |
22
|
|
|
$_ENV = $laravelConfig['_ENV']; |
23
|
|
|
|
24
|
|
|
$this->setProcessTitle(sprintf('%s laravels: timer process', $config['process_prefix'])); |
25
|
|
|
$this->initLaravel($laravelConfig, $swoole); |
26
|
|
|
foreach ($config['jobs'] as $jobClass) { |
27
|
|
|
if (is_array($jobClass) && isset($jobClass[0])) { |
28
|
|
|
$job = new $jobClass[0](isset($jobClass[1]) ? $jobClass[1] : []); |
29
|
|
|
} else { |
30
|
|
|
$job = new $jobClass(); |
31
|
|
|
} |
32
|
|
|
if (!($job instanceof CronJob)) { |
33
|
|
|
throw new \Exception(sprintf( |
34
|
|
|
'%s must extend the abstract class %s', |
35
|
|
|
get_class($job), |
36
|
|
|
CronJob::class |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
if (empty($job->interval())) { |
41
|
|
|
throw new \Exception(sprintf('The interval of %s cannot be empty', get_class($job))); |
42
|
|
|
} |
43
|
|
|
$timerId = swoole_timer_tick($job->interval(), function () use ($job) { |
44
|
|
|
$this->callWithCatchException(function () use ($job) { |
45
|
|
|
$job->run(); |
46
|
|
|
}); |
47
|
|
|
}); |
48
|
|
|
$job->setTimerId($timerId); |
49
|
|
|
if ($job->isImmediate()) { |
50
|
|
|
swoole_timer_after(1, function () use ($job) { |
51
|
|
|
$this->callWithCatchException(function () use ($job) { |
52
|
|
|
$job->run(); |
53
|
|
|
}); |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
}; |
58
|
|
|
|
59
|
|
|
$timerProcess = new \swoole_process($startTimer, false, false); |
60
|
|
|
if ($swoole->addProcess($timerProcess)) { |
|
|
|
|
61
|
|
|
return $timerProcess; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.