CopyJobQueue::execute()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 12
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 24.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
require_once __DIR__ . '/Maintenance.php';
25
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.
31
 *
32
 * @ingroup Maintenance
33
 */
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 );
42
	}
43
44
	public function execute() {
45
		global $wgJobQueueMigrationConfig;
46
47
		$srcKey = $this->getOption( 'src' );
48
		$dstKey = $this->getOption( 'dst' );
49
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 );
54
		}
55
56
		$types = ( $this->getOption( 'type' ) === 'all' )
57
			? JobQueueGroup::singleton()->getQueueTypes()
58
			: [ $this->getOption( 'type' ) ];
59
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] );
64
65
			list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
66
			$this->output( "Copied $totalOK/$total queued $type jobs.\n" );
67
68
			list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
69
			$this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
70
		}
71
	}
72
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();
85
			}
86
		}
87
		if ( count( $batch ) ) {
88
			$dst->push( $batch );
89
			$totalOK += count( $batch );
90
			$dst->waitForBackups();
91
		}
92
93
		return [ $total, $totalOK ];
94
	}
95
}
96
97
$maintClass = 'CopyJobQueue';
98
require_once RUN_MAINTENANCE_IF_MAIN;
99