Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / maintenance / copyJobQueue.php
blobe1d697d88de91a7dd5963424acd716215ebd78ef
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 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Copy all jobs from one job queue system to another.
28 * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
29 * which is a map of queue system names to JobQueue::factory() parameters.
30 * The parameters should not have wiki or type settings and thus partial.
32 * @ingroup Maintenance
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 );
44 public function execute() {
45 global $wgJobQueueMigrationConfig;
47 $srcKey = $this->getOption( 'src' );
48 $dstKey = $this->getOption( 'dst' );
50 if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
51 $this->error( "\$wgJobQueueMigrationConfig not set for '$srcKey'.", 1 );
52 } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
53 $this->error( "\$wgJobQueueMigrationConfig not set for '$dstKey'.", 1 );
56 $types = ( $this->getOption( 'type' ) === 'all' )
57 ? JobQueueGroup::singleton()->getQueueTypes()
58 : [ $this->getOption( 'type' ) ];
60 foreach ( $types as $type ) {
61 $baseConfig = [ 'type' => $type, 'wiki' => wfWikiID() ];
62 $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
63 $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
65 list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
66 $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
68 list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
69 $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
73 protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
74 $total = 0;
75 $totalOK = 0;
76 $batch = [];
77 foreach ( $jobs as $job ) {
78 ++$total;
79 $batch[] = $job;
80 if ( count( $batch ) >= $this->mBatchSize ) {
81 $dst->push( $batch );
82 $totalOK += count( $batch );
83 $batch = [];
84 $dst->waitForBackups();
87 if ( count( $batch ) ) {
88 $dst->push( $batch );
89 $totalOK += count( $batch );
90 $dst->waitForBackups();
93 return [ $total, $totalOK ];
97 $maintClass = 'CopyJobQueue';
98 require_once RUN_MAINTENANCE_IF_MAIN;