Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
CopyJobQueue
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
3 / 3
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
5
 copyJobs
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
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\JobQueue\JobQueue;
25use MediaWiki\Maintenance\Maintenance;
26use MediaWiki\WikiMap\WikiMap;
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
32/**
33 * Copy all jobs from one job queue system to another.
34 * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
35 * which is a map of queue system names to JobQueue::factory() parameters.
36 * The parameters should not have wiki or type settings and thus partial.
37 *
38 * @ingroup Maintenance
39 */
40class CopyJobQueue extends Maintenance {
41    public function __construct() {
42        parent::__construct();
43        $this->addDescription( 'Copy jobs from one queue system to another.' );
44        $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
45        $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
46        $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
47        $this->setBatchSize( 500 );
48    }
49
50    public function execute() {
51        global $wgJobQueueMigrationConfig;
52
53        $srcKey = $this->getOption( 'src' );
54        $dstKey = $this->getOption( 'dst' );
55
56        if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
57            $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
58        } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
59            $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
60        }
61
62        $types = ( $this->getOption( 'type' ) === 'all' )
63            ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
64            : [ $this->getOption( 'type' ) ];
65
66        $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
67        foreach ( $types as $type ) {
68            $baseConfig = [ 'type' => $type, 'domain' => $dbDomain ];
69            $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
70            $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
71
72            [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
73            $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
74
75            [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
76            $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
77        }
78    }
79
80    protected function copyJobs( JobQueue $src, JobQueue $dst, iterable $jobs ): array {
81        $total = 0;
82        $totalOK = 0;
83        $batch = [];
84        foreach ( $jobs as $job ) {
85            ++$total;
86            $batch[] = $job;
87            if ( count( $batch ) >= $this->getBatchSize() ) {
88                $dst->push( $batch );
89                $totalOK += count( $batch );
90                $batch = [];
91                $dst->waitForBackups();
92            }
93        }
94        if ( count( $batch ) ) {
95            $dst->push( $batch );
96            $totalOK += count( $batch );
97            $dst->waitForBackups();
98        }
99
100        return [ $total, $totalOK ];
101    }
102}
103
104// @codeCoverageIgnoreStart
105$maintClass = CopyJobQueue::class;
106require_once RUN_MAINTENANCE_IF_MAIN;
107// @codeCoverageIgnoreEnd