| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 = $this->getServiceContainer()->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 ) { |
| 65 |
$cache = $this->getServiceContainer()->getObjectCacheFactory()->getInstance( CACHE_DB ); |
| 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 ); |
| 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 ) { |
| 79 |
|
| 80 |
if ( $job instanceof Job && $job->getQueuedTimestamp() < wfTimestamp( TS_UNIX, $lastRepushTime ) ) { |
| 81 |
++$skipped; |
| 82 |
continue; |
| 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 ); |
| 94 |
|
| 95 |
$this->output( "Re-pushed $count job(s) [$skipped skipped].\n" ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
$maintClass = ManageJobs::class; |
| 101 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 102 |
|