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\Task\Locker; |
6
|
|
|
use \Comodojo\Extender\Schedule\Manager as ScheduleManager; |
7
|
|
|
use \Comodojo\Extender\Schedule\Updater as ScheduleUpdater; |
8
|
|
|
use \Comodojo\Foundation\Base\ConfigurationTrait; |
9
|
|
|
use \Comodojo\Extender\Traits\TasksTableTrait; |
10
|
|
|
use \Comodojo\Extender\Traits\WorkerTrait; |
11
|
|
|
use \Comodojo\Foundation\Logging\LoggerTrait; |
12
|
|
|
use \Comodojo\Foundation\Events\EventsTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @package Comodojo Extender |
16
|
|
|
* @author Marco Giovinazzi <[email protected]> |
17
|
|
|
* @license MIT |
18
|
|
|
* |
19
|
|
|
* LICENSE: |
20
|
|
|
* |
21
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
22
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
23
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
24
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
25
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
26
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
27
|
|
|
* THE SOFTWARE. |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
class ScheduleWorker extends AbstractWorker { |
31
|
|
|
|
32
|
|
|
use ConfigurationTrait; |
33
|
|
|
use LoggerTrait; |
34
|
|
|
use EventsTrait; |
35
|
|
|
use TasksTableTrait; |
36
|
|
|
use WorkerTrait; |
37
|
|
|
|
38
|
|
|
protected $locker; |
39
|
|
|
|
40
|
|
|
protected $wakeup_time = 0; |
41
|
|
|
|
42
|
|
|
public function spinup() { |
43
|
|
|
|
44
|
|
|
$configuration = $this->getConfiguration(); |
45
|
|
|
|
46
|
|
|
$base_path = $configuration->get('base-path'); |
47
|
|
|
$lock_path = $configuration->get('run-path'); |
48
|
|
|
$lock_file = "$base_path/$lock_path/schedule.worker.lock"; |
49
|
|
|
|
50
|
|
|
$this->locker = new Locker($lock_file); |
51
|
|
|
$this->locker->lock([]); |
52
|
|
|
|
53
|
|
|
$this->getEvents()->subscribe('daemon.worker.refresh', '\Comodojo\Extender\Listeners\RefreshScheduler'); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function loop() { |
58
|
|
|
|
59
|
|
|
if ( $this->wakeup_time > time() ) { |
60
|
|
|
// $this->logger->debug('Still in sleep time, next planned wakeup is '.date('r', $this->wakeup_time)); |
|
|
|
|
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$schedule_manager = new ScheduleManager( |
65
|
|
|
$this->getConfiguration(), |
66
|
|
|
$this->getLogger(), |
67
|
|
|
$this->getEvents() |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$jobs = $schedule_manager->getAll(true); |
71
|
|
|
unset($schedule_manager); |
72
|
|
|
|
73
|
|
|
$results = []; |
74
|
|
|
|
75
|
|
|
// if ( empty($jobs) ) { |
|
|
|
|
76
|
|
|
// |
77
|
|
|
// $this->logger->debug('Nothing to do right now, sleeping... zzZZzZzZzz'); |
|
|
|
|
78
|
|
|
// |
79
|
|
|
// } else { |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ( !empty($jobs) ) { |
82
|
|
|
|
83
|
|
|
$this->logger->debug(count($jobs)." jobs will be executed"); |
84
|
|
|
$requests = $this->jobsToRequests($jobs, true); |
85
|
|
|
|
86
|
|
|
$task_manager = new TaskManager( |
87
|
|
|
$this->locker, |
88
|
|
|
$this->getConfiguration(), |
89
|
|
|
$this->getLogger(), |
90
|
|
|
$this->getTasksTable(), |
91
|
|
|
$this->getEvents() |
92
|
|
|
); |
93
|
|
|
$results = $task_manager->addBulk($requests)->run(); |
94
|
|
|
unset($task_manager); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$schedule_updater = new ScheduleUpdater( |
99
|
|
|
$this->getConfiguration(), |
100
|
|
|
$this->getLogger(), |
101
|
|
|
$this->getEvents() |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$wut = $schedule_updater->updateFromResults($results); |
105
|
|
|
unset($schedule_updater); |
106
|
|
|
|
107
|
|
|
$this->wakeup_time = $wut; |
108
|
|
|
if ( $this->wakeup_time !== 0 ) { |
109
|
|
|
$this->logger->debug('Sleep time! Next planned wakeup is at '.date('r', $this->wakeup_time)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->locker->lock([]); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function spindown() { |
117
|
|
|
|
118
|
|
|
$this->locker->release(); |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function refreshPlans() { |
123
|
|
|
|
124
|
|
|
$this->wakeup_time = 0; |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.