mirror of
https://github.com/idanoo/php-resque.git
synced 2024-11-22 08:15:14 +00:00
WIP
This commit is contained in:
parent
e541fa9b70
commit
b8f98eecd2
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/env php
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Find and initialize Composer
|
// Find and initialize Composer
|
||||||
|
@ -3,8 +3,9 @@ class PHP_Job
|
|||||||
{
|
{
|
||||||
public function perform()
|
public function perform()
|
||||||
{
|
{
|
||||||
sleep(120);
|
fwrite(STDOUT, 'Start job! -> ');
|
||||||
fwrite(STDOUT, 'Hello!');
|
sleep(1);
|
||||||
|
fwrite(STDOUT, 'Job ended!' . PHP_EOL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
@ -14,6 +14,6 @@ $args = array(
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
$jobId = Resque::enqueue('default', $argv[1], $args, true);
|
$jobId = Resque::enqueue($argv[1], $argv[2], $args, true);
|
||||||
echo "Queued job ".$jobId."\n\n";
|
echo "Queued job ".$jobId."\n\n";
|
||||||
?>
|
?>
|
@ -114,19 +114,38 @@ class Resque
|
|||||||
* @param string $queue The name of the queue to fetch an item from.
|
* @param string $queue The name of the queue to fetch an item from.
|
||||||
* @return array Decoded item from the queue.
|
* @return array Decoded item from the queue.
|
||||||
*/
|
*/
|
||||||
public static function pop($queue, $interval = null)
|
public static function pop($queue)
|
||||||
{
|
{
|
||||||
if($interval == null) {
|
|
||||||
$item = self::redis()->lpop('queue:' . $queue);
|
$item = self::redis()->lpop('queue:' . $queue);
|
||||||
} else {
|
|
||||||
$item = self::redis()->blpop('queue:' . $queue, $interval ? $interval : Resque::DEFAULT_INTERVAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$item) {
|
if(!$item) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return json_decode($interval == 0 ? $item : $item[1], true);
|
return json_decode($item, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pop an item off the end of the specified queue, decode it and
|
||||||
|
* return it.
|
||||||
|
*
|
||||||
|
* @param string $queue The name of the queue to fetch an item from.
|
||||||
|
* @return array Decoded item from the queue.
|
||||||
|
*/
|
||||||
|
public static function blpop($queues, $interval = null)
|
||||||
|
{
|
||||||
|
$list = array();
|
||||||
|
foreach($queues AS $queue) {
|
||||||
|
$list[] = 'queue:' . $queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$item = self::redis()->blpop($list, $interval ? (int)$interval : Resque::DEFAULT_INTERVAL);
|
||||||
|
|
||||||
|
if(!$item) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_decode($item[1], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,9 +80,9 @@ class Resque_Job
|
|||||||
* @param string $queue The name of the queue to check for a job in.
|
* @param string $queue The name of the queue to check for a job in.
|
||||||
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
|
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
|
||||||
*/
|
*/
|
||||||
public static function reserve($queue, $interval = null)
|
public static function reserve($queue)
|
||||||
{
|
{
|
||||||
$payload = Resque::pop($queue, $interval);
|
$payload = Resque::pop($queue);
|
||||||
if(!is_array($payload)) {
|
if(!is_array($payload)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -90,6 +90,25 @@ class Resque_Job
|
|||||||
return new Resque_Job($queue, $payload);
|
return new Resque_Job($queue, $payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the next available job from the specified queue and return an
|
||||||
|
* instance of Resque_Job for it.
|
||||||
|
*
|
||||||
|
* @param string $queue The name of the queue to check for a job in.
|
||||||
|
* @return null|object Null when there aren't any waiting jobs, instance of Resque_Job when a job was found.
|
||||||
|
*/
|
||||||
|
public static function reserveBlocking($queues, $interval = null)
|
||||||
|
{
|
||||||
|
$payload = Resque::blpop($queues, $interval);
|
||||||
|
if(!is_array($payload)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var_dump($payload);
|
||||||
|
|
||||||
|
return new Resque_Job($payload->queue, $payload);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the status of the current job.
|
* Update the status of the current job.
|
||||||
*
|
*
|
||||||
|
@ -143,7 +143,13 @@ class Resque_Redis
|
|||||||
*/
|
*/
|
||||||
public function __call($name, $args) {
|
public function __call($name, $args) {
|
||||||
if(in_array($name, $this->keyCommands)) {
|
if(in_array($name, $this->keyCommands)) {
|
||||||
|
if(is_array($args[0])) {
|
||||||
|
foreach($args[0] AS $i => $v) {
|
||||||
|
$args[0][$i] = self::$defaultNamespace . $v;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
$args[0] = self::$defaultNamespace . $args[0];
|
$args[0] = self::$defaultNamespace . $args[0];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return $this->driver->__call($name, $args);
|
return $this->driver->__call($name, $args);
|
||||||
|
@ -152,34 +152,9 @@ class Resque_Worker
|
|||||||
$this->updateProcLine('Starting');
|
$this->updateProcLine('Starting');
|
||||||
$this->startup();
|
$this->startup();
|
||||||
|
|
||||||
while(true) {
|
while($job = $this->reserveBlocking($interval)) {
|
||||||
if($this->shutdown) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempt to find and reserve a job
|
|
||||||
$job = false;
|
|
||||||
if(!$this->paused) {
|
|
||||||
$job = $this->reserve($interval);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->updateProcLine('Waiting for ' . implode(',', $this->queues) . ' with interval ' . $interval);
|
$this->updateProcLine('Waiting for ' . implode(',', $this->queues) . ' with interval ' . $interval);
|
||||||
|
|
||||||
if(!$job) {
|
|
||||||
// For an interval of 0, break now - helps with unit testing etc
|
|
||||||
if($interval == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no job was found, we sleep for $interval before continuing and checking again
|
|
||||||
if($this->paused) {
|
|
||||||
$this->updateProcLine('Paused');
|
|
||||||
usleep($interval * 1000000); //it's paused, so don't hog redis with requests.
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->log('got ' . $job);
|
$this->log('got ' . $job);
|
||||||
Resque_Event::trigger('beforeFork', $job);
|
Resque_Event::trigger('beforeFork', $job);
|
||||||
$this->workingOn($job);
|
$this->workingOn($job);
|
||||||
@ -215,6 +190,14 @@ class Resque_Worker
|
|||||||
|
|
||||||
$this->child = null;
|
$this->child = null;
|
||||||
$this->doneWorking();
|
$this->doneWorking();
|
||||||
|
|
||||||
|
if($this->shutdown) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->paused) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->unregisterWorker();
|
$this->unregisterWorker();
|
||||||
@ -246,15 +229,15 @@ class Resque_Worker
|
|||||||
*
|
*
|
||||||
* @return object|boolean Instance of Resque_Job if a job is found, false if not.
|
* @return object|boolean Instance of Resque_Job if a job is found, false if not.
|
||||||
*/
|
*/
|
||||||
public function reserve($interval = null)
|
public function reserve()
|
||||||
{
|
{
|
||||||
$queues = $this->queues();
|
$queues = $this->queues();
|
||||||
if(!is_array($queues)) {
|
if(!is_array($queues)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
foreach($queues as $queue) {
|
foreach($queues as $queue) {
|
||||||
$this->log('Checking ' . $queue . ' with interval ' . $interval, self::LOG_VERBOSE);
|
$this->log('Checking ' . $queue, self::LOG_VERBOSE);
|
||||||
$job = Resque_Job::reserve($queue, $interval);
|
$job = Resque_Job::reserve($queue);
|
||||||
if($job) {
|
if($job) {
|
||||||
$this->log('Found job on ' . $queue, self::LOG_VERBOSE);
|
$this->log('Found job on ' . $queue, self::LOG_VERBOSE);
|
||||||
return $job;
|
return $job;
|
||||||
@ -264,6 +247,27 @@ class Resque_Worker
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempt to find a job from the top of one of the queues for this worker.
|
||||||
|
*
|
||||||
|
* @return object|boolean Instance of Resque_Job if a job is found, false if not.
|
||||||
|
*/
|
||||||
|
public function reserveBlocking($interval = null)
|
||||||
|
{
|
||||||
|
$queues = $this->queues();
|
||||||
|
if(!is_array($queues)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$job = Resque_Job::reserveBlocking($queues, $interval);
|
||||||
|
if($job) {
|
||||||
|
$this->log('Found job on ' . $job->queue, self::LOG_VERBOSE);
|
||||||
|
return $job;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an array containing all of the queues that this worker should use
|
* Return an array containing all of the queues that this worker should use
|
||||||
* when searching for jobs.
|
* when searching for jobs.
|
||||||
|
Loading…
Reference in New Issue
Block a user