Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[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 * @author Aaron Schulz
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 /**
28 * Maintenance script that handles managing job queue admin tasks (re-push, delete, ...)
29 *
30 * @ingroup Maintenance
31 */
32 class ManageJobs extends Maintenance {
33 public function __construct() {
34 parent::__construct();
35 $this->addDescription( 'Perform administrative tasks on a job queue' );
36 $this->addOption( 'type', 'Job type', true, true );
37 $this->addOption( 'action', 'Queue operation ("delete", "repush-abandoned")', true, true );
38 }
39
40 public function execute() {
41 $type = $this->getOption( 'type' );
42 $action = $this->getOption( 'action' );
43
44 $group = JobQueueGroup::singleton();
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->error( "Invalid action '$action'.", 1 );
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 ) {
63 $cache = ObjectCache::getInstance( CACHE_DB );
64 $key = $cache->makeGlobalKey( 'last-job-repush', $queue->getWiki(), $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 ) {
77 /** @var Job $job */
78 if ( $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->mBatchSize ) == 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";
98 require_once RUN_MAINTENANCE_IF_MAIN;