MediaWiki master
copyJobQueue.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Copy jobs from one queue system to another.' );
42 $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
43 $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
44 $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
45 $this->setBatchSize( 500 );
46 }
47
48 public function execute() {
49 global $wgJobQueueMigrationConfig;
50
51 $srcKey = $this->getOption( 'src' );
52 $dstKey = $this->getOption( 'dst' );
53
54 if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
55 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
56 } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
57 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
58 }
59
60 $types = ( $this->getOption( 'type' ) === 'all' )
61 ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
62 : [ $this->getOption( 'type' ) ];
63
64 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
65 foreach ( $types as $type ) {
66 $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
67 $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
68 $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
69
70 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
71 $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
72
73 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
74 $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
75 }
76 }
77
78 protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
79 $total = 0;
80 $totalOK = 0;
81 $batch = [];
82 foreach ( $jobs as $job ) {
83 ++$total;
84 $batch[] = $job;
85 if ( count( $batch ) >= $this->getBatchSize() ) {
86 $dst->push( $batch );
87 $totalOK += count( $batch );
88 $batch = [];
89 $dst->waitForBackups();
90 }
91 }
92 if ( count( $batch ) ) {
93 $dst->push( $batch );
94 $totalOK += count( $batch );
95 $dst->waitForBackups();
96 }
97
98 return [ $total, $totalOK ];
99 }
100}
101
102// @codeCoverageIgnoreStart
103$maintClass = CopyJobQueue::class;
104require_once RUN_MAINTENANCE_IF_MAIN;
105// @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:43
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:601
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:350
static factory(array $params)
Get a job queue object of the specified type.
Definition JobQueue.php:150
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
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.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
$maintClass
if(count( $args)< 1) $job