MediaWiki master
manageJobs.php
Go to the documentation of this file.
1<?php
13use Wikimedia\Timestamp\TimestampFormat as TS;
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
24class ManageJobs extends Maintenance {
25 public function __construct() {
26 parent::__construct();
27 $this->addDescription( 'Perform administrative tasks on a job queue' );
28 $this->addOption( 'type', 'Job type', true, true );
29 $this->addOption( 'action', 'Queue operation ("delete", "repush-abandoned")', true, true );
30 $this->setBatchSize( 100 );
31 }
32
33 public function execute() {
34 $type = $this->getOption( 'type' );
35 $action = $this->getOption( 'action' );
36
37 $group = $this->getServiceContainer()->getJobQueueGroup();
38 $queue = $group->get( $type );
39
40 if ( $action === 'delete' ) {
41 $this->delete( $queue );
42 } elseif ( $action === 'repush-abandoned' ) {
43 $this->repushAbandoned( $queue );
44 } else {
45 $this->fatalError( "Invalid action '$action'." );
46 }
47 }
48
49 private function delete( JobQueue $queue ) {
50 $this->output( "Queue has {$queue->getSize()} job(s); deleting...\n" );
51 $queue->delete();
52 $this->output( "Done; current size is {$queue->getSize()} job(s).\n" );
53 }
54
55 private function repushAbandoned( JobQueue $queue ) {
56 $cache = $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_DB );
57 $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getDomain(), $queue->getType() );
58
59 $now = wfTimestampNow();
60 $lastRepushTime = $cache->get( $key );
61 if ( $lastRepushTime === false ) {
62 $lastRepushTime = wfTimestamp( TS::MW, 1 ); // include all jobs
63 }
64
65 $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
66
67 $count = 0;
68 $skipped = 0;
69 foreach ( $queue->getAllAbandonedJobs() as $job ) {
71 if ( $job instanceof Job && $job->getQueuedTimestamp() < wfTimestamp( TS::UNIX, $lastRepushTime ) ) {
72 ++$skipped;
73 continue; // already re-pushed in prior round
74 }
75
76 $queue->push( $job );
77 ++$count;
78
79 if ( ( $count % $this->getBatchSize() ) == 0 ) {
80 $queue->waitForBackups();
81 }
82 }
83
84 $cache->set( $key, $now ); // next run will ignore these jobs
85
86 $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
87 }
88}
89
90// @codeCoverageIgnoreStart
91$maintClass = ManageJobs::class;
92require_once RUN_MAINTENANCE_IF_MAIN;
93// @codeCoverageIgnoreEnd
const CACHE_DB
Definition Defines.php:74
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:36
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:342
delete()
Delete all unclaimed and delayed jobs from the queue.
Definition JobQueue.php:571
getAllAbandonedJobs()
Get an iterator to traverse over all abandoned jobs in this queue.
Definition JobQueue.php:669
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:594
Describe and execute a background job.
Definition Job.php:28
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