*/ private $data = array(); /** @var array */ private $cursor = array(); /** @var array */ private $result = array(); /** @var float Overall progress, 0-100. */ private $progress = 0.0; /** @var float Progress of the current stage, 0-1. */ private $stage_progress = 0.0; /** @var string */ private $label = ''; /** @var array */ private $detail = array(); /** @var int */ private $tick = 0; /** @var array{code:string,message:string}|null */ private $error = null; /** @var int */ private $created; /** @var int */ private $updated; /** @var int Not persisted: how long the pump should wait before the next tick. */ private $delay_ms = 0; /** * @param array $data */ public static function create( string $id, string $type, int $user_id, array $data = array() ): self { $job = new self(); $job->id = $id; $job->type = $type; $job->user_id = $user_id; $job->data = $data; $job->created = time(); $job->updated = time(); // Imports begin by waiting for the browser to upload chunks; exports begin working at once. $job->stage = self::TYPE_IMPORT === $type ? 'upload' : ''; return $job; } /** * @param array $raw */ public static function fromArray( array $raw ): ?self { foreach ( array( 'id', 'type', 'user_id', 'status' ) as $required ) { if ( ! isset( $raw[ $required ] ) ) { return null; } } $job = new self(); $job->id = (string) $raw['id']; $job->type = (string) $raw['type']; $job->user_id = (int) $raw['user_id']; $job->status = (string) $raw['status']; $job->stage = (string) ( $raw['stage'] ?? '' ); $job->data = is_array( $raw['data'] ?? null ) ? $raw['data'] : array(); $job->cursor = is_array( $raw['cursor'] ?? null ) ? $raw['cursor'] : array(); $job->result = is_array( $raw['result'] ?? null ) ? $raw['result'] : array(); $job->progress = (float) ( $raw['progress'] ?? 0 ); $job->stage_progress = (float) ( $raw['stage_progress'] ?? 0 ); $job->label = (string) ( $raw['label'] ?? '' ); $job->detail = is_array( $raw['detail'] ?? null ) ? $raw['detail'] : array(); $job->tick = (int) ( $raw['tick'] ?? 0 ); $job->error = is_array( $raw['error'] ?? null ) ? $raw['error'] : null; $job->created = (int) ( $raw['created'] ?? time() ); $job->updated = (int) ( $raw['updated'] ?? time() ); return $job; } /** * Full state for state.json. * * @return array */ public function toStorage(): array { return array( 'id' => $this->id, 'type' => $this->type, 'user_id' => $this->user_id, 'stage' => $this->stage, 'status' => $this->status, 'data' => $this->data, 'cursor' => $this->cursor, 'result' => $this->result, 'progress' => $this->progress, 'stage_progress' => $this->stage_progress, 'label' => $this->label, 'detail' => $this->detail, 'tick' => $this->tick, 'error' => $this->error, 'created' => $this->created, 'updated' => $this->updated, ); } /** * What the browser may see. Deliberately omits data and cursor: they hold * server filesystem paths and database details. * * @return array */ public function toArray(): array { return array( 'id' => $this->id, 'type' => $this->type, 'stage' => $this->stage, 'status' => $this->status, 'progress' => round( $this->progress, 1 ), 'stage_progress' => round( $this->stage_progress, 3 ), 'cancellable' => ! $this->isTerminal() && ! ( $this->data['swapped'] ?? false ), // True once the restored database is live: from then on there is no way back. 'swapped' => (bool) ( $this->data['swapped'] ?? false ), // Which Reset Hub tool this is (one of five fixed words), so a reloaded page can say what finished. 'scope' => 'reset' === $this->type ? (string) ( $this->data['scope'] ?? '' ) : null, 'label' => $this->label, 'detail' => $this->detail, 'tick' => $this->tick, 'error' => $this->error, 'result' => $this->result, ); } /** * Tells the JS pump what to do next. * * @return array{action:string,delay_ms:int} */ public function nextHint(): array { if ( $this->isTerminal() ) { return array( 'action' => 'stop', 'delay_ms' => 0, ); } if ( self::TYPE_IMPORT === $this->type && 'upload' === $this->stage && empty( $this->data['remote_url'] ) ) { return array( 'action' => 'upload', 'delay_ms' => 0, ); } return array( 'action' => 'tick', 'delay_ms' => $this->delay_ms, ); } public function id(): string { return $this->id; } public function type(): string { return $this->type; } public function userId(): int { return $this->user_id; } public function stage(): string { return $this->stage; } public function status(): string { return $this->status; } public function isTerminal(): bool { return self::RUNNING !== $this->status; } public function tickCount(): int { return $this->tick; } public function updatedAt(): int { return $this->updated; } public function createdAt(): int { return $this->created; } public function errorInfo(): ?array { return $this->error; } /** * @param mixed $default * @return mixed */ public function get( string $key, $default = null ) { return array_key_exists( $key, $this->data ) ? $this->data[ $key ] : $default; } /** * @param mixed $value */ public function set( string $key, $value ): void { $this->data[ $key ] = $value; } /** * @param mixed $default * @return mixed */ public function cursor( string $key, $default = null ) { return array_key_exists( $key, $this->cursor ) ? $this->cursor[ $key ] : $default; } /** * @param mixed $value */ public function setCursor( string $key, $value ): void { $this->cursor[ $key ] = $value; } /** * @param array $result */ public function setResult( array $result ): void { $this->result = $result; } /** * @return array */ public function result(): array { return $this->result; } /** * Moves to another stage. The cursor belongs to the stage and starts empty. */ public function advanceTo( string $stage ): void { $this->stage = $stage; $this->cursor = array(); $this->stage_progress = 0.0; $this->detail = array(); } public function setStageProgress( float $fraction ): void { $this->stage_progress = max( 0.0, min( 1.0, $fraction ) ); } /** * What the current stage reports about its work: counts, the file or table in hand, bytes. * * @return array */ public function detail(): array { return $this->detail; } public function label(): string { return $this->label; } /** * Overall progress as last worked out, 0-100. */ public function progress(): float { return $this->progress; } public function stageProgress(): float { return $this->stage_progress; } public function setOverallProgress( float $percent ): void { $this->progress = max( 0.0, min( 100.0, $percent ) ); } public function setLabel( string $label ): void { $this->label = $label; } /** * @param array $detail */ public function setDetail( array $detail ): void { $this->detail = $detail; } public function setDelay( int $milliseconds ): void { $this->delay_ms = max( 0, $milliseconds ); } public function incrementTick(): void { ++$this->tick; } public function touch(): void { $this->updated = time(); } public function fail( string $code, string $message ): void { $this->status = self::FAILED; $this->error = array( 'code' => $code, 'message' => $message, ); } public function complete(): void { $this->status = self::COMPLETE; $this->progress = 100.0; $this->stage_progress = 1.0; $this->stage = 'complete'; $this->cursor = array(); } public function cancel(): void { $this->status = self::CANCELLED; } }