Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow checks to be serializable #230

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"illuminate/database": "^8.75|^9.0|^10.0|^11.0",
"illuminate/notifications": "^8.75|^9.0|^10.0|^11.0",
"illuminate/support": "^8.75|^9.0|^10.0|^11.0",
"laravel/serializable-closure": "^1.3",
"nunomaduro/termwind": "^1.0|^2.0",
"spatie/enum": "^3.13",
"spatie/laravel-package-tools": "^1.12.1",
Expand Down
32 changes: 32 additions & 0 deletions src/Checks/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Macroable;
use Laravel\SerializableClosure\SerializableClosure;
use Spatie\Health\Enums\Status;

abstract class Check
Expand Down Expand Up @@ -78,6 +79,10 @@ public function getName(): string
return Str::of($baseName)->beforeLast('Check');
}

public function getRunConditions(): array {
return $this->shouldRun;
}

public function shouldRun(): bool
{
foreach ($this->shouldRun as $shouldRun) {
Expand Down Expand Up @@ -119,4 +124,31 @@ public function markAsCrashed(): Result
public function onTerminate(mixed $request, mixed $response): void
{
}

public function __serialize(): array {
$vars = get_object_vars($this);

$serializableClosures = [];
foreach ($vars['shouldRun'] as $shouldRun) {
$serializableClosures[] = new SerializableClosure($shouldRun);
}

$vars['shouldRun'] = $serializableClosures;

return $vars;
}

public function __unserialize(array $data): void {
foreach ($data as $property => $value) {
$this->$property = $value;
}

$unwrappedClosures = [];

foreach($this->shouldRun as $shouldRun) {
$unwrappedClosures[] = $shouldRun->getClosure();
}

$this->shouldRun = $unwrappedClosures;
}
}
29 changes: 29 additions & 0 deletions tests/Checks/QueueCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use Spatie\Health\Jobs\HealthQueueJob;

use function Pest\Laravel\artisan;
use function PHPUnit\Framework\assertCount;
use function PHPUnit\Framework\assertInstanceOf;
use function Spatie\PestPluginTestTime\testTime;
use function Spatie\Snapshots\assertMatchesObjectSnapshot;
use function Spatie\Snapshots\assertMatchesSnapshot;

beforeEach(function () {
$this->queueCheck = QueueCheck::new();
Expand Down Expand Up @@ -109,3 +113,28 @@

expect($this->queueCheck->getQueues())->toBe([$queueName]);
});

it('can be serialized', function() {
$check = QueueCheck::new()
->onQueue('sync')
->if(fn() => false);

$result = serialize($check);
// Replace with a consistent identifier
$result = preg_replace('/s:32:"[0-9a-z]{32}";/', 's:32:"0000000000000000000000000000000000000000";', $result);

assertMatchesSnapshot($result);
});

it('can be unserialized', function() {
$check = QueueCheck::new()
->onQueue('sync')
->if(fn() => false);

$result = unserialize(serialize($check));

assertCount(1, $result->getRunConditions());
assertInstanceOf(Closure::class, $result->getRunConditions()[0]);

assertMatchesObjectSnapshot($result);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
O:38:"Spatie\Health\Checks\Checks\QueueCheck":8:{s:10:"expression";s:9:"* * * * *";s:4:"name";N;s:5:"label";N;s:9:"shouldRun";a:1:{i:0;O:47:"Laravel\SerializableClosure\SerializableClosure":1:{s:12:"serializable";O:46:"Laravel\SerializableClosure\Serializers\Native":5:{s:3:"use";a:0:{}s:8:"function";s:13:"fn() => false";s:5:"scope";s:29:"P\Tests\Checks\QueueCheckTest";s:4:"this";N;s:4:"self";s:32:"0000000000000000000000000000000000000000";}}}s:8:"cacheKey";s:37:"health:checks:queue:latestHeartbeatAt";s:14:"cacheStoreName";N;s:37:"failWhenTestJobTakesLongerThanMinutes";i:5;s:8:"onQueues";a:1:{i:0;s:4:"sync";}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cacheStoreName: array
queues:
- sync
label: Queue
name: Queue
runConditions:
- { }
Loading