-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Having non JSON-RPC being written to STDOUT will cause the source or …
…action processes to be terminated. Increase error handling for these to catch this before it happens and ensure it is logged correctly function setupErrorHandling() -> Ensure we don't write errors to STDOUT, Initialise shutdown (errors) and exception handlers for source and action processes function wrap_source_php_cmd() -> When a source process is wrapped with this function instead of php_cmd, the file source_prepend.php is configured as the auto_prepend_file directive. The source_prepend.php does some boilerplate setup with loading composer autoload, setting up error handling and disabling output buffering
- Loading branch information
1 parent
fa4912d
commit d214d81
Showing
7 changed files
with
133 additions
and
20 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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use function EdgeTelemetrics\EventCorrelation\initialiseSourceProcess; | ||
|
||
require __DIR__ . '/../../../vendor/autoload.php'; | ||
|
||
initialiseSourceProcess(true); |
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
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
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use EdgeTelemetrics\EventCorrelation\Event; | ||
use EdgeTelemetrics\EventCorrelation\Scheduler; | ||
use EdgeTelemetrics\JSON_RPC\Notification as JsonRpcNotification; | ||
|
||
$event = new Event(['event' => 'Test:Event:1','datetime' => (new DateTimeImmutable())->format('c')]); | ||
$rpc = new JsonRpcNotification(Scheduler::INPUT_ACTION_HANDLE, ['event' => $event]); | ||
fwrite(STDOUT, json_encode($rpc) . "\n"); | ||
|
||
trigger_error('Test error handling of a triggered error'); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types=1); | ||
|
||
use EdgeTelemetrics\EventCorrelation\Event; | ||
use EdgeTelemetrics\EventCorrelation\Scheduler; | ||
use EdgeTelemetrics\JSON_RPC\Notification as JsonRpcNotification; | ||
|
||
$event = new Event(['event' => 'Test:Event:1','datetime' => (new DateTimeImmutable())->format('c')]); | ||
$rpc = new JsonRpcNotification(Scheduler::INPUT_ACTION_HANDLE, ['event' => $event]); | ||
fwrite(STDOUT, json_encode($rpc) . "\n"); | ||
|
||
throw new Exception('Test error handling of an un-caught exception'); |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* Test: Unhandled exception thrown in action | ||
*/ | ||
use Bref\Logger\StderrLogger; | ||
use EdgeTelemetrics\EventCorrelation\Scheduler; | ||
use EdgeTelemetrics\EventCorrelation\tests\Rules\LogEverything; | ||
use Psr\Log\LogLevel; | ||
|
||
use function EdgeTelemetrics\EventCorrelation\php_cmd; | ||
use function EdgeTelemetrics\EventCorrelation\wrap_source_php_cmd; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$rules = [ | ||
LogEverything::class, | ||
]; | ||
|
||
define('STATE_FILE', tempnam(sys_get_temp_dir(), 'php-ec-test.')); | ||
|
||
(new class($rules) extends Scheduler { | ||
public function __construct(array $rules) | ||
{ | ||
parent::__construct($rules); | ||
set_exception_handler([$this, "handle_exception"]); | ||
$this->setLogger(new StderrLogger(LogLevel::DEBUG)); | ||
|
||
$this->register_input_process('source_with_exception', wrap_source_php_cmd(__DIR__ . "/Sources/source_with_exception.php")); | ||
$this->register_input_process('source_with_exception', wrap_source_php_cmd(__DIR__ . "/Sources/source_with_error.php")); | ||
|
||
$this->register_action('log', php_cmd(__DIR__ . "/actions/log.php"), null, false, ['LOG_FILENAME' => '/tmp/php-ec-test.txt']); | ||
|
||
$this->setSavefileName(STATE_FILE); | ||
$this->setSaveStateInterval(1); | ||
} | ||
|
||
public function handle_exception(Throwable $exception) { | ||
$this->logger->emergency("Fatal", ['exception' => $exception,]); | ||
} | ||
})->run(); | ||
|
||
$state = json_decode(file_get_contents(STATE_FILE), true); | ||
echo json_encode($state, JSON_PRETTY_PRINT); | ||
|
||
unlink(STATE_FILE); | ||
unlink('/tmp/php-ec-test.txt'); |