-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from bhushan/feat/process-fork
feat: create child process for calling guzzle http request
- Loading branch information
Showing
2 changed files
with
51 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,8 @@ | |
use Treblle\Model\Data; | ||
use Treblle\Model\Error; | ||
|
||
use function Safe\getmypid; | ||
|
||
/** | ||
* Create a FREE Treblle account => https://treblle.com/register. | ||
*/ | ||
|
@@ -130,6 +132,8 @@ private function buildPayload(): array | |
|
||
/** | ||
* Process the log when PHP is finished processing. | ||
* | ||
* @throws \Throwable | ||
*/ | ||
public function onShutdown(): void | ||
{ | ||
|
@@ -142,11 +146,44 @@ public function onShutdown(): void | |
} | ||
|
||
/** @todo come up with some kind of fallback to be sent if we cannot convert array to json */ | ||
$payload = []; | ||
$payload = '{}'; | ||
} | ||
|
||
if (!\function_exists('pcntl_fork') || (\defined('ARE_TESTS_RUNNING') && ARE_TESTS_RUNNING)) { | ||
$this->collectData($payload); | ||
|
||
return; | ||
} | ||
|
||
$pid = pcntl_fork(); | ||
|
||
if ($this->isUnableToForkProcess($pid)) { | ||
$this->collectData($payload); | ||
|
||
return; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JustSteveKing
Author
Contributor
|
||
} | ||
|
||
if ($this->isChildProcess($pid)) { | ||
$this->collectData($payload); | ||
$this->killProcessWithId((int) getmypid()); | ||
} | ||
} | ||
|
||
public function getBaseUrl(): string | ||
{ | ||
$urls = [ | ||
'https://rocknrolla.treblle.com', | ||
'https://punisher.treblle.com', | ||
'https://sicario.treblle.com', | ||
]; | ||
|
||
return $urls[array_rand($urls)]; | ||
} | ||
|
||
private function collectData(string $payload): void | ||
{ | ||
try { | ||
$response = $this->guzzle->request( | ||
$this->guzzle->request( | ||
'POST', | ||
$this->getBaseUrl(), | ||
[ | ||
|
@@ -168,14 +205,18 @@ public function onShutdown(): void | |
} | ||
} | ||
|
||
public function getBaseUrl(): string | ||
private function isChildProcess(int $pid): bool | ||
{ | ||
$urls = [ | ||
'https://rocknrolla.treblle.com', | ||
'https://punisher.treblle.com', | ||
'https://sicario.treblle.com', | ||
]; | ||
return $pid === 0; | ||
} | ||
|
||
return $urls[array_rand($urls)]; | ||
private function isUnableToForkProcess(int $pid): bool | ||
{ | ||
return $pid === -1; | ||
} | ||
|
||
private function killProcessWithId(int $pid): void | ||
{ | ||
mb_strtoupper(mb_substr(PHP_OS, 0, 3)) === 'WIN' ? exec("taskkill /F /T /PID {$pid}") : exec("kill -9 {$pid}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Note that the parent process MUST wait on the PID in order to reap the process -- otherwise, zombie processes will remain and eventually crash the server. Pretty sure, anyway. However I suggest doing something like this:
Note the
static function()
so the class can still be destructed if required by the GC. This ensures the current request is closed, then the process waits for the fork to complete.