22<?php
33
44// Find and initialize Composer
5+ use Resque \JobStrategy \Fastcgi ;
6+ use Resque \JobStrategy \Fork ;
7+ use Resque \JobStrategy \InProcess ;
58use Resque \Redis ;
69use Resque \Resque ;
710use Resque \Log ;
@@ -13,8 +16,6 @@ $files = array(
1316 __DIR__ . '/../../../../autoload.php ' ,
1417 __DIR__ . '/../vendor/autoload.php ' ,
1518);
16-
17- $ found = false ;
1819foreach ($ files as $ file ) {
1920 if (file_exists ($ file )) {
2021 require_once $ file ;
@@ -31,7 +32,7 @@ if (!class_exists('Composer\Autoload\ClassLoader', false)) {
3132}
3233
3334$ QUEUE = getenv ('QUEUE ' );
34- if (empty ($ QUEUE )) {
35+ if (empty ($ QUEUE )) {
3536 die ("Set QUEUE env var containing the list of queues to work. \n" );
3637}
3738
@@ -45,7 +46,7 @@ $REDIS_BACKEND = getenv('REDIS_BACKEND');
4546
4647// A redis database number
4748$ REDIS_BACKEND_DB = getenv ('REDIS_BACKEND_DB ' );
48- if (!empty ($ REDIS_BACKEND )) {
49+ if (!empty ($ REDIS_BACKEND )) {
4950 if (empty ($ REDIS_BACKEND_DB ))
5051 Resque::setBackend ($ REDIS_BACKEND );
5152 else
@@ -56,15 +57,14 @@ $logLevel = false;
5657$ LOGGING = getenv ('LOGGING ' );
5758$ VERBOSE = getenv ('VERBOSE ' );
5859$ VVERBOSE = getenv ('VVERBOSE ' );
59- if (!empty ($ LOGGING ) || !empty ($ VERBOSE )) {
60+ if (!empty ($ LOGGING ) || !empty ($ VERBOSE )) {
6061 $ logLevel = true ;
61- }
62- else if (!empty ($ VVERBOSE )) {
62+ } elseif (!empty ($ VVERBOSE )) {
6363 $ logLevel = true ;
6464}
6565
6666$ APP_INCLUDE = getenv ('APP_INCLUDE ' );
67- if ($ APP_INCLUDE ) {
67+ if ($ APP_INCLUDE ) {
6868 if (!file_exists ($ APP_INCLUDE )) {
6969 die ('APP_INCLUDE ( ' .$ APP_INCLUDE .") does not exist. \n" );
7070 }
@@ -82,34 +82,72 @@ $BLOCKING = getenv('BLOCKING') !== FALSE;
8282
8383$ interval = 5 ;
8484$ INTERVAL = getenv ('INTERVAL ' );
85- if (!empty ($ INTERVAL )) {
85+ if (!empty ($ INTERVAL )) {
8686 $ interval = $ INTERVAL ;
8787}
8888
8989$ count = 1 ;
9090$ COUNT = getenv ('COUNT ' );
91- if (!empty ($ COUNT ) && $ COUNT > 1 ) {
91+ if (!empty ($ COUNT ) && $ COUNT > 1 ) {
9292 $ count = $ COUNT ;
9393}
9494
9595$ PREFIX = getenv ('PREFIX ' );
96- if (!empty ($ PREFIX )) {
96+ if (!empty ($ PREFIX )) {
9797 $ logger ->log (Psr \Log \LogLevel::INFO , 'Prefix set to {prefix} ' , array ('prefix ' => $ PREFIX ));
9898 Redis::prefix ($ PREFIX );
9999}
100100
101- if ($ count > 1 ) {
102- for ($ i = 0 ; $ i < $ count ; ++$ i ) {
101+ $ jobStrategy = null ;
102+ $ JOB_STRATEGY = getenv ('JOB_STRATEGY ' );
103+ switch ($ JOB_STRATEGY ) {
104+ case 'inprocess ' :
105+ $ jobStrategy = new InProcess ;
106+ break ;
107+ case 'fork ' :
108+ $ jobStrategy = new Fork ;
109+ break ;
110+ case 'fastcgi ' :
111+ $ fastcgiLocation = '127.0.0.1:9000 ' ;
112+ $ FASTCGI_LOCATION = getenv ('FASTCGI_LOCATION ' );
113+ if (!empty ($ FASTCGI_LOCATION )) {
114+ $ fastcgiLocation = $ FASTCGI_LOCATION ;
115+ }
116+
117+ $ fastcgiScript = __DIR__ .'/../extras/fastcgi_worker.php ' ;
118+ $ FASTCGI_SCRIPT = getenv ('FASTCGI_SCRIPT ' );
119+ if (!empty ($ FASTCGI_SCRIPT )) {
120+ $ fastcgiScript = $ FASTCGI_SCRIPT ;
121+ }
122+
123+ require_once __DIR__ .'/../lib/JobStrategy/Fastcgi.php ' ;
124+ $ jobStrategy = new Fastcgi (
125+ $ fastcgiLocation ,
126+ $ fastcgiScript ,
127+ array (
128+ 'APP_INCLUDE ' => $ APP_INCLUDE ,
129+ 'REDIS_BACKEND ' => $ REDIS_BACKEND ,
130+ )
131+ );
132+ break ;
133+ }
134+
135+
136+ if ($ count > 1 ) {
137+ for ($ i = 0 ; $ i < $ count ; ++$ i ) {
103138 $ pid = Resque::fork ();
104- if ($ pid == -1 ) {
139+ if ($ pid == -1 ) {
105140 $ logger ->log (Psr \Log \LogLevel::EMERGENCY , 'Could not fork worker {count} ' , array ('count ' => $ i ));
106141 die ();
107142 }
108143 // Child, start the worker
109- else if (!$ pid ) {
144+ elseif (!$ pid ) {
110145 $ queues = explode (', ' , $ QUEUE );
111146 $ worker = new Worker ($ queues );
112147 $ worker ->setLogger ($ logger );
148+ if ($ jobStrategy ) {
149+ $ worker ->setJobStrategy ($ jobStrategy );
150+ }
113151 $ logger ->log (Psr \Log \LogLevel::NOTICE , 'Starting worker {worker} ' , array ('worker ' => $ worker ));
114152 $ worker ->work ($ interval , $ BLOCKING );
115153 break ;
@@ -121,6 +159,9 @@ else {
121159 $ queues = explode (', ' , $ QUEUE );
122160 $ worker = new Worker ($ queues );
123161 $ worker ->setLogger ($ logger );
162+ if ($ jobStrategy ) {
163+ $ worker ->setJobStrategy ($ jobStrategy );
164+ }
124165
125166 $ PIDFILE = getenv ('PIDFILE ' );
126167 if ($ PIDFILE ) {
0 commit comments