forked from chrisboulton/php-resque
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring to JobStrategy, adding FastCGI option, fixing unit test i…
…ssues Merging changes from chrisboulton#81
- Loading branch information
Showing
12 changed files
with
467 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Resque\Resque; | ||
|
||
if (!isset($_SERVER['RESQUE_JOB'])) { | ||
header('Status: 500 No Job'); | ||
return; | ||
} | ||
|
||
// Look for parent project's Composer autoloader | ||
$path = __DIR__.'/../../../vendor/autoload.php'; | ||
if (!file_exists($path)) { | ||
// Fallback to this project's autoloader | ||
$path = __DIR__.'/../vendor/autoload.php'; | ||
} | ||
// Die if Composer hasn't been run yet | ||
require_once $path; | ||
|
||
if (isset($_SERVER['REDIS_BACKEND'])) { | ||
Resque::setBackend($_SERVER['REDIS_BACKEND']); | ||
} | ||
|
||
try { | ||
if (isset($_SERVER['APP_INCLUDE'])) { | ||
require_once $_SERVER['APP_INCLUDE']; | ||
} | ||
|
||
$job = unserialize(urldecode($_SERVER['RESQUE_JOB'])); | ||
$job->worker->perform($job); | ||
} catch (\Exception $e) { | ||
if (isset($job)) { | ||
$job->fail($e); | ||
} else { | ||
header('Status: 500'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
namespace Resque\JobStrategy; | ||
|
||
use Psr\Log\LogLevel; | ||
use Resque\Worker; | ||
use Resque\Job; | ||
use EBernhardson\FastCGI\Client; | ||
use EBernhardson\FastCGI\CommunicationException; | ||
|
||
/** | ||
* @package Resque/JobStrategy | ||
* @author Erik Bernhardson <[email protected]> | ||
* @license http://www.opensource.org/licenses/mit-license.php | ||
*/ | ||
class Fastcgi implements StrategyInterface | ||
{ | ||
/** | ||
* @var bool True when waiting for a response from FastCGI server | ||
*/ | ||
private $waiting = false; | ||
|
||
/** | ||
* @var array Default environment for FastCGI requests | ||
*/ | ||
protected $requestData = array( | ||
'GATEWAY_INTERFACE' => 'FastCGI/1.0', | ||
'REQUEST_METHOD' => 'GET', | ||
'SERVER_SOFTWARE' => 'php-resque-fastcgi/1.3-dev', | ||
'REMOTE_ADDR' => '127.0.0.1', | ||
'REMOTE_PORT' => 8888, | ||
'SERVER_ADDR' => '127.0.0.1', | ||
'SERVER_PORT' => 8888, | ||
'SERVER_PROTOCOL' => 'HTTP/1.1' | ||
); | ||
|
||
/** @var string */ | ||
private $location; | ||
/** @var Client */ | ||
private $fcgi; | ||
/** @var Worker */ | ||
private $worker; | ||
|
||
/** | ||
* @param string $location When the location contains a `:` it will be considered a host/port pair | ||
* otherwise a unix socket path | ||
* @param string $script Absolute path to the script that will load resque and perform the job | ||
* @param array $environment Additional environment variables available in $_SERVER to the FastCGI script | ||
*/ | ||
public function __construct($location, $script, $environment = array()) | ||
{ | ||
$this->location = $location; | ||
|
||
$port = false; | ||
if (false !== strpos($location, ':')) { | ||
list($location, $port) = explode(':', $location, 2); | ||
} | ||
|
||
$this->fcgi = new Client($location, $port); | ||
$this->fcgi->setKeepAlive(true); | ||
|
||
$this->requestData = $environment + $this->requestData + array( | ||
'SCRIPT_FILENAME' => $script, | ||
'SERVER_NAME' => php_uname('n'), | ||
'RESQUE_DIR' => __DIR__.'/../../../', | ||
); | ||
} | ||
|
||
/** | ||
* @param Worker $worker | ||
*/ | ||
public function setWorker(Worker $worker) | ||
{ | ||
$this->worker = $worker; | ||
} | ||
|
||
/** | ||
* Executes the provided job over a FastCGI connection | ||
* | ||
* @param Job $job | ||
*/ | ||
public function perform(Job $job) | ||
{ | ||
$status = 'Requested fcgi job execution from ' . $this->location . ' at ' . strftime('%F %T'); | ||
$this->worker->updateProcLine($status); | ||
$this->worker->logger->log(LogLevel::INFO, $status); | ||
|
||
$this->waiting = true; | ||
|
||
try { | ||
$this->fcgi->request(array( | ||
'RESQUE_JOB' => urlencode(serialize($job)), | ||
) + $this->requestData, ''); | ||
|
||
$response = $this->fcgi->response(); | ||
$this->waiting = false; | ||
} catch (CommunicationException $e) { | ||
$this->waiting = false; | ||
$job->fail($e); | ||
return; | ||
} | ||
|
||
if ($response['statusCode'] !== 200) { | ||
$job->fail(new \Exception(sprintf( | ||
'FastCGI job returned non-200 status code: %s Stdout: %s Stderr: %s', | ||
$response['headers']['status'], | ||
$response['body'], | ||
$response['stderr'] | ||
))); | ||
} | ||
} | ||
|
||
/** | ||
* Shutdown the worker process. | ||
*/ | ||
public function shutdown() | ||
{ | ||
if ($this->waiting === false) { | ||
$this->worker->logger->log(LogLevel::INFO, 'No child to kill.'); | ||
} else { | ||
$this->worker->logger->log(LogLevel::INFO, 'Closing fcgi connection with job in progress.'); | ||
} | ||
$this->fcgi->close(); | ||
} | ||
} |
Oops, something went wrong.