|
1
|
|
|
<?php namespace Comodojo\Extender\Workers; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Daemon\Worker\AbstractWorker; |
|
4
|
|
|
use \Comodojo\Extender\Task\Manager as TaskManager; |
|
5
|
|
|
use \Comodojo\Extender\Schedule\Manager as ScheduleManager; |
|
6
|
|
|
use \Comodojo\Extender\Task\Request; |
|
7
|
|
|
use \Comodojo\Daemon\Traits\LoggerTrait; |
|
8
|
|
|
use \Comodojo\Daemon\Traits\EventsTrait; |
|
9
|
|
|
use \Comodojo\Extender\Traits\ConfigurationTrait; |
|
10
|
|
|
use \Comodojo\Extender\Traits\TasksTableTrait; |
|
11
|
|
|
use \Comodojo\Extender\Traits\EntityManagerTrait; |
|
12
|
|
|
use \Comodojo\Extender\Traits\WorkerTrait; |
|
13
|
|
|
|
|
14
|
|
|
class ScheduleWorker extends AbstractWorker { |
|
15
|
|
|
|
|
16
|
|
|
use ConfigurationTrait; |
|
17
|
|
|
use LoggerTrait; |
|
18
|
|
|
use EventsTrait; |
|
19
|
|
|
use TasksTableTrait; |
|
20
|
|
|
use EntityManagerTrait; |
|
21
|
|
|
use WorkerTrait; |
|
22
|
|
|
|
|
23
|
|
|
protected $wakeup_time = 0; |
|
24
|
|
|
|
|
25
|
|
View Code Duplication |
public function spinup() { |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$this->task_manager = new TaskManager( |
|
28
|
|
|
'schedule.worker', |
|
29
|
|
|
$this->getConfiguration(), |
|
30
|
|
|
$this->getLogger(), |
|
31
|
|
|
$this->getTasksTable(), |
|
32
|
|
|
$this->getEvents(), |
|
33
|
|
|
$this->getEntityManager() |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$this->getEvents()->subscribe('daemon.worker.refresh', '\Comodojo\Extender\Listeners\RefreshScheduler'); |
|
37
|
|
|
|
|
38
|
|
|
$this->job_manager = new ScheduleManager( |
|
39
|
|
|
$this->getConfiguration(), |
|
40
|
|
|
$this->getLogger(), |
|
41
|
|
|
$this->getEvents(), |
|
42
|
|
|
$this->getEntityManager() |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function loop() { |
|
48
|
|
|
|
|
49
|
|
|
if ( $this->wakeup_time > time() ) { |
|
50
|
|
|
$this->logger->info('Still in sleep time, sorry'); |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$jobs = $this->job_manager->getJobs(true); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if ( empty($jobs) ) { |
|
57
|
|
|
|
|
58
|
|
|
$this->logger->debug('Nothing to do right now, sleeping... zzZZzZzZzz'); |
|
59
|
|
|
|
|
60
|
|
|
} else { |
|
61
|
|
|
|
|
62
|
|
|
$this->logger->debug(count($jobs)." jobs will be executed"); |
|
63
|
|
|
|
|
64
|
|
|
$requests = $this->jobsToRequests($jobs); |
|
65
|
|
|
|
|
66
|
|
|
$this->task_manager->addBulk($requests); |
|
67
|
|
|
|
|
68
|
|
|
$result = $this->task_manager->run(); |
|
69
|
|
|
|
|
70
|
|
|
$this->job_manager->updateSchedules($result); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->wakeup_time = $this->job_manager->getNextCycleTimestamp(); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function spindown() { |
|
79
|
|
|
|
|
80
|
|
|
unset($this->task_manager); |
|
81
|
|
|
unset($this->job_manager); |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function refreshPlans() { |
|
86
|
|
|
|
|
87
|
|
|
$this->wakeup_time = 0; |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.