Skip to content

Commit

Permalink
Merge pull request #230 from Riley19280/serializable-checks
Browse files Browse the repository at this point in the history
Allow checks to be serializable
  • Loading branch information
freekmurze authored Jun 27, 2024
2 parents 971b330 + f2b1fd2 commit 1ea1950
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
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:
- { }

0 comments on commit 1ea1950

Please sign in to comment.