MediaWiki  master
manageJobs.php
Go to the documentation of this file.
1 <?php
24 require_once __DIR__ . '/Maintenance.php';
25 
31 class ManageJobs extends Maintenance {
32  public function __construct() {
33  parent::__construct();
34  $this->addDescription( 'Perform administrative tasks on a job queue' );
35  $this->addOption( 'type', 'Job type', true, true );
36  $this->addOption( 'action', 'Queue operation ("delete", "repush-abandoned")', true, true );
37  $this->setBatchSize( 100 );
38  }
39 
40  public function execute() {
41  $type = $this->getOption( 'type' );
42  $action = $this->getOption( 'action' );
43 
44  $group = $this->getServiceContainer()->getJobQueueGroup();
45  $queue = $group->get( $type );
46 
47  if ( $action === 'delete' ) {
48  $this->delete( $queue );
49  } elseif ( $action === 'repush-abandoned' ) {
50  $this->repushAbandoned( $queue );
51  } else {
52  $this->fatalError( "Invalid action '$action'." );
53  }
54  }
55 
56  private function delete( JobQueue $queue ) {
57  $this->output( "Queue has {$queue->getSize()} job(s); deleting...\n" );
58  $queue->delete();
59  $this->output( "Done; current size is {$queue->getSize()} job(s).\n" );
60  }
61 
62  private function repushAbandoned( JobQueue $queue ) {
64  $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getDomain(), $queue->getType() );
65 
66  $now = wfTimestampNow();
67  $lastRepushTime = $cache->get( $key );
68  if ( $lastRepushTime === false ) {
69  $lastRepushTime = wfTimestamp( TS_MW, 1 ); // include all jobs
70  }
71 
72  $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
73 
74  $count = 0;
75  $skipped = 0;
76  foreach ( $queue->getAllAbandonedJobs() as $job ) {
78  if ( $job instanceof Job && $job->getQueuedTimestamp() < wfTimestamp( TS_UNIX, $lastRepushTime ) ) {
79  ++$skipped;
80  continue; // already re-pushed in prior round
81  }
82 
83  $queue->push( $job );
84  ++$count;
85 
86  if ( ( $count % $this->getBatchSize() ) == 0 ) {
87  $queue->waitForBackups();
88  }
89  }
90 
91  $cache->set( $key, $now ); // next run will ignore these jobs
92 
93  $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
94  }
95 }
96 
97 $maintClass = ManageJobs::class;
98 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:38
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:594
push( $jobs, $flags=0)
Push one or more jobs into the queue.
Definition: JobQueue.php:343
delete()
Delete all unclaimed and delayed jobs from the queue.
Definition: JobQueue.php:571
getDomain()
Definition: JobQueue.php:160
getAllAbandonedJobs()
Get an iterator to traverse over all abandoned jobs in this queue.
Definition: JobQueue.php:669
Class to both describe a background job and handle jobs.
Definition: Job.php:40
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.
getServiceContainer()
Returns the main service container.
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:31
execute()
Do the actual work.
Definition: manageJobs.php:40
__construct()
Default constructor.
Definition: manageJobs.php:32
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:83
$maintClass
Definition: manageJobs.php:97
if(count( $args)< 1) $job