MediaWiki master
manageJobs.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
37class ManageJobs extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addDescription( 'Perform administrative tasks on a job queue' );
41 $this->addOption( 'type', 'Job type', true, true );
42 $this->addOption( 'action', 'Queue operation ("delete", "repush-abandoned")', true, true );
43 $this->setBatchSize( 100 );
44 }
45
46 public function execute() {
47 $type = $this->getOption( 'type' );
48 $action = $this->getOption( 'action' );
49
50 $group = $this->getServiceContainer()->getJobQueueGroup();
51 $queue = $group->get( $type );
52
53 if ( $action === 'delete' ) {
54 $this->delete( $queue );
55 } elseif ( $action === 'repush-abandoned' ) {
56 $this->repushAbandoned( $queue );
57 } else {
58 $this->fatalError( "Invalid action '$action'." );
59 }
60 }
61
62 private function delete( JobQueue $queue ) {
63 $this->output( "Queue has {$queue->getSize()} job(s); deleting...\n" );
64 $queue->delete();
65 $this->output( "Done; current size is {$queue->getSize()} job(s).\n" );
66 }
67
68 private function repushAbandoned( JobQueue $queue ) {
69 $cache = $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_DB );
70 $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getDomain(), $queue->getType() );
71
72 $now = wfTimestampNow();
73 $lastRepushTime = $cache->get( $key );
74 if ( $lastRepushTime === false ) {
75 $lastRepushTime = wfTimestamp( TS_MW, 1 ); // include all jobs
76 }
77
78 $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
79
80 $count = 0;
81 $skipped = 0;
82 foreach ( $queue->getAllAbandonedJobs() as $job ) {
84 if ( $job instanceof Job && $job->getQueuedTimestamp() < wfTimestamp( TS_UNIX, $lastRepushTime ) ) {
85 ++$skipped;
86 continue; // already re-pushed in prior round
87 }
88
89 $queue->push( $job );
90 ++$count;
91
92 if ( ( $count % $this->getBatchSize() ) == 0 ) {
93 $queue->waitForBackups();
94 }
95 }
96
97 $cache->set( $key, $now ); // next run will ignore these jobs
98
99 $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
100 }
101}
102
103// @codeCoverageIgnoreStart
104$maintClass = ManageJobs::class;
105require_once RUN_MAINTENANCE_IF_MAIN;
106// @codeCoverageIgnoreEnd
const CACHE_DB
Definition Defines.php:88
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script that handles managing job queue admin tasks (re-push, delete, ....
execute()
Do the actual work.
__construct()
Default constructor.
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:50
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:357
delete()
Delete all unclaimed and delayed jobs from the queue.
Definition JobQueue.php:585
getAllAbandonedJobs()
Get an iterator to traverse over all abandoned jobs in this queue.
Definition JobQueue.php:683
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:608
Describe and execute a background job.
Definition Job.php:41
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
$maintClass
if(count( $args)< 1) $job