MediaWiki  master
copyJobQueue.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
36 class CopyJobQueue extends Maintenance {
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Copy jobs from one queue system to another.' );
40  $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
41  $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
42  $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
43  $this->setBatchSize( 500 );
44  }
45 
46  public function execute() {
47  global $wgJobQueueMigrationConfig;
48 
49  $srcKey = $this->getOption( 'src' );
50  $dstKey = $this->getOption( 'dst' );
51 
52  if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
53  $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
54  } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
55  $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
56  }
57 
58  $types = ( $this->getOption( 'type' ) === 'all' )
59  ? MediaWikiServices::getInstance()->getJobQueueGroup()->getQueueTypes()
60  : [ $this->getOption( 'type' ) ];
61 
62  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
63  foreach ( $types as $type ) {
64  $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
65  $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
66  $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
67 
68  list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
69  $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
70 
71  list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
72  $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
73  }
74  }
75 
76  protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
77  $total = 0;
78  $totalOK = 0;
79  $batch = [];
80  foreach ( $jobs as $job ) {
81  ++$total;
82  $batch[] = $job;
83  if ( count( $batch ) >= $this->getBatchSize() ) {
84  $dst->push( $batch );
85  $totalOK += count( $batch );
86  $batch = [];
87  $dst->waitForBackups();
88  }
89  }
90  if ( count( $batch ) ) {
91  $dst->push( $batch );
92  $totalOK += count( $batch );
93  $dst->waitForBackups();
94  }
95 
96  return [ $total, $totalOK ];
97  }
98 }
99 
100 $maintClass = CopyJobQueue::class;
101 require_once RUN_MAINTENANCE_IF_MAIN;
Copy all jobs from one job queue system to another.
execute()
Do the actual work.
copyJobs(JobQueue $src, JobQueue $dst, $jobs)
__construct()
Default constructor.
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:36
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:596
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition: JobQueue.php:346
static factory(array $params)
Get a job queue object of the specified type.
Definition: JobQueue.php:137
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
output( $out, $channel=null)
Throw some output to the user.
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.
Service locator for MediaWiki core services.
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
$maintClass
if(count( $args)< 1) $job