|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* sysPass |
|
4
|
|
|
* |
|
5
|
|
|
* @author nuxsmin |
|
6
|
|
|
* @link https://syspass.org |
|
7
|
|
|
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of sysPass. |
|
10
|
|
|
* |
|
11
|
|
|
* sysPass is free software: you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* sysPass is distributed in the hope that it will be useful, |
|
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19
|
|
|
* GNU General Public License for more details. |
|
20
|
|
|
* |
|
21
|
|
|
* You should have received a copy of the GNU General Public License |
|
22
|
|
|
* along with sysPass. If not, see <http://www.gnu.org/licenses/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace SP\Services\Task; |
|
26
|
|
|
|
|
27
|
|
|
use SP\Services\Service; |
|
28
|
|
|
use SP\Services\ServiceException; |
|
29
|
|
|
use SP\Storage\File\FileException; |
|
30
|
|
|
use SP\Storage\File\FileHandler; |
|
31
|
|
|
use SP\Util\Util; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class TaskService |
|
35
|
|
|
* |
|
36
|
|
|
* @package SP\Services |
|
37
|
|
|
*/ |
|
38
|
|
|
final class TaskService extends Service |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* Time for waiting to initialization |
|
42
|
|
|
*/ |
|
43
|
|
|
const STARTUP_WAIT_TIME = 5; |
|
44
|
|
|
/** |
|
45
|
|
|
* Initialization attempts |
|
46
|
|
|
*/ |
|
47
|
|
|
const STARTUP_WAIT_COUNT = 30; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var \Closure |
|
50
|
|
|
*/ |
|
51
|
|
|
private $messagePusher; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var Task |
|
54
|
|
|
*/ |
|
55
|
|
|
private $task; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var string |
|
58
|
|
|
*/ |
|
59
|
|
|
private $taskDirectory; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $taskId; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var FileHandler |
|
66
|
|
|
*/ |
|
67
|
|
|
private $taskFile; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Track task status |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $taskId |
|
73
|
|
|
* @param \Closure $messagePusher |
|
74
|
|
|
* |
|
75
|
|
|
* @throws ServiceException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function trackStatus($taskId, \Closure $messagePusher) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->taskId = $taskId; |
|
80
|
|
|
$this->taskDirectory = Util::getTempDir(); |
|
|
|
|
|
|
81
|
|
|
$this->messagePusher = $messagePusher; |
|
82
|
|
|
|
|
83
|
|
|
if ($this->taskDirectory === false || !$this->getLock()) { |
|
84
|
|
|
throw new ServiceException(__('Unable to create the lock file')); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$count = 0; |
|
88
|
|
|
|
|
89
|
|
|
while (!$this->checkTaskRegistered() |
|
90
|
|
|
|| !file_exists($this->task->getFileOut()->getFile()) |
|
91
|
|
|
) { |
|
92
|
|
|
if ($count >= self::STARTUP_WAIT_COUNT) { |
|
93
|
|
|
throw new ServiceException(__('Task not set within wait time')); |
|
94
|
|
|
} else { |
|
95
|
|
|
logger(sprintf( |
|
96
|
|
|
'Waiting for task "%s" (%ds) ...', |
|
97
|
|
|
$taskId, |
|
98
|
|
|
(self::STARTUP_WAIT_COUNT - $count) * self::STARTUP_WAIT_TIME |
|
99
|
|
|
)); |
|
100
|
|
|
|
|
101
|
|
|
$count++; |
|
102
|
|
|
sleep(self::STARTUP_WAIT_TIME); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->readTaskStatus(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get a lock for task execution |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getLock() |
|
115
|
|
|
{ |
|
116
|
|
|
$lockFile = new FileHandler($this->taskDirectory . DIRECTORY_SEPARATOR . $this->taskId . '.lock'); |
|
117
|
|
|
|
|
118
|
|
|
try { |
|
119
|
|
|
if ($lockFile->getFileTime() + (self::STARTUP_WAIT_COUNT * self::STARTUP_WAIT_TIME) < time()) { |
|
120
|
|
|
$lockFile->delete(); |
|
121
|
|
|
} |
|
122
|
|
|
} catch (FileException $e) { |
|
123
|
|
|
processException($e); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
try { |
|
127
|
|
|
$lockFile->write(time()); |
|
128
|
|
|
|
|
129
|
|
|
return true; |
|
130
|
|
|
} catch (FileException $e) { |
|
131
|
|
|
processException($e); |
|
132
|
|
|
|
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Check whether the task's file has been registered |
|
139
|
|
|
* |
|
140
|
|
|
* @return bool |
|
141
|
|
|
*/ |
|
142
|
|
|
private function checkTaskRegistered() |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_object($this->task)) { |
|
145
|
|
|
logger('Task detected: ' . $this->task->getTaskId()); |
|
146
|
|
|
|
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
try { |
|
151
|
|
|
$this->taskFile = new FileHandler($this->taskDirectory . DIRECTORY_SEPARATOR . $this->taskId . '.task'); |
|
152
|
|
|
$this->taskFile->checkFileExists(); |
|
153
|
|
|
$this->task = unserialize($this->taskFile->readToString()); |
|
154
|
|
|
|
|
155
|
|
|
return is_object($this->task); |
|
156
|
|
|
} catch (FileException $e) { |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Read a task status and send it back to the browser (messagePusher) |
|
163
|
|
|
*/ |
|
164
|
|
|
private function readTaskStatus() |
|
165
|
|
|
{ |
|
166
|
|
|
logger('Tracking task status: ' . $this->task->getTaskId()); |
|
167
|
|
|
|
|
168
|
|
|
$id = 0; |
|
169
|
|
|
$failCount = 0; |
|
170
|
|
|
$outputFile = $this->task->getFileOut(); |
|
171
|
|
|
|
|
172
|
|
|
while ($failCount <= self::STARTUP_WAIT_COUNT |
|
173
|
|
|
&& $this->checkTaskFile() |
|
174
|
|
|
) { |
|
175
|
|
|
try { |
|
176
|
|
|
$content = $outputFile->readToString(); |
|
177
|
|
|
|
|
178
|
|
|
if (!empty($content)) { |
|
179
|
|
|
$this->messagePusher->call($this, $id, $content); |
|
180
|
|
|
$id++; |
|
181
|
|
|
} else { |
|
182
|
|
|
$message = TaskFactory::createMessage($this->task->getTaskId(), __('Waiting for progress updating ...')); |
|
183
|
|
|
|
|
184
|
|
|
logger($message->getTask()); |
|
185
|
|
|
|
|
186
|
|
|
$this->messagePusher->call( |
|
187
|
|
|
$this, |
|
188
|
|
|
$id, |
|
189
|
|
|
$message->composeJson() |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
$failCount++; |
|
193
|
|
|
} |
|
194
|
|
|
} catch (FileException $e) { |
|
195
|
|
|
processException($e); |
|
196
|
|
|
|
|
197
|
|
|
$this->messagePusher->call( |
|
198
|
|
|
$this, |
|
199
|
|
|
$id, |
|
200
|
|
|
TaskFactory::createMessage($this->task->getTaskId(), $e->getMessage()) |
|
201
|
|
|
->composeJson() |
|
202
|
|
|
); |
|
203
|
|
|
|
|
204
|
|
|
$failCount++; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
sleep($this->task->getInterval()); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Check whether the task's output file exists |
|
213
|
|
|
*/ |
|
214
|
|
|
private function checkTaskFile() |
|
215
|
|
|
{ |
|
216
|
|
|
try { |
|
217
|
|
|
$this->taskFile->checkFileExists(); |
|
218
|
|
|
|
|
219
|
|
|
return true; |
|
220
|
|
|
} catch (FileException $e) { |
|
221
|
|
|
return false; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.