1
|
|
|
<?php namespace Comodojo\Extender\Task; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
4
|
|
|
use \Psr\Log\LoggerInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @package Comodojo Extender |
8
|
|
|
* @author Marco Giovinazzi <[email protected]> |
9
|
|
|
* @license MIT |
10
|
|
|
* |
11
|
|
|
* LICENSE: |
12
|
|
|
* |
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19
|
|
|
* THE SOFTWARE. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
class Locker { |
23
|
|
|
|
24
|
|
|
use ConfigurationTrait; |
25
|
|
|
use LoggerTrait; |
26
|
|
|
|
27
|
|
|
private $running_jobs = []; |
28
|
|
|
|
29
|
|
|
private $completed_jobs = []; |
30
|
|
|
|
31
|
|
|
private $queued_jobs = []; |
32
|
|
|
|
33
|
|
|
private $lock_file = 'extender.tasks.locker'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
public function __construct(Configuration $configuration, LoggerInterface $logger) { |
36
|
|
|
|
37
|
|
|
$this->setConfiguration($configuration); |
38
|
|
|
$this->setLogger($logger); |
39
|
|
|
|
40
|
|
|
$lock_file = $configuration->get('queue-file'); |
41
|
|
|
if ( $lock_file !== null ) $this->$lock_file = $lock_file; |
42
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function isQueued(Job $job) { |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->logger->debug('Adding job '.$job->name.' (uid '.$job->uid.') to queue'); |
48
|
|
|
|
49
|
|
|
$uid = $job->uid; |
50
|
|
|
|
51
|
|
|
$this->queued_jobs[$uid] = $job; |
52
|
|
|
|
53
|
|
|
$this->dump(); |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function isStarting($uid, $pid) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$job = $this->queued_jobs[$uid]; |
62
|
|
|
|
63
|
|
|
$this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') is starting with pid '.$pid); |
64
|
|
|
|
65
|
|
|
$job->pid = $pid; |
66
|
|
|
|
67
|
|
|
$job->start_timestamp = microtime(true); |
68
|
|
|
|
69
|
|
|
$this->running_jobs[$uid] = $job; |
70
|
|
|
|
71
|
|
|
unset($this->queued_jobs[$uid]); |
72
|
|
|
|
73
|
|
|
$this->dump(); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function isCompleted($uid, $success, $result, $wid = null) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
$job = $this->running_jobs[$uid]; |
82
|
|
|
|
83
|
|
|
$this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') completed with '.($success ? 'success' : 'error')); |
84
|
|
|
|
85
|
|
|
$job->success = $success; |
86
|
|
|
|
87
|
|
|
$job->result = $result; |
88
|
|
|
|
89
|
|
|
$job->wid = $wid; |
90
|
|
|
|
91
|
|
|
$job->end_timestamp = microtime(true); |
92
|
|
|
|
93
|
|
|
$this->completed_jobs[$uid] = $job; |
94
|
|
|
|
95
|
|
|
unset($this->running_jobs[$uid]); |
96
|
|
|
|
97
|
|
|
$this->dump(); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function isAborted($uid, $error) { |
|
|
|
|
104
|
|
|
|
105
|
|
|
$job = $this->running_jobs[$uid]; |
106
|
|
|
|
107
|
|
|
$this->logger->debug('Job '.$job->name.' (uid '.$job->uid.') aborted, reason: '.$error); |
108
|
|
|
|
109
|
|
|
$job->success = false; |
110
|
|
|
|
111
|
|
|
$job->result = $error; |
112
|
|
|
|
113
|
|
|
$job->wid = null; |
114
|
|
|
|
115
|
|
|
$job->end_timestamp = microtime(true); |
116
|
|
|
|
117
|
|
|
$this->completed_jobs[$uid] = $job; |
118
|
|
|
|
119
|
|
|
unset($this->queued_jobs[$uid]); |
120
|
|
|
|
121
|
|
|
$this->dump(); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function queued() { |
128
|
|
|
|
129
|
|
|
return $this->queued_jobs; |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function running() { |
134
|
|
|
|
135
|
|
|
return $this->running_jobs; |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function completed() { |
140
|
|
|
|
141
|
|
|
return $this->completed_jobs; |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function free() { |
146
|
|
|
|
147
|
|
|
$this->queued_jobs = array(); |
148
|
|
|
$this->running_jobs = array(); |
149
|
|
|
$this->completed_jobs = array(); |
150
|
|
|
|
151
|
|
|
$this->dump(); |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function release() { |
156
|
|
|
|
157
|
|
|
$lock = file_exists($this->queue_file) ? unlink($this->queue_file) : true; |
158
|
|
|
|
159
|
|
|
return $lock; |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
private function dump() { |
|
|
|
|
164
|
|
|
|
165
|
|
|
$data = array( |
166
|
|
|
'QUEUED' => count($this->queued_jobs), |
167
|
|
|
'RUNNING' => count($this->running_jobs), |
168
|
|
|
'COMPLETED' => count($this->completed_jobs) |
169
|
|
|
); |
170
|
|
|
|
171
|
|
|
$content = serialize($data); |
172
|
|
|
|
173
|
|
|
return file_put_contents($this->queue_file, $content); |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.