3 require( 'Net/Gearman/Client.php' );
4 require( 'Net/Gearman/Worker.php' );
6 class MWGearmanJob extends Net_Gearman_Job_Common {
7 function switchWiki( $wiki, $params ) {
8 echo "Switching to $wiki\n";
10 # Pretend that we have completed it right now, because the new process won't do it
11 $this->complete( array( 'result' => true ) );
12 socket_close( $this->conn );
15 $php = readlink( '/proc/' . posix_getpid() . '/exe' );
17 # Run the worker script
18 $args = array( $_SERVER['PHP_SELF'],
20 '--fake-job', serialize( $params ) );
21 $args = array_merge( $args, $GLOBALS['args'] );
22 pcntl_exec( $php, $args, $_ENV );
23 echo "Error running exec\n";
26 function run( $params ) {
27 if ( wfWikiID() !== $params['wiki'] ) {
28 $this->switchWiki( $params['wiki'], $params );
30 return self::runNoSwitch( $params );
33 static function runNoSwitch( $params ) {
34 echo implode( ' ', $params ) . "\n";
35 $title = Title::newFromText( $params['title'] );
36 $mwJob = Job::factory( $params['command'], $title, $params['params'] );
41 class NonScaryGearmanWorker extends Net_Gearman_Worker {
44 * Copied from Net_Gearman_Worker but with the scary "run any PHP file in
45 * the filesystem" feature removed.
47 protected function doWork($socket) {
48 Net_Gearman_Connection::send($socket, 'grab_job');
50 $resp = array('function' => 'noop');
51 while (count($resp) && $resp['function'] == 'noop') {
52 $resp = Net_Gearman_Connection::blockingRead($socket);
55 if (in_array($resp['function'], array('noop', 'no_job'))) {
59 if ($resp['function'] != 'job_assign') {
60 throw new Net_Gearman_Exception('Holy Cow! What are you doing?!');
63 $name = $resp['data']['func'];
64 $handle = $resp['data']['handle'];
67 if (isset($resp['data']['arg']) &&
68 Net_Gearman_Connection::stringLength($resp['data']['arg'])) {
69 $arg = json_decode($resp['data']['arg'], true);
72 ### START MW DIFFERENT BIT
73 if ( $name != 'mw_job' ) {
74 throw new Net_Gearman_Job_Exception('Invalid function');
76 $job = new MWGearmanJob($socket, $handle);
77 ### END MW DIFFERENT BIT
80 $this->start($handle, $name, $arg);
81 $res = $job->run($arg);
82 if (!is_array($res)) {
83 $res = array('result' => $res);
87 $this->complete($handle, $name, $res);
88 } catch (Net_Gearman_Job_Exception $e) {
90 $this->fail($handle, $name, $e);
93 // Force the job's destructor to run