Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CopyJobQueue
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 copyJobs
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Copy all jobs from one job queue system to another.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use MediaWiki\WikiMap\WikiMap;
25
26require_once __DIR__ . '/Maintenance.php';
27
28/**
29 * Copy all jobs from one job queue system to another.
30 * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
31 * which is a map of queue system names to JobQueue::factory() parameters.
32 * The parameters should not have wiki or type settings and thus partial.
33 *
34 * @ingroup Maintenance
35 */
36class 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            ? $this->getServiceContainer()->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            [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
69            $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
70
71            [ $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;
101require_once RUN_MAINTENANCE_IF_MAIN;