202c1e7f4f6ba4906238d420c132fdc222ad609f
[lhc/web/wiklou.git] / maintenance / runJobs.php
1 <?php
2 /**
3 * This script starts pending jobs.
4 *
5 * Usage:
6 * --maxjobs <num> (default 10000)
7 * --type <job_cmd>
8 *
9 * @file
10 * @ingroup Maintenance
11 */
12
13 require_once( "Maintenance.php" );
14
15 class RunJobs extends Maintenance {
16 public function __construct() {
17 global $wgUseNormalUser;
18 parent::__construct();
19 $this->mDescription = "Run pending jobs";
20 $this->addParam( 'maxjobs', 'Maximum number of jobs to run', false, true );
21 $this->addParam( 'type', 'Type of job to run', false, true );
22 $this->addParam( 'procs', 'Number of processes to use', false, true );
23 $wgUseNormalUser = true;
24 }
25
26 public function execute() {
27 global $wgTitle;
28 if ( $this->hasOption( 'procs' ) ) {
29 $procs = intval( $this->getOption('procs') );
30 if ( $procs < 1 || $procs > 1000 ) {
31 $this->error( "Invalid argument to --procs\n", true );
32 }
33 $fc = new ForkController( $procs );
34 if ( $fc->start( $procs ) != 'child' ) {
35 exit( 0 );
36 }
37 }
38 $maxJobs = $this->getOption( 'maxjobs', 10000 );
39 $type = $this->getOption( 'type', false );
40 $wgTitle = Title::newFromText( 'RunJobs.php' );
41 $dbw = wfGetDB( DB_MASTER );
42 $n = 0;
43 $conds = '';
44 if ($type !== false)
45 $conds = "job_cmd = " . $dbw->addQuotes($type);
46
47 while ( $dbw->selectField( 'job', 'job_id', $conds, 'runJobs.php' ) ) {
48 $offset=0;
49 for (;;) {
50 $job = ($type == false) ?
51 Job::pop($offset)
52 : Job::pop_type($type);
53
54 if ($job == false)
55 break;
56
57 wfWaitForSlaves( 5 );
58 $t = microtime( true );
59 $offset=$job->id;
60 $status = $job->run();
61 $t = microtime( true ) - $t;
62 $timeMs = intval( $t * 1000 );
63 if ( !$status ) {
64 $this->runJobsLog( $job->toString() . " t=$timeMs error={$job->error}" );
65 } else {
66 $this->runJobsLog( $job->toString() . " t=$timeMs good" );
67 }
68 if ( $maxJobs && ++$n > $maxJobs ) {
69 break 2;
70 }
71 }
72 }
73 }
74
75 /**
76 * Log the job message
77 * @param $msg String The message to log
78 */
79 private function runJobsLog( $msg ) {
80 $this->output( wfTimestamp( TS_DB ) . " $msg\n" );
81 wfDebugLog( 'runJobs', $msg );
82 }
83 }
84
85 $maintClass = "RunJobs";
86 require_once( DO_MAINTENANCE );