* Submit jobs in batches and wait for each batch, to avoid overflowing the queue
[lhc/web/wiklou.git] / maintenance / gearman / gearmanWorker.php
1 <?php
2
3 $optionsWithArgs = array( 'fake-job', 'procs' );
4 require( dirname(__FILE__).'/../commandLine.inc' );
5 require( dirname(__FILE__).'/gearman.inc' );
6
7 if ( isset( $options['procs'] ) ) {
8 $procs = $options['procs'];
9 if ( $procs < 1 || $procs > 1000 ) {
10 echo "Invalid number of processes, please specify a number between 1 and 1000\n";
11 exit( 1 );
12 }
13 $fc = new ForkController;
14 if ( $fc->forkWorkers( $procs ) == 'parent' ) {
15 $fc->runParent();
16 exit( 0 );
17 }
18 }
19
20 if ( !$args ) {
21 $args = array( 'localhost' );
22 }
23
24 if ( isset( $options['fake-job'] ) ) {
25 $params = unserialize( $options['fake-job'] );
26 MWGearmanJob::runNoSwitch( $params );
27 }
28
29 $worker = new NonScaryGearmanWorker( $args );
30 $worker->addAbility( 'mw_job' );
31 $worker->beginWork( 'wfGearmanMonitor' );
32
33 function wfGearmanMonitor( $idle, $lastJob ) {
34 static $lastSleep = 0;
35 $interval = 5;
36 $now = time();
37 if ( $now - $lastSleep >= $interval ) {
38 wfWaitForSlaves( $interval );
39 $lastSleep = $now;
40 }
41 return false;
42 }