Merge "docs: Fix typo"
[mediawiki.git] / maintenance / copyJobQueue.php
blobb67e2eacc525b303b77e3f1b156c71a709bd087d
1 <?php
2 /**
3 * Copy all jobs from one job queue system to another.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance;
25 use MediaWiki\WikiMap\WikiMap;
27 // @codeCoverageIgnoreStart
28 require_once __DIR__ . '/Maintenance.php';
29 // @codeCoverageIgnoreEnd
31 /**
32 * Copy all jobs from one job queue system to another.
33 * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
34 * which is a map of queue system names to JobQueue::factory() parameters.
35 * The parameters should not have wiki or type settings and thus partial.
37 * @ingroup Maintenance
39 class CopyJobQueue extends Maintenance {
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 );
49 public function execute() {
50 global $wgJobQueueMigrationConfig;
52 $srcKey = $this->getOption( 'src' );
53 $dstKey = $this->getOption( 'dst' );
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'." );
61 $types = ( $this->getOption( 'type' ) === 'all' )
62 ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
63 : [ $this->getOption( 'type' ) ];
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] );
71 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
72 $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
74 [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
75 $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
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();
93 if ( count( $batch ) ) {
94 $dst->push( $batch );
95 $totalOK += count( $batch );
96 $dst->waitForBackups();
99 return [ $total, $totalOK ];
103 // @codeCoverageIgnoreStart
104 $maintClass = CopyJobQueue::class;
105 require_once RUN_MAINTENANCE_IF_MAIN;
106 // @codeCoverageIgnoreEnd