MediaWiki master
copyJobQueue.php
Go to the documentation of this file.
1<?php
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Copy jobs from one queue system to another.' );
43 $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
44 $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
45 $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
46 $this->setBatchSize( 500 );
47 }
48
49 public function execute() {
50 global $wgJobQueueMigrationConfig;
51
52 $srcKey = $this->getOption( 'src' );
53 $dstKey = $this->getOption( 'dst' );
54
55 if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
56 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
57 } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
58 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
59 }
60
61 $types = ( $this->getOption( 'type' ) === 'all' )
62 ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
63 : [ $this->getOption( 'type' ) ];
64
65 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
66 foreach ( $types as $type ) {
67 $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
68 $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
69 $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
70
71 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
72 $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
73
74 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
75 $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
76 }
77 }
78
79 protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
80 $total = 0;
81 $totalOK = 0;
82 $batch = [];
83 foreach ( $jobs as $job ) {
84 ++$total;
85 $batch[] = $job;
86 if ( count( $batch ) >= $this->getBatchSize() ) {
87 $dst->push( $batch );
88 $totalOK += count( $batch );
89 $batch = [];
90 $dst->waitForBackups();
91 }
92 }
93 if ( count( $batch ) ) {
94 $dst->push( $batch );
95 $totalOK += count( $batch );
96 $dst->waitForBackups();
97 }
98
99 return [ $total, $totalOK ];
100 }
101}
102
103// @codeCoverageIgnoreStart
104$maintClass = CopyJobQueue::class;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd
Copy all jobs from one job queue system to another.
execute()
Do the actual work.
copyJobs(JobQueue $src, JobQueue $dst, $jobs)
__construct()
Default constructor.
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:45
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:603
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:352
static factory(array $params)
Get a job queue object of the specified type.
Definition JobQueue.php:152
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:31
$maintClass
if(count( $args)< 1) $job