You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.6 KiB
PHP
97 lines
3.6 KiB
PHP
<?php
|
|
namespace App\Models;
|
|
final class CloudJob extends Model
|
|
{
|
|
public const DISPATCH_DEADLINE_SECONDS = 300;
|
|
|
|
protected $connection = 'default';
|
|
protected $table = 'cloud_jobs';
|
|
protected $hidden = ['sealed_payload', 'lease_token'];
|
|
protected $casts = [
|
|
'backup_id' => 'int',
|
|
'backup_restore_id' => 'int',
|
|
'backup_recovery_id' => 'int',
|
|
'vpc_id' => 'int',
|
|
'vpc_port_id' => 'int',
|
|
'vpc_batch_id' => 'int',
|
|
'supersedes_job_id' => 'int',
|
|
'vpc_repair_root_job_id' => 'int',
|
|
];
|
|
|
|
/**
|
|
* The application intentionally does not install illuminate/events, so
|
|
* model creating observers do not run. Enforce the insert invariant from
|
|
* save() itself instead of depending on an inactive event dispatcher.
|
|
*/
|
|
public function save(array $options = [])
|
|
{
|
|
if (!$this->exists) {
|
|
$this->initializeForCreate();
|
|
}
|
|
return parent::save($options);
|
|
}
|
|
|
|
public function initializeForCreate(): void
|
|
{
|
|
$job = $this;
|
|
$now = time();
|
|
if (!in_array((string) ($job->execution_mode ?? ''),
|
|
['agent', 'local', 'aggregate'], true)) {
|
|
throw new \LogicException(
|
|
'Cloud job execution_mode must be explicit'
|
|
);
|
|
}
|
|
if (!is_string($job->operation_id)
|
|
|| preg_match('/^[a-f0-9]{32}$/D', $job->operation_id) !== 1) {
|
|
$job->operation_id = bin2hex(random_bytes(16));
|
|
}
|
|
if (!is_string($job->request_idempotency_key)
|
|
|| preg_match('/^[A-Za-z0-9_.-]{16,128}$/D',
|
|
$job->request_idempotency_key) !== 1) {
|
|
$job->request_idempotency_key = $job->operation_id;
|
|
}
|
|
if (!is_string($job->request_fingerprint)
|
|
|| preg_match('/^[a-f0-9]{64}$/D', $job->request_fingerprint)
|
|
!== 1) {
|
|
$job->request_fingerprint = hash('sha256', implode('|', [
|
|
(string) $job->kind,
|
|
(string) ($job->user_id ?? ''),
|
|
(string) ($job->host_id ?? ''),
|
|
(string) ($job->instance_id ?? ''),
|
|
(string) ($job->invoice_id ?? ''),
|
|
(string) ($job->order_id ?? ''),
|
|
(string) ($job->snapshot_id ?? ''),
|
|
]));
|
|
}
|
|
$aggregate = (string) $job->execution_mode === 'aggregate';
|
|
if ($aggregate && (!(is_int($job->receipt_deadline_at)
|
|
|| (is_string($job->receipt_deadline_at)
|
|
&& preg_match('/^[1-9][0-9]*$/D',
|
|
$job->receipt_deadline_at) === 1))
|
|
|| (int) $job->receipt_deadline_at <= $now)) {
|
|
throw new \LogicException(
|
|
'Aggregate cloud job requires an absolute receipt deadline'
|
|
);
|
|
}
|
|
$job->phase = $aggregate ? 'awaiting_receipt' : 'accepted';
|
|
$job->outcome = null;
|
|
$job->status = $aggregate ? 'running' : 'queued';
|
|
$job->attempts = 0;
|
|
$job->dispatch_attempts = 0;
|
|
$job->dispatch_deadline_at = $job->dispatch_deadline_at
|
|
?: $now + self::DISPATCH_DEADLINE_SECONDS;
|
|
$job->dispatch_started_at = null;
|
|
$job->agent_accepted_at = null;
|
|
$job->receipt_deadline_at = $aggregate
|
|
? (int) $job->receipt_deadline_at : null;
|
|
$job->terminal_at = null;
|
|
$job->effect_may_have_applied = null;
|
|
$job->safe_to_resubmit = null;
|
|
$job->effect_resolution = 'not_applicable';
|
|
$job->finalization_incident = 0;
|
|
$job->finalization_error_code = null;
|
|
$job->completion_event_id = null;
|
|
$job->read_at = null;
|
|
}
|
|
}
|