-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundPHPTasks.php
285 lines (234 loc) · 5.77 KB
/
BackgroundPHPTasks.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
class BackgroundPHPTask
{
/**
* The BackgroundPHPTask class.
*
* @category Process
* @package BackgroundPHPTask
* @author gnieark <gnieark@tinad.fr>
* @license GNU General Public License V3
* @link https://github.com/gnieark/BackgroundPHPtasks
*/
/**
* The full path of the pid file to use or create.
*
* @var string
*/
private $pidFile = "";
/**
* the pid id, will be populated after exec.
*
* @var int
*/
private $pid;
/**
* Where is stored the script output.
*
* @var string
*/
private $outputFile = "/dev/null";
/**
* The php's script to execute full path + name .
*
* @var string
*/
private $phpScript;
/**
* Args to pass to the script when called. /!\Shell arguments, not query args $_GET
*
* @var array
*/
private $args = array();
/**
* Store the status
*
* @var string
*/
private $status = "pending"; //should be pending/running/terminated
/**
* Use it as title, unique-key, as you want
*
* @var string
*/
private $identifier =""; //not the PID, juste another identifier if needed
/**
* Return the status, Eventually check for a change before
*
* @return string
*/
public function get_status()
{
if( ( $this->status == "running" ) && (!$this->is_running()) )
{
$this->status = "terminated";
}
return $this->status;
}
/**
* Return the pid (normaly useless,for debug)
*
* @return integer
*/
public function get_pid()
{
return $this->pid;
}
/**
* Return the pid file path (normaly useless,for debug)
*
* @return string
*/
public function get_pifFile()
{
return $this->pidFile;
}
/**
* Return the php script file path (normaly useless,for debug)
*
* @return string
*/
public function get_phpScript()
{
return $this->phpScript;
}
/**
* Return the last pid on a pid file
*
* @return integer
*/
private function get_pid_from_pidfile()
{
$data = file($this->pidFile);
return intval( $data[count($data) -1] );
}
/**
* Set the php script to use
*
* @return BackgroundPHPTask for chaining
*
* @param $phpScript the path
*/
public function set_phpScript(string $phpScript)
{
$this->phpScript = $phpScript;
return $this;
}
/**
* Set the php script to use, but by given the whole script on a string
*
* @return BackgroundPHPTask for chaining
*
* @param $script the script. containing the opening bracket <?php
*/
public function set_phpScriptWithoutFile(string $script)
{
$scriptPath= tempnam(sys_get_temp_dir(), 'BackgroundPhpTask');
file_put_contents($scriptPath, $script);
return $this->set_phpScript($scriptPath);
}
/**
* Set an identifier (optional). Should be usefull for your Deus Ex
* Machina
* @return BackgroundPHPTask for chaining
*
* @param string $identifier, what you want
*/
public function set_identifier(string $identifier)
{
$this->identifier = $identifier;
return $this;
}
/**
* add an argument to give to the script
*
* @return BackgroundPHPTask for chaining
*
* @param string $arg, what you want
*/
public function add_arg(string $arg){
$this->args[] = escapeshellarg($arg);
return $this;
}
/**
* Set where your scripts outputs are going.
* If none given, default is /dev/null
*
* @return BackgroundPHPTask for chaining
*
* @param string Path of output file (will be created if not yet existing)
*/
public function set_outputFile(string $outputFile)
{
$this->outputFile = $outputFile;
return $this;
}
/**
* Set the pid file where the process id will be stored
*
*
* @return BackgroundPHPTask for chaining
*
* @param string Path of pid file (will be created if not yet existing)
*/
public function set_pidFile(string $pidFile)
{
$this->pidFile = $pidFile;
return $this;
}
/**
* Launch the script execution
*
* @return BackgroundPHPTask for chaining
*
*/
public function exec()
{
if(is_null($this->phpScript))
{
throw new Exception('No php script setted');
}
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", PHP_BINARY . " " . $this->phpScript . " " . implode(" ", $this->args), $this->outputFile, $this->pidFile));
$this->status ="running";
$this->pid = $this->get_pid_from_pidfile();
}
/**
* test if a script is running (using his pid)
*
* @return bool
*/
public function is_running()
{
try{
$result = shell_exec(sprintf("ps %s", $this->pid));
if( count(preg_split("/\n/", $result)) > 2){
return true;
}
}catch(Exception $e){}
return false;
}
/**
* Kill the current process if running
*
* @return BackgroundPHPTask for chaining
*
*/
public function stop()
{
posix_kill( $this->pid, SIGTERM );
return $this;
}
/**
* Delete the current process output file, if you need to make cleaness
*
* @return BackgroundPHPTask for chaining
*
*/
public function remove_output_file()
{
if(file_exists ( $this->outputFile )){
unlink($this->outputFile);
}
return $this;
}
}