client = $client; $this->results = $results; $this->pairing = $pairing; } public function register_hooks(): void { add_action( self::CRON_HOOK, array( $this, 'poll' ), 10, 2 ); } public function start( RunSummary $summary ): void { $this->results->save( $summary ); if ( ! $summary->is_terminal() ) { $this->schedule( $summary->state->public_id, 0, $summary->next_polling_delay ); } } public function poll( string $public_id, int $attempt = 0 ): void { if ( ! $this->pairing->is_connected() || $attempt >= self::MAX_POLLS ) { return; } try { $summary = $this->client->status( $public_id ); $this->results->save( $summary ); if ( ! $summary->is_terminal() ) { $this->schedule( $public_id, $attempt + 1, $summary->next_polling_delay ); } } catch ( ApiException $exception ) { $this->results->mark_error( $exception->error_code() ); if ( ApiException::REMOTE_FETCH === $exception->error_code() ) { $this->schedule( $public_id, $attempt + 1, self::RETRY_SECONDS ); } } } private function schedule( string $public_id, int $attempt, int $delay ): void { if ( $attempt >= self::MAX_POLLS ) { return; } wp_schedule_single_event( time() + max( 5, min( 300, $delay ) ), self::CRON_HOOK, array( $public_id, $attempt ) ); } }