1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scheduler\ActionInspector; |
4
|
|
|
|
5
|
|
|
use Scheduler\Action\ActionInterface; |
6
|
|
|
use Scheduler\Exception\SchedulerException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FileActionInspector |
10
|
|
|
* @package Scheduler\ActionInspector |
11
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class FileActionInspector extends AbstractActionInspector |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
private $filePath; |
17
|
|
|
|
18
|
|
|
/** @var resource */ |
19
|
|
|
private $fh; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* FileActionInspector constructor. |
23
|
|
|
* @param $filePath |
24
|
|
|
*/ |
25
|
2 |
|
public function __construct($filePath) |
26
|
|
|
{ |
27
|
2 |
|
$this->filePath = $filePath; |
28
|
2 |
|
if (!file_exists($this->filePath)) { |
29
|
2 |
|
file_put_contents($this->filePath, ''); |
30
|
2 |
|
} |
31
|
2 |
|
$this->fh = fopen($this->filePath, 'r+'); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ActionInterface $action |
36
|
|
|
* @return boolean returns `false` if action is already exists in this state in the log |
37
|
|
|
* @throws SchedulerException |
38
|
|
|
*/ |
39
|
2 |
|
public function update(ActionInterface $action) |
40
|
|
|
{ |
41
|
2 |
|
flock($this->fh, LOCK_EX); |
42
|
2 |
|
$messages = $this->getMessages(); |
43
|
2 |
|
$actionId = $action->getId(); |
44
|
2 |
|
$actionState = $action->getState(); |
45
|
2 |
|
$previousState = isset($messages[$actionId]) ? $messages[$actionId]['state'] : null; |
46
|
|
|
|
47
|
2 |
|
if ($this->isStateAllowed($action, $previousState)) { |
48
|
2 |
|
$messages[$actionId]['state'] = $actionState; |
49
|
2 |
|
$result = true; |
50
|
2 |
|
if ($actionState === ActionInterface::STATE_FINISHED) { |
51
|
2 |
|
$messages[$actionId]['report'] = $action->getReport(); |
52
|
2 |
|
} |
53
|
2 |
|
} else { |
54
|
2 |
|
$result = false; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$this->save($messages); |
58
|
2 |
|
flock($this->fh, LOCK_UN); |
59
|
2 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Close an open file pointer when class instance destructs. |
64
|
|
|
*/ |
65
|
2 |
|
public function __destruct() |
66
|
|
|
{ |
67
|
2 |
|
fclose($this->fh); |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get action messages from log file |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
2 |
|
private function getMessages() |
75
|
|
|
{ |
76
|
2 |
|
$messages = []; |
77
|
2 |
|
$content = ''; |
78
|
2 |
|
fseek($this->fh, 0); |
79
|
2 |
|
while (!feof($this->fh)) { |
80
|
2 |
|
$content .= fgets($this->fh); |
81
|
2 |
|
} |
82
|
2 |
|
if ($content) { |
83
|
2 |
|
$messages = json_decode($content, true); |
84
|
2 |
|
} |
85
|
2 |
|
return $messages; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Save action messages to file |
90
|
|
|
* @param array $messages |
91
|
|
|
*/ |
92
|
2 |
|
private function save(array $messages) |
93
|
|
|
{ |
94
|
2 |
|
fseek($this->fh, 0); |
95
|
2 |
|
fwrite($this->fh, json_encode($messages)); |
96
|
|
|
} |
97
|
|
|
} |