Skip to content

Commit

Permalink
On sync save we try to ensure we get some state to file by allowing J…
Browse files Browse the repository at this point in the history
…SON_PARTIAL_OUTPUT_ON_ERROR

Reset aysnc failure count once a sync save is complete to allow async saving to continue.
  • Loading branch information
lucasnetau committed Mar 20, 2024
1 parent 614d540 commit dad12e4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/SaveHandler/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use function hrtime;
use function json_decode;
use function json_encode;
use function json_last_error;
use function json_last_error_msg;
use function random_bytes;
use function realpath;
Expand Down Expand Up @@ -132,6 +133,10 @@ public function saveStateAsync(array $state): void
$this->logger->debug('Initialised save handler process');
}
$state = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
if ($state === false) {
$this->emit('save:failed', ['exception' => new RuntimeException("Encoding application state failed. " . json_last_error_msg())] );
return;
}

$uniqid = round(hrtime(true)/1e+3) . '.' . bin2hex(random_bytes(4));
$rpc_request = new JsonRpcRequest(Scheduler::ACTION_RUN_METHOD, ['state' => $state, 'time' => hrtime(true)], $uniqid);
Expand All @@ -151,7 +156,10 @@ public function saveStateSync(array $state): void
$this->emit('save:failed', ['exception' => new RuntimeException("Error creating temporary save state file, check filesystem")] );
return;
}
$state = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION);
$state = json_encode($state, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_PARTIAL_OUTPUT_ON_ERROR);
if (json_last_error() !== JSON_ERROR_NONE) {
$this->logger->warning('Error encoding application state');
}
if ($state === false) {
$this->emit('save:failed', ['exception' => new RuntimeException("Encoding application state failed. " . json_last_error_msg())] );
return;
Expand All @@ -172,6 +180,7 @@ public function saveStateSync(array $state): void
$this->logger->warning('It took ' . $this->saveStateLastDuration . ' milliseconds to save state to disk');
}
$this->logger->debug('State saved to filesystem');
$this->asyncFailureCount = 0; //Reset the async failure count to re-enable async save after a successful sync save
}

public function asyncSaveInProgress(): bool
Expand Down

0 comments on commit dad12e4

Please sign in to comment.