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