MediaWiki master
copyJobQueue.php
Go to the documentation of this file.
1<?php
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
27 public function __construct() {
28 parent::__construct();
29 $this->addDescription( 'Copy jobs from one queue system to another.' );
30 $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
31 $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
32 $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
33 $this->setBatchSize( 500 );
34 }
35
36 public function execute() {
37 global $wgJobQueueMigrationConfig;
38
39 $srcKey = $this->getOption( 'src' );
40 $dstKey = $this->getOption( 'dst' );
41
42 if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
43 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
44 } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
45 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
46 }
47
48 $types = ( $this->getOption( 'type' ) === 'all' )
49 ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
50 : [ $this->getOption( 'type' ) ];
51
52 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
53 foreach ( $types as $type ) {
54 $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
55 $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
56 $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
57
58 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
59 $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
60
61 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
62 $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
63 }
64 }
65
66 protected function copyJobs( JobQueue $src, JobQueue $dst, iterable $jobs ): array {
67 $total = 0;
68 $totalOK = 0;
69 $batch = [];
70 foreach ( $jobs as $job ) {
71 ++$total;
72 $batch[] = $job;
73 if ( count( $batch ) >= $this->getBatchSize() ) {
74 $dst->push( $batch );
75 $totalOK += count( $batch );
76 $batch = [];
77 $dst->waitForBackups();
78 }
79 }
80 if ( count( $batch ) ) {
81 $dst->push( $batch );
82 $totalOK += count( $batch );
83 $dst->waitForBackups();
84 }
85
86 return [ $total, $totalOK ];
87 }
88}
89
90// @codeCoverageIgnoreStart
91$maintClass = CopyJobQueue::class;
92require_once RUN_MAINTENANCE_IF_MAIN;
93// @codeCoverageIgnoreEnd
Copy all jobs from one job queue system to another.
execute()
Do the actual work.
copyJobs(JobQueue $src, JobQueue $dst, iterable $jobs)
__construct()
Default constructor.
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:36
getAllQueuedJobs()
Get an iterator to traverse over all available jobs in this queue.
getAllDelayedJobs()
Get an iterator to traverse over all delayed jobs in this queue.
Definition JobQueue.php:642
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:342
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:594
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
$maintClass
if(count( $args)< 1) $job