MediaWiki  1.34.0
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  }
38 
39  public function execute() {
40  $type = $this->getOption( 'type' );
41  $action = $this->getOption( 'action' );
42 
43  $group = JobQueueGroup::singleton();
44  $queue = $group->get( $type );
45 
46  if ( $action === 'delete' ) {
47  $this->delete( $queue );
48  } elseif ( $action === 'repush-abandoned' ) {
49  $this->repushAbandoned( $queue );
50  } else {
51  $this->fatalError( "Invalid action '$action'." );
52  }
53  }
54 
55  private function delete( JobQueue $queue ) {
56  $this->output( "Queue has {$queue->getSize()} job(s); deleting...\n" );
57  $queue->delete();
58  $this->output( "Done; current size is {$queue->getSize()} job(s).\n" );
59  }
60 
61  private function repushAbandoned( JobQueue $queue ) {
63  $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getDomain(), $queue->getType() );
64 
65  $now = wfTimestampNow();
66  $lastRepushTime = $cache->get( $key );
67  if ( $lastRepushTime === false ) {
68  $lastRepushTime = wfTimestamp( TS_MW, 1 ); // include all jobs
69  }
70 
71  $this->output( "Last re-push time: $lastRepushTime; current time: $now\n" );
72 
73  $count = 0;
74  $skipped = 0;
75  foreach ( $queue->getAllAbandonedJobs() as $job ) {
77  if ( $job->getQueuedTimestamp() < wfTimestamp( TS_UNIX, $lastRepushTime ) ) {
78  ++$skipped;
79  continue; // already re-pushed in prior round
80  }
81 
82  $queue->push( $job );
83  ++$count;
84 
85  if ( ( $count % $this->getBatchSize() ) == 0 ) {
86  $queue->waitForBackups();
87  }
88  }
89 
90  $cache->set( $key, $now ); // next run will ignore these jobs
91 
92  $this->output( "Re-pushed $count job(s) [$skipped skipped].\n" );
93  }
94 }
95 
96 $maintClass = ManageJobs::class;
97 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1869
ManageJobs
Maintenance script that handles managing job queue admin tasks (re-push, delete, ....
Definition: manageJobs.php:31
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
ManageJobs\execute
execute()
Do the actual work.
Definition: manageJobs.php:39
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
ObjectCache\getInstance
static getInstance( $id)
Get a cached instance of the specified type of cache object.
Definition: ObjectCache.php:80
$queue
$queue
Definition: mergeMessageFileList.php:157
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1898
ManageJobs\__construct
__construct()
Default constructor.
Definition: manageJobs.php:32
ManageJobs\repushAbandoned
repushAbandoned(JobQueue $queue)
Definition: manageJobs.php:61
JobQueue\waitForBackups
waitForBackups()
Wait for any replica DBs or backup servers to catch up.
Definition: JobQueue.php:570
$maintClass
$maintClass
Definition: manageJobs.php:96
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$cache
$cache
Definition: mcc.php:33
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:386
JobQueue
Class to handle enqueueing and running of background jobs.
Definition: JobQueue.php:31
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
CACHE_DB
const CACHE_DB
Definition: Defines.php:83
$type
$type
Definition: testCompression.php:48