summaryrefslogtreecommitdiff
path: root/maintenance/copyJobQueue.php
blob: e833115ba9422d30dbdedc33d52fd16ee6c897c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
 * Copy all jobs from one job queue system to another.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

require_once __DIR__ . '/Maintenance.php';

/**
 * Copy all jobs from one job queue system to another.
 * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
 * which is a map of queue system names to JobQueue::factory() parameters.
 * The parameters should not have wiki or type settings and thus partial.
 *
 * @ingroup Maintenance
 */
class CopyJobQueue extends Maintenance {
	public function __construct() {
		parent::__construct();
		$this->mDescription = "Copy jobs from one queue system to another.";
		$this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
		$this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
		$this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
		$this->setBatchSize( 500 );
	}

	public function execute() {
		global $wgJobQueueMigrationConfig;

		$srcKey = $this->getOption( 'src' );
		$dstKey = $this->getOption( 'dst' );

		if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
			$this->error( "\$wgJobQueueMigrationConfig not set for '$srcKey'.", 1 );
		} elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
			$this->error( "\$wgJobQueueMigrationConfig not set for '$dstKey'.", 1 );
		}

		$types = ( $this->getOption( 'type' ) === 'all' )
			? JobQueueGroup::singleton()->getQueueTypes()
			: array( $this->getOption( 'type' ) );

		foreach ( $types as $type ) {
			$baseConfig = array( 'type' => $type, 'wiki' => wfWikiID() );
			$src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
			$dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );

			list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
			$this->output( "Copied $totalOK/$total queued $type jobs.\n" );

			list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
			$this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
		}
	}

	protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
		$total = 0;
		$totalOK = 0;
		$batch = array();
		foreach ( $jobs as $job ) {
			++$total;
			$batch[] = $job;
			if ( count( $batch ) >= $this->mBatchSize ) {
				if ( $dst->push( $batch ) ) {
					$totalOK += count( $batch );
				}
				$batch = array();
				$dst->waitForBackups();
			}
		}
		if ( count( $batch ) ) {
			if ( $dst->push( $batch ) ) {
				$totalOK += count( $batch );
			}
			$dst->waitForBackups();
		}
		return array( $total, $totalOK );
	}
}

$maintClass = 'CopyJobQueue';
require_once RUN_MAINTENANCE_IF_MAIN;