Debian Codegraph @

maintenance/manageJobs.php

raw symbols SCIP
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
24 // @codeCoverageIgnoreStart
25 require_once __DIR__ . '/Maintenance.php';
26 // @codeCoverageIgnoreEnd
27
28 /**
29 * Maintenance script that handles managing job queue admin tasks (re-push, delete, ...)
30 *
31 * @ingroup Maintenance
32 */
33 class 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 = $this->getServiceContainer()->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 // @codeCoverageIgnoreStart
100 $maintClass = ManageJobs::class;
101 require_once RUN_MAINTENANCE_IF_MAIN;
102 // @codeCoverageIgnoreEnd