1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-07-20 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\ProcessForkManager; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractTask |
11
|
|
|
* @package Net\Bazzline\Component\ProcessForkManager |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractTask implements TaskInterface |
14
|
|
|
{ |
15
|
|
|
const STATUS_ABORTED = 2; |
16
|
|
|
const STATUS_FINISHED = 1; |
17
|
|
|
const STATUS_NOT_STARTED = 0; |
18
|
|
|
const STATUS_RUNNING = 3; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $parentProcessId; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $startTime = 0; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $status = self::STATUS_NOT_STARTED; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public function getRunTime() |
39
|
|
|
{ |
40
|
|
|
return (time() - $this->startTime); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return int |
45
|
|
|
*/ |
46
|
|
|
public function getMemoryUsage() |
47
|
|
|
{ |
48
|
|
|
return memory_get_usage(true); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function getParentProcessId() |
55
|
|
|
{ |
56
|
|
|
return $this->parentProcessId; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
public function getProcessId() |
63
|
|
|
{ |
64
|
|
|
return getmypid(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public function isAborted() |
71
|
|
|
{ |
72
|
|
|
return ($this->status === self::STATUS_ABORTED); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function isFinished() |
79
|
|
|
{ |
80
|
|
|
return ($this->status === self::STATUS_FINISHED); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isNotStarted() |
87
|
|
|
{ |
88
|
|
|
return ($this->status === self::STATUS_NOT_STARTED); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function isRunning() |
95
|
|
|
{ |
96
|
|
|
return ($this->status === self::STATUS_RUNNING); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function markAsAborted() |
100
|
|
|
{ |
101
|
|
|
$this->status = self::STATUS_ABORTED; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function markAsFinished() |
105
|
|
|
{ |
106
|
|
|
$this->status = self::STATUS_FINISHED; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function markAsRunning() |
110
|
|
|
{ |
111
|
|
|
$this->status = self::STATUS_RUNNING; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $parentProcessId |
116
|
|
|
*/ |
117
|
|
|
public function setParentProcessId($parentProcessId) |
118
|
|
|
{ |
119
|
|
|
$this->parentProcessId = (int) $parentProcessId; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int $timestamp |
124
|
|
|
*/ |
125
|
|
|
public function setStartTime($timestamp) |
126
|
|
|
{ |
127
|
|
|
$this->startTime = (int) $timestamp; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function dispatchSignal() |
131
|
|
|
{ |
132
|
|
|
pcntl_signal_dispatch(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $nameOfSignalHandlerMethod |
137
|
|
|
* @throws InvalidArgumentException |
138
|
|
|
* @codeCoverageIgnore |
139
|
|
|
*/ |
140
|
|
|
protected function setUpSignalHandling($nameOfSignalHandlerMethod) |
141
|
|
|
{ |
142
|
|
View Code Duplication |
if (!is_callable(array($this, $nameOfSignalHandlerMethod))) { |
|
|
|
|
143
|
|
|
throw new InvalidArgumentException( |
144
|
|
|
'provided method name "' . $nameOfSignalHandlerMethod . '" is not available' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
//pcntl_signal(SIGHUP, array($this, $nameOfSignalHandlerMethod)); |
149
|
|
|
pcntl_signal(SIGINT, array($this, $nameOfSignalHandlerMethod)); //shell ctrl+c |
150
|
|
|
//pcntl_signal(SIGUSR1, array($this, $nameOfSignalHandlerMethod)); |
151
|
|
|
//pcntl_signal(SIGUSR2, array($this, $nameOfSignalHandlerMethod)); |
152
|
|
|
//pcntl_signal(SIGQUIT, array($this, $nameOfSignalHandlerMethod)); |
153
|
|
|
//pcntl_signal(SIGILL, array($this, $nameOfSignalHandlerMethod)); |
154
|
|
|
//pcntl_signal(SIGABRT, array($this, $nameOfSignalHandlerMethod)); |
155
|
|
|
//pcntl_signal(SIGFPE, array($this, $nameOfSignalHandlerMethod)); |
156
|
|
|
//pcntl_signal(SIGSEGV, array($this, $nameOfSignalHandlerMethod)); |
157
|
|
|
//pcntl_signal(SIGPIPE, array($this, $nameOfSignalHandlerMethod)); |
158
|
|
|
//pcntl_signal(SIGALRM, array($this, $nameOfSignalHandlerMethod)); |
159
|
|
|
pcntl_signal(SIGTERM, array($this, $nameOfSignalHandlerMethod)); //kill <pid> |
160
|
|
|
//pcntl_signal(SIGCHLD, array($this, $nameOfSignalHandlerMethod)); |
161
|
|
|
//pcntl_signal(SIGCONT, array($this, $nameOfSignalHandlerMethod)); |
162
|
|
|
//pcntl_signal(SIGTSTP, array($this, $nameOfSignalHandlerMethod)); |
163
|
|
|
//pcntl_signal(SIGTTIN, array($this, $nameOfSignalHandlerMethod)); |
164
|
|
|
//pcntl_signal(SIGTTOU, array($this, $nameOfSignalHandlerMethod)); |
165
|
|
|
} |
166
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.