Repository navigation
feat: typed callbacks and errors #17
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ccfba72
feat: expose callback failures as {code, message}
loks0n 33b76e1
fix: validate every present callback error
loks0n 749d1b6
test: assert the null result directly
loks0n f4b9b0f
refactor: keep the envelope ignorant of its payload
loks0n 52e47fc
feat: type failure codes as an enum, drop the legacy string shape
loks0n 3177349
feat: typed callback payloads behind a generic CloudEvent
loks0n f1a3514
refactor: Payload is Callback, Failure is Error
loks0n 8815df7
fix: callbacks live in Callback, not Callback\Callback
loks0n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| /** | ||
| * The data of one callback, typed by its event. Decode a raw envelope into | ||
| * one with CallbackEvent::decode() — see CloudEvent::decode(). | ||
| */ | ||
| interface Callback | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| public static function fromArray(array $data): static; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Exception\ClientException; | ||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * orchestrator.deployment.response — an async request completed. statusCode and | ||
| * body are null when the request never reached a replica; $error then says | ||
| * why. A body that is not valid UTF-8 arrives base64-encoded with bodyEncoding | ||
| * "base64". requestHeaders is null when it was dropped for size. | ||
| */ | ||
| final readonly class DeploymentResponse implements Callback | ||
| { | ||
| public function __construct( | ||
| public string $deploymentId, | ||
| public string $invocationId, | ||
| public string $requestMethod, | ||
| public string $requestPath, | ||
| public bool $requestPathTruncated, | ||
| /** @var array<string, list<string>>|null */ | ||
| public ?array $requestHeaders, | ||
| public bool $requestHeadersTruncated, | ||
| public ?float $durationSeconds, | ||
| public ?int $statusCode, | ||
| public ?string $body, | ||
| public ?string $bodyEncoding, | ||
| public bool $bodyTruncated, | ||
| public ?Error $error, | ||
| ) {} | ||
|
|
||
| public static function fromArray(array $data): static | ||
| { | ||
| $headers = $data['requestHeaders'] ?? null; | ||
| if ($headers !== null && ! \is_array($headers)) { | ||
| throw new ClientException('Invalid deployment response: requestHeaders must be an object.'); | ||
| } | ||
|
|
||
| /** @var array<string, list<string>>|null $headers */ | ||
| return new self( | ||
| deploymentId: Data::string($data, 'deploymentId', 'deployment response'), | ||
| invocationId: Data::string($data, 'invocationId', 'deployment response'), | ||
| requestMethod: Data::string($data, 'requestMethod', 'deployment response'), | ||
| requestPath: Data::string($data, 'requestPath', 'deployment response'), | ||
| requestPathTruncated: Data::bool($data, 'requestPathTruncated', 'deployment response'), | ||
| requestHeaders: $headers, | ||
| requestHeadersTruncated: Data::bool($data, 'requestHeadersTruncated', 'deployment response'), | ||
| durationSeconds: Data::optionalFloat($data, 'durationSeconds', 'deployment response'), | ||
| statusCode: Data::optionalInt($data, 'statusCode', 'deployment response'), | ||
| body: Data::optionalString($data, 'body', 'deployment response'), | ||
| bodyEncoding: Data::optionalString($data, 'bodyEncoding', 'deployment response'), | ||
| bodyTruncated: Data::bool($data, 'bodyTruncated', 'deployment response'), | ||
| error: Error::fromData($data), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Enum\ErrorCode; | ||
| use OpenRuntimes\Orchestrator\Exception\ClientException; | ||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * The error carried by a failed callback: a stable code to branch on, and a | ||
| * sentence about this occurrence to show — never to parse. | ||
| */ | ||
| final readonly class Error | ||
| { | ||
| public function __construct( | ||
| public ErrorCode $code, | ||
| public string $message, | ||
| ) {} | ||
|
|
||
| /** | ||
| * The error a callback's data reports, or null when it reports success. | ||
| * | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| public static function fromData(array $data): ?self | ||
| { | ||
| if (! \array_key_exists('error', $data)) { | ||
| return null; | ||
| } | ||
| if (! \is_array($data['error'])) { | ||
| throw new ClientException('Invalid callback error: must be an object.'); | ||
| } | ||
|
|
||
| /** @var ErrorCode $code */ | ||
| $code = Data::enum($data['error'], 'code', ErrorCode::class, 'callback error'); | ||
|
|
||
| return new self($code, Data::string($data['error'], 'message', 'callback error')); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * orchestrator.job.artifact — one artifact finished. On failure, $error says | ||
| * why; format and compression are what the artifact was sniffed to be, null | ||
| * when it was never read far enough to tell. | ||
| */ | ||
| final readonly class JobArtifact implements Callback | ||
| { | ||
| public function __construct( | ||
| public string $jobId, | ||
| public string $artifactId, | ||
| public string $artifactType, | ||
| public string $status, | ||
| public mixed $content, | ||
| public ?float $durationSeconds, | ||
| public ?string $format, | ||
| public ?string $compression, | ||
| public ?Error $error, | ||
| /** @var array<string, string> */ | ||
| public array $meta, | ||
| ) {} | ||
|
|
||
| public static function fromArray(array $data): static | ||
| { | ||
| return new self( | ||
| jobId: Data::string($data, 'jobId', 'job artifact'), | ||
| artifactId: Data::string($data, 'artifactId', 'job artifact'), | ||
| artifactType: Data::string($data, 'artifactType', 'job artifact'), | ||
| status: Data::string($data, 'status', 'job artifact'), | ||
| content: $data['content'] ?? null, | ||
| durationSeconds: Data::optionalFloat($data, 'durationSeconds', 'job artifact'), | ||
| format: Data::optionalString($data, 'format', 'job artifact'), | ||
| compression: Data::optionalString($data, 'compression', 'job artifact'), | ||
| error: Error::fromData($data), | ||
| meta: Data::stringMap($data, 'meta', 'job artifact'), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * orchestrator.job.complete — every post-job artifact has been processed, | ||
| * successfully or not. No more events follow for this job. | ||
| */ | ||
| final readonly class JobComplete implements Callback | ||
| { | ||
| public function __construct( | ||
| public string $jobId, | ||
| /** @var array<string, string> */ | ||
| public array $meta, | ||
| ) {} | ||
|
|
||
| public static function fromArray(array $data): static | ||
| { | ||
| return new self( | ||
| jobId: Data::string($data, 'jobId', 'job complete'), | ||
| meta: Data::stringMap($data, 'meta', 'job complete'), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * orchestrator.job.exit — the worker exited. exitCode is -1 when the job failed | ||
| * before the worker could run; reason is the backend's own detail ("oom") and | ||
| * null when it has nothing to add beyond the code. | ||
| */ | ||
| final readonly class JobExit implements Callback | ||
| { | ||
| public function __construct( | ||
| public string $jobId, | ||
| public int $exitCode, | ||
| public ?string $reason, | ||
| public string $image, | ||
| public ?float $durationSeconds, | ||
| public ?Error $error, | ||
| /** @var array<string, string> */ | ||
| public array $meta, | ||
| ) {} | ||
|
|
||
| public static function fromArray(array $data): static | ||
| { | ||
| return new self( | ||
| jobId: Data::string($data, 'jobId', 'job exit'), | ||
| exitCode: Data::int($data, 'exitCode', 'job exit'), | ||
| reason: Data::optionalString($data, 'reason', 'job exit'), | ||
| image: Data::string($data, 'image', 'job exit'), | ||
| durationSeconds: Data::optionalFloat($data, 'durationSeconds', 'job exit'), | ||
| error: Error::fromData($data), | ||
| meta: Data::stringMap($data, 'meta', 'job exit'), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenRuntimes\Orchestrator\Callback; | ||
|
|
||
| use OpenRuntimes\Orchestrator\Model\Data; | ||
|
|
||
| /** | ||
| * orchestrator.job.log — a batch of stdout or stderr lines. | ||
| */ | ||
| final readonly class JobLog implements Callback | ||
| { | ||
| public function __construct( | ||
| public string $jobId, | ||
| /** @var list<string> */ | ||
| public array $lines, | ||
| public string $stream, | ||
| /** @var array<string, string> */ | ||
| public array $meta, | ||
| ) {} | ||
|
|
||
| public static function fromArray(array $data): static | ||
| { | ||
| return new self( | ||
| jobId: Data::string($data, 'jobId', 'job log'), | ||
| lines: Data::strings($data, 'lines', 'job log'), | ||
| stream: Data::string($data, 'stream', 'job log'), | ||
| meta: Data::stringMap($data, 'meta', 'job log'), | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.