Localisation updates from https://translatewiki.net.
[mediawiki.git] / maintenance / manageJobs.php
blobee8e0fb9ee69fd4d25daeaa638236b0d55954481
1 <?php
2 /**
3 * Maintenance script that handles managing job queue admin tasks
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance;
26 // @codeCoverageIgnoreStart
27 require_once __DIR__ . '/Maintenance.php';
28 // @codeCoverageIgnoreEnd
30 /**
31 * Maintenance script that handles managing job queue admin tasks (re-push, delete, ...)
33 * @ingroup Maintenance
35 class 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 );
44 public function execute() {
45 $type = $this->getOption( 'type' );
46 $action = $this->getOption( 'action' );
48 $group = $this->getServiceContainer()->getJobQueueGroup();
49 $queue = $group->get( $type );
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'." );
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" );
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() );
70 $now = wfTimestampNow();
71 $lastRepushTime = $cache->get( $key );
72 if ( $lastRepushTime === false ) {
73 $lastRepushTime = wfTimestamp( TS_MW, 1 ); // include all jobs
76 $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
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
87 $queue->push( $job );
88 ++$count;
90 if ( ( $count % $this->getBatchSize() ) == 0 ) {
91 $queue->waitForBackups();
95 $cache->set( $key, $now ); // next run will ignore these jobs
97 $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
101 // @codeCoverageIgnoreStart
102 $maintClass = ManageJobs::class;
103 require_once RUN_MAINTENANCE_IF_MAIN;
104 // @codeCoverageIgnoreEnd