Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
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\Maintenance\Maintenance;
25use MediaWiki\WikiMap\WikiMap;
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
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.
36 *
37 * @ingroup Maintenance
38 */
39class 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 );
47    }
48
49    public function execute() {
50        global $wgJobQueueMigrationConfig;
51
52        $srcKey = $this->getOption( 'src' );
53        $dstKey = $this->getOption( 'dst' );
54
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'." );
59        }
60
61        $types = ( $this->getOption( 'type' ) === 'all' )
62            ? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes()
63            : [ $this->getOption( 'type' ) ];
64
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] );
70
71            [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
72            $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
73
74            [ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
75            $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
76        }
77    }
78
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();
91            }
92        }
93        if ( count( $batch ) ) {
94            $dst->push( $batch );
95            $totalOK += count( $batch );
96            $dst->waitForBackups();
97        }
98
99        return [ $total, $totalOK ];
100    }
101}
102
103// @codeCoverageIgnoreStart
104$maintClass = CopyJobQueue::class;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd