Merge "Use our fork of less.php" into REL1_31
[lhc/web/wiklou.git] / maintenance / manageJobs.php
1 <?php
2 /**
3 * Maintenance script that handles managing job queue admin tasks
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 require_once __DIR__ . '/Maintenance.php';
25
26 /**
27 * Maintenance script that handles managing job queue admin tasks (re-push, delete, ...)
28 *
29 * @ingroup Maintenance
30 */
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 ) {
62 $cache = ObjectCache::getInstance( CACHE_DB );
63 $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getWiki(), $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 ) {
76 /** @var Job $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;