Skip to content

Commit

Permalink
Cleanup restoreState handler. Add an extra try/catch block when resto…
Browse files Browse the repository at this point in the history
…ring state to output a user-friendly error message. Add additional logging when loading from saved state.
  • Loading branch information
lucasnetau committed Sep 6, 2021
1 parent 2620a78 commit ff29fea
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,26 @@ function scheduleNextTimeout()
}

/**
*
* Boot up from a saved state file
*/
protected function restoreState()
{
/** Load State from save file */
$savedState = $this->loadStateFromFile();
$restored = $savedState !== false;
if (false !== $savedState) {
$this->setState($savedState['scheduler']);
unset($savedState['scheduler']);
}
$restoring = $savedState !== false;

/** Initialise the Correlation Engine */
$this->engine = new CorrelationEngine($this->rules);
if (false !== $savedState) {
$this->engine->setState($savedState['engine']);
unset($savedState['engine']);

if ($restoring) {
try {
$this->setState($savedState['scheduler']);
$this->engine->setState($savedState['engine']);
} catch (Exception $ex) {
fwrite(STDERR, "A fatal exception was thrown while loading previous saved state. " . $ex->getMessage() . PHP_EOL);
exit(-1);
}
fwrite(STDERR, "Successfully loaded from saved state" . PHP_EOL);
}
unset($savedState);

Expand All @@ -592,8 +595,8 @@ protected function restoreState()
/** Inject some synthetic events in to the engine to flag that the engine is starting for the first time or restoring
* Rules can handle these events for initialisation purposes (handy for setting up rules that detect when an event is missing)
*/
$this->loop->futureTick(function() use ($restored) {
$event = $restored ? new Event(['event' => static::CONTROL_MSG_RESTORED_STATE]) : new Event(['event' => static::CONTROL_MSG_NEW_STATE]);
$this->loop->futureTick(function() use ($restoring) {
$event = $restoring ? new Event(['event' => static::CONTROL_MSG_RESTORED_STATE]) : new Event(['event' => static::CONTROL_MSG_NEW_STATE]);

/**
* Pass the event to the engine to be handled
Expand Down

0 comments on commit ff29fea

Please sign in to comment.