MediaWiki  master
copyJobQueue.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
34 class CopyJobQueue extends Maintenance {
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Copy jobs from one queue system to another.' );
38  $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
39  $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
40  $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
41  $this->setBatchSize( 500 );
42  }
43 
44  public function execute() {
45  global $wgJobQueueMigrationConfig;
46 
47  $srcKey = $this->getOption( 'src' );
48  $dstKey = $this->getOption( 'dst' );
49 
50  if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
51  $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
52  } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
53  $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
54  }
55 
56  $types = ( $this->getOption( 'type' ) === 'all' )
57  ? JobQueueGroup::singleton()->getQueueTypes()
58  : [ $this->getOption( 'type' ) ];
59 
60  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
61  foreach ( $types as $type ) {
62  $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
63  $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
64  $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
65 
66  list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
67  $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
68 
69  list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
70  $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
71  }
72  }
73 
74  protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
75  $total = 0;
76  $totalOK = 0;
77  $batch = [];
78  foreach ( $jobs as $job ) {
79  ++$total;
80  $batch[] = $job;
81  if ( count( $batch ) >= $this->getBatchSize() ) {
82  $dst->push( $batch );
83  $totalOK += count( $batch );
84  $batch = [];
85  $dst->waitForBackups();
86  }
87  }
88  if ( count( $batch ) ) {
89  $dst->push( $batch );
90  $totalOK += count( $batch );
91  $dst->waitForBackups();
92  }
93 
94  return [ $total, $totalOK ];
95  }
96 }
97 
98 $maintClass = CopyJobQueue::class;
99 require_once RUN_MAINTENANCE_IF_MAIN;
Copy all jobs from one job queue system to another.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
setBatchSize( $s=0)
Set the batch size.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition: JobQueue.php:318
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
copyJobs(JobQueue $src, JobQueue $dst, $jobs)
static factory(array $params)
Get a job queue object of the specified type.
Definition: JobQueue.php:114
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:31
if(count( $args)< 1) $job
getBatchSize()
Returns batch size.
$maintClass
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static singleton( $domain=false)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:570