summaryrefslogtreecommitdiff
path: root/maintenance/gearman/gearmanWorker.php
blob: 3ea100818c163eca7612b2c38cf29ab1b5c55c2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

$optionsWithArgs = array( 'fake-job', 'procs' );
require( dirname( __FILE__ ) . '/../commandLine.inc' );
require( dirname( __FILE__ ) . '/gearman.inc' );

ini_set( 'memory_limit', '150M' );

if ( isset( $options['procs'] ) ) {
	$procs = $options['procs'];
	if ( $procs < 1 || $procs > 1000 ) {
		echo "Invalid number of processes, please specify a number between 1 and 1000\n";
		exit( 1 );
	}
	$fc = new ForkController( $procs, ForkController::RESTART_ON_ERROR );
	if ( $fc->start() != 'child' ) {
		exit( 0 );
	}
}

if ( !$args ) {
	$args = array( 'localhost' );
}

if ( isset( $options['fake-job'] ) ) {
	$params = unserialize( $options['fake-job'] );
	MWGearmanJob::runNoSwitch( $params );
}

$worker = new NonScaryGearmanWorker( $args );
$worker->addAbility( 'mw_job' );
$worker->beginWork( 'wfGearmanMonitor' );

function wfGearmanMonitor( $idle, $lastJob ) {
	static $lastSleep = 0;
	$interval = 5;
	$now = time();
	if ( $now - $lastSleep >= $interval ) {
		wfWaitForSlaves();
		$lastSleep = $now;
	}
	return false;
}