*/ private $pipelines; /** * @param array $pipelines Keyed by job type. */ public function __construct( JobStore $store, array $pipelines ) { $this->store = $store; $this->pipelines = $pipelines; } public function tick( JobState $job ): JobState { if ( $job->isTerminal() ) { return $job; } $lock = $this->store->acquire( $job ); if ( null === $lock ) { // A tick for this job is already running (double click, retried request). // Report its latest committed state and ask the pump to come back shortly. $current = $this->store->load( $job->id() ) ?? $job; $current->setDelay( 750 ); return $current; } try { // Reload under the lock: the copy we were handed may predate the previous tick. $fresh = $this->store->load( $job->id() ); return null === $fresh ? $job : $this->run( $fresh ); } finally { $this->store->release( $lock ); } } /** * Cancels immediately when no tick is running, otherwise flags the running tick * to stop at its next safe point. */ public function cancel( JobState $job ): JobState { if ( $job->isTerminal() ) { return $job; } // Once the new database is live, cancelling would leave it running with the old // code. The only way out is forward. if ( $job->get( 'swapped' ) ) { return $job; } $this->store->requestCancel( $job ); $lock = $this->store->acquire( $job ); if ( null === $lock ) { $job->setLabel( __( 'Cancelling…', 'migratex' ) ); return $job; } try { $fresh = $this->store->load( $job->id() ) ?? $job; return $this->finishCancel( $fresh ); } finally { $this->store->release( $lock ); } } private function run( JobState $job ): JobState { if ( $job->isTerminal() ) { return $job; } if ( $this->store->cancelRequested( $job ) && ! $job->get( 'swapped' ) ) { return $this->finishCancel( $job ); } $pipeline = $this->pipelines[ $job->type() ] ?? null; if ( null === $pipeline ) { $job->fail( 'om_unknown_type', __( 'Unknown job type.', 'migratex' ) ); $this->store->save( $job ); return $job; } $stages = array(); foreach ( $pipeline->stages() as $stage ) { $stages[ $stage->name() ] = $stage; } $order = array_keys( $stages ); if ( '' === $job->stage() ) { $job->advanceTo( $order[0] ); } $budget = Budget::forRequest(); $budget->onCommit( function () use ( $job ): void { $this->store->save( $job ); } ); // From inside a long tick, say what is happening at most once a second: the progress screen reads // this while the tick is still running, so the work looks like work and not a frozen bar. $budget->onHeartbeat( function () use ( $job, $stages ): void { Progress::publish( $job, $this->overallProgress( $stages, $job ) ); } ); $announced = ''; $job->incrementTick(); $job->setDelay( 0 ); while ( true ) { $stage = $stages[ $job->stage() ] ?? null; if ( null === $stage ) { $job->fail( 'om_unknown_stage', sprintf( 'Unknown stage "%s".', $job->stage() ) ); break; } $job->setLabel( $stage->label() ); if ( $announced !== $job->stage() ) { $announced = $job->stage(); Progress::publish( $job, $this->overallProgress( $stages, $job ), true, sprintf( /* translators: %s: step name */ __( 'Started: %s', 'migratex' ), $stage->label() ) ); } try { $result = $stage->run( $job, $budget ); } catch ( \Throwable $e ) { Logger::log( $job->id(), sprintf( 'Stage %s threw %s: %s (%s:%d)', $stage->name(), get_class( $e ), $e->getMessage(), basename( $e->getFile() ), $e->getLine() ) ); $result = StageResult::fail( 'om_stage_exception', $e->getMessage() ); } if ( StageResult::FAIL === $result->kind() ) { Logger::log( $job->id(), sprintf( 'Stage %s failed: %s', $stage->name(), $result->message() ) ); $job->fail( $result->code(), $result->message() ); break; } if ( StageResult::WAIT === $result->kind() ) { break; } if ( StageResult::NEXT === $result->kind() ) { $position = array_search( $stage->name(), $order, true ); $next = $order[ $position + 1 ] ?? null; if ( null === $next ) { $job->complete(); Progress::publish( $job, 100.0, true, __( 'Complete.', 'migratex' ) ); break; } $job->advanceTo( $next ); // End the tick at every stage boundary. The response then names the stage // that is about to run, so the progress screen shows each step even when a // small site would otherwise finish everything inside one request. $job->setLabel( $stages[ $next ]->label() ); $job->setOverallProgress( $this->overallProgress( $stages, $job ) ); $this->store->save( $job ); Progress::publish( $job, $job->progress(), true ); return $job; } else { $job->setDelay( $result->delayMs() ); } $job->setOverallProgress( $this->overallProgress( $stages, $job ) ); // Commit after every unit so a crash costs at most one batch. $this->store->save( $job ); Progress::publish( $job, $job->progress(), true ); if ( $this->store->cancelRequested( $job ) && ! $job->get( 'swapped' ) ) { return $this->finishCancel( $job ); } if ( $budget->exhausted() ) { break; } } if ( JobState::FAILED === $job->status() ) { $pipeline->abort( $job ); $info = $job->errorInfo(); Progress::publish( $job, $job->progress(), true, sprintf( /* translators: %s: reason */ __( 'Failed: %s', 'migratex' ), null === $info ? '' : $info['message'] ) ); } elseif ( ! $job->isTerminal() ) { $job->setOverallProgress( $this->overallProgress( $stages, $job ) ); Progress::publish( $job, $job->progress(), true ); } $this->store->save( $job ); return $job; } private function finishCancel( JobState $job ): JobState { $pipeline = $this->pipelines[ $job->type() ] ?? null; if ( null !== $pipeline ) { try { $pipeline->abort( $job ); } catch ( \Throwable $e ) { Logger::log( $job->id(), 'Abort cleanup failed: ' . $e->getMessage() ); } } try { $this->store->clearWorkspace( $job ); } catch ( \Throwable $e ) { Logger::log( $job->id(), 'Workspace cleanup failed: ' . $e->getMessage() ); } $job->cancel(); $job->setLabel( __( 'Cancelled', 'migratex' ) ); $this->store->save( $job ); Progress::publish( $job, $job->progress(), true, __( 'Cancelled.', 'migratex' ) ); return $job; } /** * @param array $stages */ private function overallProgress( array $stages, JobState $job ): float { $total = 0; foreach ( $stages as $stage ) { $total += $stage->weight(); } if ( $total < 1 ) { return 0.0; } $done = 0.0; foreach ( $stages as $name => $stage ) { if ( $name === $job->stage() ) { $done += $stage->weight() * $job->stageProgress(); break; } $done += $stage->weight(); } // Never show 100% before the job is actually complete. return min( 99.0, $done / $total * 100 ); } }