Merge "Print chained exceptions when maintenance script fails."
[lhc/web/wiklou.git] / maintenance / copyJobQueue.php
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 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->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
52 } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
53 $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
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->getBatchSize() ) {
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::class;
98 require_once RUN_MAINTENANCE_IF_MAIN;