MediaWiki  master
manageJobs.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
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 = MediaWikiServices::getInstance()->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 ) {
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 ) {
80  if ( $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 $maintClass = ManageJobs::class;
100 require_once RUN_MAINTENANCE_IF_MAIN;
const CACHE_DB
Definition: Defines.php:87
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.
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:36
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:596
delete()
Delete all unclaimed and delayed jobs from the queue.
Definition: JobQueue.php:573
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:66
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
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.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Maintenance script that handles managing job queue admin tasks (re-push, delete, ....
Definition: manageJobs.php:33
execute()
Do the actual work.
Definition: manageJobs.php:42
__construct()
Default constructor.
Definition: manageJobs.php:34
Service locator for MediaWiki core services.
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:75
$maintClass
Definition: manageJobs.php:99
$cache
Definition: mcc.php:33
if(count( $args)< 1) $job