Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManageJobs
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 delete
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 repushAbandoned
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * Maintenance script that handles managing job queue admin tasks
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\MediaWikiServices;
25
26require_once __DIR__ . '/Maintenance.php';
27
28/**
29 * Maintenance script that handles managing job queue admin tasks (re-push, delete, ...)
30 *
31 * @ingroup Maintenance
32 */
33class ManageJobs extends Maintenance {
34    public function __construct() {
35        parent::__construct();
36        $this->addDescription( 'Perform administrative tasks on a job queue' );
37        $this->addOption( 'type', 'Job type', true, true );
38        $this->addOption( 'action', 'Queue operation ("delete", "repush-abandoned")', true, true );
39        $this->setBatchSize( 100 );
40    }
41
42    public function execute() {
43        $type = $this->getOption( 'type' );
44        $action = $this->getOption( 'action' );
45
46        $group = $this->getServiceContainer()->getJobQueueGroup();
47        $queue = $group->get( $type );
48
49        if ( $action === 'delete' ) {
50            $this->delete( $queue );
51        } elseif ( $action === 'repush-abandoned' ) {
52            $this->repushAbandoned( $queue );
53        } else {
54            $this->fatalError( "Invalid action '$action'." );
55        }
56    }
57
58    private function delete( JobQueue $queue ) {
59        $this->output( "Queue has {$queue->getSize()} job(s); deleting...\n" );
60        $queue->delete();
61        $this->output( "Done; current size is {$queue->getSize()} job(s).\n" );
62    }
63
64    private function repushAbandoned( JobQueue $queue ) {
65        $cache = MediaWikiServices::getInstance()->getObjectCacheFactory()->getInstance( CACHE_DB );
66        $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getDomain(), $queue->getType() );
67
68        $now = wfTimestampNow();
69        $lastRepushTime = $cache->get( $key );
70        if ( $lastRepushTime === false ) {
71            $lastRepushTime = wfTimestamp( TS_MW, 1 ); // include all jobs
72        }
73
74        $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
75
76        $count = 0;
77        $skipped = 0;
78        foreach ( $queue->getAllAbandonedJobs() as $job ) {
79            /** @var Job $job */
80            if ( $job instanceof Job && $job->getQueuedTimestamp() < wfTimestamp( TS_UNIX, $lastRepushTime ) ) {
81                ++$skipped;
82                continue; // already re-pushed in prior round
83            }
84
85            $queue->push( $job );
86            ++$count;
87
88            if ( ( $count % $this->getBatchSize() ) == 0 ) {
89                $queue->waitForBackups();
90            }
91        }
92
93        $cache->set( $key, $now ); // next run will ignore these jobs
94
95        $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
96    }
97}
98
99$maintClass = ManageJobs::class;
100require_once RUN_MAINTENANCE_IF_MAIN;