-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScriptAlone.php
177 lines (147 loc) · 5.17 KB
/
ScriptAlone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
*
* @desc Class for limiting running only one instance of some script
* @see https://github.com/barbushin/scriptalone
* @author Barbushin Sergey http://linkedin.com/in/barbushin
*
*/
class ScriptAlone {
protected $processUid;
protected $startedTime;
protected $workedTime;
protected $expireTime;
protected $withoutNotifyLifetime;
protected $scriptLifetime;
protected $stateFilepath;
protected $stopOnReadyToStop;
protected $scriptWasStopped;
protected $scriptWasRestarted;
public function __construct($stateFilepath, $withoutNotifyLifetime = 60, $scriptLifetime = null, $stopOnReadyToStop = false) {
if(!$withoutNotifyLifetime) {
$withoutNotifyLifetime = 60*60*24*365*100; // 100 years
}
if($scriptLifetime < $withoutNotifyLifetime) {
throw new Exception('Value of argument "scriptLifetime" cannot be less than value of "withoutNotifyLifetime"');
}
$this->stateFilepath = $stateFilepath;
$this->withoutNotifyLifetime = $withoutNotifyLifetime;
$this->scriptLifetime = $scriptLifetime;
$this->stopOnReadyToStop = $stopOnReadyToStop;
$this->processUid = time() . mt_rand();
$this->startedTime = time();
if ($this->isAnotherNotExpiredScriptInstance()) {
$this->stop('Another script instance still not expired');
}
$this->notifyItWorks(false);
}
/**************************************************************
STATE STORAGE
**************************************************************/
protected function stateVarsToString(array $vars) {
$string = '';
foreach ($vars as $title => $value) {
$string .= "$title: $value\n";
}
return rtrim($string, "\n");
}
protected function stateStringToVars($string) {
$vars = array();
if (preg_match_all('/([\w]+?)\:\s*(.+)/', $string, $m)) {
foreach ($m[1] as $i => $title) {
$vars[$title] = $m[2][$i];
}
}
return $vars;
}
protected function saveState() {
$stateString = $this->stateVarsToString(array(
'PID' => $this->processUid,
'Started' => date('Y-m-d H:i:s', $this->startedTime),
'Worked' => date('Y-m-d H:i:s', $this->workedTime),
'Expire' => date('Y-m-d H:i:s', $this->expireTime)));
if (!file_put_contents($this->stateFilepath, $stateString)) {
throw new Exception('Saving state was failed, cannot write file '.$this->stateFilepath);
}
}
protected function readState(&$processUid, &$startedTime, &$workedTime, &$expireTime) {
clearstatcache();
if (is_file($this->stateFilepath)) {
$state = $this->stateStringToVars(file_get_contents($this->stateFilepath));
$processUid = isset($state['PID']) ? $state['PID'] : null;
$startedTime = isset($state['Started']) ? strtotime($state['Started']) : null;
$workedTime = isset($state['Worked']) ? strtotime($state['Worked']) : null;
$expireTime = isset($state['Expire']) ? strtotime($state['Expire']) : null;
return $state;
}
return false;
}
/**************************************************************
STATES
**************************************************************/
public function isAnotherNotExpiredScriptInstance() {
if ($state = $this->readState($processUid, $startedTime, $workedTime, $expireTime)) {
return $processUid != $this->processUid && $expireTime > time();
}
return false;
}
public function isAnotherScriptInstanceStarted() {
if ($this->readState($processUid, $startedTime, $workedTime, $expireTime)) {
return $processUid != $this->processUid;
}
return false;
}
public function isReadyToStop() {
return $this->scriptLifetime && ($this->startedTime + $this->scriptLifetime - time()) < $this->withoutNotifyLifetime;
}
public function isScriptStopped() {
return is_file($this->stateFilepath . '.stop');
}
public function isScriptRestart() {
return is_file($this->stateFilepath . '.restart');
}
/**************************************************************
ACTIONS
**************************************************************/
public function notifyItWorks($checkAnotherScriptInstanceStarted = true) {
$this->workedTime = time();
$this->expireTime = $this->workedTime + $this->withoutNotifyLifetime;
if ($this->isScriptStopped()) {
$this->unlinkStateFilepath();
$this->stop('Script is stoped by stop-file');
}
if($this->isScriptRestart()) {
$this->restart();
}
elseif ($checkAnotherScriptInstanceStarted && $this->isAnotherScriptInstanceStarted()) {
$this->stop('Another script instance was started');
}
elseif($this->isReadyToStop() && $this->stopOnReadyToStop) {
$this->unlinkStateFilepath();
$this->stop('Script lifetime is over');
}
set_time_limit($this->withoutNotifyLifetime);
$this->saveState();
}
protected function stop($message) {
$this->scriptWasStopped = true;
throw new ScriptAlone_Stopped($message);
}
protected function restart() {
unlink($this->stateFilepath . '.restart');
$this->scriptWasRestarted = true;
$this->stop('Script need to be restarted');
}
protected function unlinkStateFilepath() {
if (is_file($this->stateFilepath)) {
unlink($this->stateFilepath);
}
}
public function __destruct() {
if (!$this->scriptWasStopped || $this->scriptWasRestarted) {
$this->unlinkStateFilepath();
}
}
}
class ScriptAlone_Stopped extends Exception {
}