Skip to content

Commit 8a687cb

Browse files
author
Benjamin Calef
committed
[v0.1.6] resolve issue with 'mysql gone away' in cron context
1 parent 8a6e0e6 commit 8a687cb

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Diff for: Model/Processor/ForkedProcessor.php

+19-7
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,14 @@ private function handleSingleChildProcesses(): void
8686
} else {
8787
// child process
8888
$this->processChild($currentPage, $totalPages, $childProcessCounter);
89-
exit(0);
89+
register_shutdown_function(function() {
90+
posix_kill(getmypid(), SIGKILL);
91+
});
92+
exit;
9093
}
9194

9295
$pid = pcntl_waitpid($pid, $status);
93-
if (pcntl_wexitstatus($status) != 0) {
96+
if (pcntl_wtermsig($status) != 9) {
9497
$this->logger->error('Error with child process', [
9598
'pid' => $pid,
9699
'exit_code' => $status
@@ -122,7 +125,7 @@ private function handleMultipleChildProcesses(): void
122125
// manage children
123126
while ($childProcessCounter >= $this->maxChildrenProcess) {
124127
$pid = pcntl_wait($status);
125-
if (pcntl_wexitstatus($status) != 0) {
128+
if (pcntl_wtermsig($status) != 9) {
126129
$this->logger->error('Error with child process', [
127130
'pid' => $pid,
128131
'exit_code' => $status
@@ -143,7 +146,10 @@ private function handleMultipleChildProcesses(): void
143146
} else {
144147
// child process
145148
$this->processChild($currentPage, $totalPages, $childProcessCounter);
146-
exit(0);
149+
register_shutdown_function(function() {
150+
posix_kill(getmypid(), SIGKILL);
151+
});
152+
exit;
147153
}
148154

149155
$currentPage++;
@@ -157,7 +163,7 @@ private function handleMultipleChildProcesses(): void
157163
while ($childProcessCounter > 0) {
158164
$pid = pcntl_wait($status);
159165
$childProcessCounter--;
160-
if (pcntl_wexitstatus($status) != 0) {
166+
if (pcntl_wtermsig($status) != 9) {
161167
$this->logger->error('Error with child process', [
162168
'pid' => $pid,
163169
'exit_code' => $status
@@ -202,7 +208,10 @@ private function processChild(int $currentPage, int $totalPages, int $childProce
202208
'current_page' => $currentPage,
203209
'exception' => $e
204210
]);
205-
exit(1);
211+
register_shutdown_function(function() {
212+
posix_kill(getmypid(), SIGABRT);
213+
});
214+
exit;
206215
}
207216

208217
$this->logger->info('Running child process', [
@@ -229,7 +238,10 @@ private function processChild(int $currentPage, int $totalPages, int $childProce
229238
}
230239

231240
if ($itemProceed === 0) {
232-
exit(1);
241+
register_shutdown_function(function() {
242+
posix_kill(getmypid(), SIGABRT);
243+
});
244+
exit;
233245
}
234246

235247
$this->logger->info('Finished child process', [

Diff for: composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "zepgram/module-multi-threading",
33
"description": "This module is a powerful tool for developers who want to process large data sets in a short amount of time",
44
"type": "magento2-module",
5-
"version": "0.1.5",
5+
"version": "0.1.6",
66
"authors": [
77
{
88
"name": "Benjamin Calef",
@@ -12,7 +12,8 @@
1212
"require": {
1313
"magento/framework": "^101.0.0|^102.0.0|^103.0.0",
1414
"magento/module-store": "^101",
15-
"ext-pcntl": "*"
15+
"ext-pcntl": "*",
16+
"ext-posix": "*"
1617
},
1718
"autoload": {
1819
"files": [

0 commit comments

Comments
 (0)