Skip to content

Commit 0957ae3

Browse files
Introduced new error and success handlers
1 parent 0c5250a commit 0957ae3

8 files changed

Lines changed: 208 additions & 25 deletions

packages/async/src/AsyncCheckCollection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
namespace de\codenamephp\deploymentchecks\async;
2020

2121
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
22-
use de\codenamephp\deploymentchecks\base\Result\ResultCollection;
22+
use de\codenamephp\deploymentchecks\base\Result\Collection\ResultCollection;
2323
use de\codenamephp\deploymentchecks\base\Result\ResultInterface;
2424
use Spatie\Async\Pool;
25-
use Spatie\Fork\Fork;
2625
use Throwable;
2726

2827
final readonly class AsyncCheckCollection implements CheckInterface {
@@ -40,10 +39,10 @@ public function run() : ResultInterface {
4039
$result = new ResultCollection();
4140
foreach($this->checks as $check) {
4241
$parallelCheck = new ParallelCheck($check);
43-
$this->pool
42+
$runnable = $this->pool
4443
->add($parallelCheck)
45-
->then(static fn($output) => $parallelCheck->success($result, $output))
46-
->catch(fn(Throwable $exception) => $parallelCheck->error($result, $exception));
44+
->then(static fn(ResultInterface $output) => $parallelCheck->successHandler()->handle($result, $output));
45+
if($parallelCheck instanceof WithErrorHandlerInterface) $runnable->catch(static fn(Throwable $exception) => $parallelCheck->errorHandler()->handle($result, $exception));
4746
}
4847
$this->pool->wait();
4948
return $result;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async\ErrorHandler;
19+
20+
use de\codenamephp\deploymentchecks\base\Result\Collection\ResultCollectionInterface;
21+
use Throwable;
22+
23+
/**
24+
* Interface for a simple error handler. It expects the result collection build in the async collection and the exception that was thrown.
25+
*/
26+
interface ErrorHandlerInterface {
27+
28+
public function handle(ResultCollectionInterface $resultCollection, Throwable $exception) : mixed;
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async\ErrorHandler;
19+
20+
use de\codenamephp\deploymentchecks\base\Result\Collection\ResultCollectionInterface;
21+
use Throwable;
22+
23+
/**
24+
* Just rethrows the exception
25+
*/
26+
final class RethrowException implements ErrorHandlerInterface {
27+
28+
public function handle(ResultCollectionInterface $resultCollection, Throwable $exception) : mixed {
29+
throw $exception;
30+
}
31+
32+
}

packages/async/src/ParallelCheck.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,30 @@
1717

1818
namespace de\codenamephp\deploymentchecks\async;
1919

20-
use Closure;
20+
use de\codenamephp\deploymentchecks\async\ErrorHandler\ErrorHandlerInterface;
21+
use de\codenamephp\deploymentchecks\async\ErrorHandler\RethrowException;
22+
use de\codenamephp\deploymentchecks\async\SuccessHandler\AddToResultCollection;
23+
use de\codenamephp\deploymentchecks\async\SuccessHandler\SuccessHandlerInterface;
2124
use de\codenamephp\deploymentchecks\base\Check\CheckInterface;
22-
use de\codenamephp\deploymentchecks\base\Result\ResultCollection;
2325
use de\codenamephp\deploymentchecks\base\Result\ResultInterface;
24-
use Laravel\SerializableClosure\SerializableClosure;
25-
use Throwable;
2626

27-
final class ParallelCheck {
28-
29-
public readonly SerializableClosure $successCallback;
30-
31-
public readonly SerializableClosure $errorCallback;
27+
final readonly class ParallelCheck implements ParallelCheckInterface, WithErrorHandlerInterface {
3228

3329
public function __construct(
34-
public readonly CheckInterface $check,
35-
Closure|callable $successCallback = null,
36-
Closure|callable $errorCallback = null
37-
) {
38-
$this->successCallback = new SerializableClosure($successCallback ?? static fn(ResultCollection $resultCollection, ResultInterface $result) => $resultCollection->add($result));
39-
$this->errorCallback = new SerializableClosure($errorCallback ?? static fn(ResultCollection $resultCollection, Throwable $exception) => throw $exception);
40-
}
30+
public CheckInterface $check,
31+
public ?SuccessHandlerInterface $successHandler = new AddToResultCollection(),
32+
public ?ErrorHandlerInterface $errorHandler = new RethrowException(),
33+
) {}
4134

4235
public function __invoke() : ResultInterface {
4336
return $this->check->run();
4437
}
4538

46-
public function success(ResultCollection $resultCollection, ResultInterface $result) : mixed {
47-
return ($this->successCallback)($resultCollection, $result);
39+
public function successHandler() : SuccessHandlerInterface {
40+
return $this->successHandler;
4841
}
4942

50-
public function error(ResultCollection $resultCollection, Throwable $exception) : mixed {
51-
return ($this->errorCallback)($resultCollection, $exception);
43+
public function errorHandler() : ErrorHandlerInterface {
44+
return $this->errorHandler;
5245
}
5346
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async;
19+
20+
use de\codenamephp\deploymentchecks\async\SuccessHandler\SuccessHandlerInterface;
21+
use de\codenamephp\deploymentchecks\base\Result\ResultInterface;
22+
23+
/**
24+
* Interface for checks that can be used in parallel using the async pool
25+
*/
26+
interface ParallelCheckInterface {
27+
28+
/**
29+
* Since the async pool only accepts callables, this method is used to invoke the check and return the result
30+
*
31+
* @return ResultInterface
32+
*/
33+
public function __invoke() : ResultInterface;
34+
35+
/**
36+
* Gets the success handler that is used to handle the result if the check was successful
37+
*
38+
* @return SuccessHandlerInterface
39+
*/
40+
public function successHandler() : SuccessHandlerInterface;
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async\SuccessHandler;
19+
20+
use de\codenamephp\deploymentchecks\base\Result\Collection\ResultCollectionInterface;
21+
use de\codenamephp\deploymentchecks\base\Result\ResultInterface;
22+
23+
/**
24+
* Just adds the result to the collection
25+
*/
26+
final class AddToResultCollection implements SuccessHandlerInterface {
27+
28+
public function handle(ResultCollectionInterface $resultCollection, ResultInterface $result) : ResultInterface {
29+
$resultCollection->add($result);
30+
return $result;
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async\SuccessHandler;
19+
20+
use de\codenamephp\deploymentchecks\base\Result\Collection\ResultCollectionInterface;
21+
use de\codenamephp\deploymentchecks\base\Result\ResultInterface;
22+
23+
/**
24+
* Interface for parallel checks that want to define a custom success handler
25+
*/
26+
interface SuccessHandlerInterface {
27+
28+
public function handle(ResultCollectionInterface $resultCollection, ResultInterface $result) : ResultInterface;
29+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\async;
19+
20+
use de\codenamephp\deploymentchecks\async\ErrorHandler\ErrorHandlerInterface;
21+
22+
/**
23+
* Interface for parallel checks that want to define a custom error handler
24+
*/
25+
interface WithErrorHandlerInterface {
26+
27+
public function errorHandler() : ErrorHandlerInterface;
28+
}

0 commit comments

Comments
 (0)