MediaWiki  1.34.0
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;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
JobQueue\push
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition: JobQueue.php:318
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
CopyJobQueue\copyJobs
copyJobs(JobQueue $src, JobQueue $dst, $jobs)
Definition: copyJobQueue.php:74
CopyJobQueue
Copy all jobs from one job queue system to another.
Definition: copyJobQueue.php:34
CopyJobQueue\__construct
__construct()
Default constructor.
Definition: copyJobQueue.php:35
JobQueue\waitForBackups
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:570
JobQueue\factory
static factory(array $params)
Get a job queue object of the specified type.
Definition: JobQueue.php:114
$maintClass
$maintClass
Definition: copyJobQueue.php:98
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
JobQueue
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:31
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
CopyJobQueue\execute
execute()
Do the actual work.
Definition: copyJobQueue.php:44
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:394
$type
$type
Definition: testCompression.php:48