summaryrefslogtreecommitdiff
path: root/maintenance/gearman/gearmanWorker.php
blob: 0b26ff9f17aa44c2c95a865952ab1f3cc1129cd9 (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
<?php

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

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( $interval );
		$lastSleep = $now;
	}
	return false;
}