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; } }