Debian Codegraph @

maintenance/copyJobQueue.php

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