1 | <?php |
||
35 | class ProcessCleanUpHook |
||
36 | { |
||
37 | /** |
||
38 | * @var CrawlerController |
||
39 | */ |
||
40 | private $crawlerLib; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private $extensionSettings; |
||
46 | |||
47 | /** |
||
48 | * Main function of process CleanUp Hook. |
||
49 | * |
||
50 | * @param CrawlerController $crawlerLib Crawler Lib class |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function crawler_init(CrawlerController $crawlerLib) |
||
55 | { |
||
56 | $this->crawlerLib = $crawlerLib; |
||
57 | $this->extensionSettings = $this->crawlerLib->extensionSettings; |
||
58 | |||
59 | // Clean Up |
||
60 | $this->removeActiveOrphanProcesses(); |
||
61 | $this->removeActiveProcessesOlderThanOneHour(); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Remove active processes older than one hour |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | private function removeActiveProcessesOlderThanOneHour() |
||
70 | { |
||
71 | $results = $this->getDatabaseConnection()->exec_SELECTgetRows( |
||
72 | 'process_id, system_process_id', |
||
73 | 'tx_crawler_process', |
||
74 | 'ttl <= ' . intval(time() - $this->extensionSettings['processMaxRunTime'] - 3600) . ' AND active = 1' |
||
75 | ); |
||
76 | |||
77 | if (!is_array($results)) { |
||
78 | return; |
||
79 | } |
||
80 | foreach ($results as $result) { |
||
81 | $systemProcessId = (int)$result['system_process_id']; |
||
82 | $processId = $result['process_id']; |
||
83 | if ($systemProcessId > 1) { |
||
84 | if ($this->doProcessStillExists($systemProcessId)) { |
||
85 | $this->killProcess($systemProcessId); |
||
86 | } |
||
87 | $this->removeProcessFromProcesslist($processId); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Removes active orphan processes from process list |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | private function removeActiveOrphanProcesses() |
||
98 | { |
||
99 | $results = $this->getDatabaseConnection()->exec_SELECTgetRows( |
||
100 | 'process_id, system_process_id', |
||
101 | 'tx_crawler_process', |
||
102 | 'ttl <= ' . intval(time() - $this->extensionSettings['processMaxRunTime']) . ' AND active = 1' |
||
103 | ); |
||
104 | |||
105 | if (!is_array($results)) { |
||
106 | return; |
||
107 | } |
||
108 | foreach ($results as $result) { |
||
109 | $processExists = false; |
||
110 | $systemProcessId = (int)$result['system_process_id']; |
||
111 | $processId = $result['process_id']; |
||
112 | if ($systemProcessId > 1) { |
||
113 | $dispatcherProcesses = $this->findDispatcherProcesses(); |
||
114 | if (!is_array($dispatcherProcesses) || empty($dispatcherProcesses)) { |
||
115 | $this->removeProcessFromProcesslist($processId); |
||
116 | return; |
||
117 | } |
||
118 | foreach ($dispatcherProcesses as $process) { |
||
119 | $responseArray = $this->createResponseArray($process); |
||
120 | if ($systemProcessId === (int)$responseArray[1]) { |
||
121 | $processExists = true; |
||
122 | }; |
||
123 | } |
||
124 | if (!$processExists) { |
||
125 | $this->removeProcessFromProcesslist($processId); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Remove a process from processlist |
||
133 | * |
||
134 | * @param string $processId Unique process Id. |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | private function removeProcessFromProcesslist($processId) |
||
139 | { |
||
140 | $this->getDatabaseConnection()->exec_DELETEquery( |
||
141 | 'tx_crawler_process', |
||
142 | 'process_id = ' . $this->getDatabaseConnection()->fullQuoteStr($processId, 'tx_crawler_process') |
||
143 | ); |
||
144 | |||
145 | $this->getDatabaseConnection()->exec_UPDATEquery( |
||
146 | 'tx_crawler_queue', |
||
147 | 'process_id = ' . $this->getDatabaseConnection()->fullQuoteStr($processId, 'tx_crawler_queue'), |
||
148 | ['process_id' => ''] |
||
149 | ); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Create response array |
||
154 | * Convert string to array with space character as delimiter, |
||
155 | * removes all empty records to have a cleaner array |
||
156 | * |
||
157 | * @param string $string String to create array from |
||
158 | * |
||
159 | * @return array |
||
160 | * |
||
161 | */ |
||
162 | private function createResponseArray($string) |
||
168 | |||
169 | /** |
||
170 | * Check if the process still exists |
||
171 | * |
||
172 | * @param int $pid Process id to be checked. |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | private function doProcessStillExists($pid) |
||
193 | |||
194 | /** |
||
195 | * Kills a process |
||
196 | * |
||
197 | * @param int $pid Process id to kill |
||
198 | * |
||
199 | * @return void |
||
200 | */ |
||
201 | private function killProcess($pid) |
||
211 | |||
212 | /** |
||
213 | * Find dispatcher processes |
||
214 | * |
||
215 | * @return array |
||
216 | */ |
||
217 | private function findDispatcherProcesses() |
||
229 | |||
230 | /** |
||
231 | * Check if OS is Windows |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | private function isOsWindows() |
||
242 | |||
243 | /** |
||
244 | * @return \TYPO3\CMS\Core\Database\DatabaseConnection |
||
245 | */ |
||
246 | private function getDatabaseConnection() |
||
250 | } |
||
251 |