MediaWiki master
manageJobs.php
Go to the documentation of this file.
1<?php
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
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 ) {
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
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.
Base class for queueing and running background jobs from a storage backend.
Definition JobQueue.php:45
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition JobQueue.php:603
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition JobQueue.php:352
delete()
Delete all unclaimed and delayed jobs from the queue.
Definition JobQueue.php:580
getAllAbandonedJobs()
Get an iterator to traverse over all abandoned jobs in this queue.
Definition JobQueue.php:678
Describe and execute a background job.
Definition Job.php:38
Maintenance script that handles managing job queue admin tasks (re-push, delete, ....
execute()
Do the actual work.
__construct()
Default constructor.
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