Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Patched for cakePHP3: #34

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
21 changes: 11 additions & 10 deletions lib/Resque/Event.php
Expand Up @@ -22,22 +22,23 @@ class Resque_Event
*/
public static function trigger($event, $data = null)
{
$fired = 0;

if (!is_array($data)) {
$data = array($data);
}

if (empty(self::$events[$event])) {
return true;
}

foreach (self::$events[$event] as $callback) {
if (!is_callable($callback)) {
continue;
if (!empty(self::$events[$event])) {
foreach (self::$events[$event] as $callback) {
if (!is_callable($callback)) {
continue;
}
$fired++;
call_user_func_array($callback, $data);
}
call_user_func_array($callback, $data);

}

return true;
return $fired;
}

/**
Expand Down
54 changes: 44 additions & 10 deletions lib/Resque/Job.php
Expand Up @@ -161,7 +161,7 @@ public function getInstance()
} else {
if(!class_exists($this->payload['class'])) {
throw new Resque_Exception(
'Could not find job class ' . $this->payload['class'] . '.'
'Could not find job class ' . $this->payload['class'] . ' (Resque_Job_Creator not loaded).'
);
}

Expand All @@ -170,12 +170,15 @@ public function getInstance()
'Job class ' . $this->payload['class'] . ' does not contain a perform method.'
);
}

$this->instance = new $this->payload['class']();

$this->instance->job = $this;
$this->instance->args = $this->getArguments();
$this->instance->queue = $this->queue;

}

$this->instance->job = $this;
$this->instance->args = $this->getArguments();
$this->instance->queue = $this->queue;
return $this->instance;
}

Expand All @@ -188,18 +191,49 @@ public function getInstance()
*/
public function perform()
{

$method = 'perform';
$beforePerformMethod = 'setUp';
$tearDownMethod = 'tearDown';
$instance = $this->getInstance();
if (is_array($instance))
{
if (isset($instance[4]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this sorcery? These seem to be magic numbers. I've no idea what these things mean :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are a way to allow helper class Resque_Job_Creator to be more flexible and use variable names for "perform" , "setUP" and "tearDown" methods to better match underneat job classes
For example to use CakePHP shells as jobs: wa0x6e/Cake-Resque#92

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, you're saying that if Resque_Job_Creator returns an array, then it'll override some settings?

I'm not sure if this is the way you want to go about things... I mean - why not just have something like

public function setUp() 
{
    return $this->realSetUp();
}

public function perform($args) 
{
    return $this->realPerform($args);
}

I'm really not a fan of this, and think it goes significantly against the original design of resque.

Can I see a use case for when this would actually be useful? Because I can only think of horrendously convoluted examples.

{
$tearDownMethod = $instance[4];
}
if (isset($instance[3]))
{
$beforePerformMethod = $instance[3];
}
if (isset($instance[2]))
{
$args = $instance[2];
}
if (isset($instance[1]))
{
$method = $instance[1];
}
$instance = $instance[0];
}

try {
Resque_Event::trigger('beforePerform', $this);

if(method_exists($instance, 'setUp')) {
$instance->setUp();
if(method_exists($instance, $beforePerformMethod)) {
$instance->$beforePerformMethod();
}

$instance->perform();

if(method_exists($instance, 'tearDown')) {
$instance->tearDown();
if (isset($args))
{
$instance->$method($args);
}
else
{
$instance->$method();
}
if(method_exists($instance, $tearDownMethod)) {
$instance->$tearDownMethod();
}

Resque_Event::trigger('afterPerform', $this);
Expand Down
26 changes: 21 additions & 5 deletions lib/Resque/Worker.php
Expand Up @@ -49,7 +49,7 @@ class Resque_Worker
const LOG_TYPE_CRITICAL = 500;
const LOG_TYPE_ALERT = 550;

public $logOutput = STDOUT;
public $logOutput = null;

/**
* @var int Current log level of this worker.
Expand Down Expand Up @@ -166,6 +166,11 @@ public function setId($workerId)
*/
public function __construct($queues)
{
if (defined('STDOUT'))
{
$this->logOutput = STDOUT;
}

if (!is_array($queues)) {
$queues = array($queues);
}
Expand Down Expand Up @@ -260,6 +265,9 @@ public function work($interval = 5)

$this->child = null;
$this->doneWorking();

$fired = Resque_Event::trigger('afterdoneworking', $job);
$this->log(array('message' => "afterdoneworking triggered {$fired} callbacks", 'data' => compact('job')), self::LOG_TYPE_INFO);
}

$this->unregisterWorker();
Expand Down Expand Up @@ -509,9 +517,13 @@ public function pruneDeadWorkers()
public function workerPids()
{
$pids = array();
exec('ps -A -o pid,comm | grep [r]esque', $cmdOutput);
exec('ps -A -o pid,comm,command | grep [r]esque', $cmdOutput);
foreach ($cmdOutput as $line) {
list($pids[]) = explode(' ', trim($line), 2);
$cols = explode(' ', trim($line), 3);
if (trim($cols[1])=='php')
{
$pids[] = trim($cols[0]);
}
}
return $pids;
}
Expand Down Expand Up @@ -626,7 +638,9 @@ public function log($message, $code = self::LOG_TYPE_INFO)
if (($this->logLevel === self::LOG_NORMAL || $this->logLevel === self::LOG_VERBOSE) && $code !== self::LOG_TYPE_DEBUG) {

if ($this->logger === null) {
fwrite($this->logOutput, "[" . date('c') . "] " . $message . "\n");
if (!is_null($this->logOutput)) {
fwrite($this->logOutput, "[" . date('c') . "] " . $message . "\n");
}
} else {
switch ($code) {
case self::LOG_TYPE_INFO:
Expand All @@ -650,7 +664,9 @@ public function log($message, $code = self::LOG_TYPE_INFO)

} else if ($code === self::LOG_TYPE_DEBUG && $this->logLevel === self::LOG_VERBOSE) {
if ($this->logger === null) {
fwrite($this->logOutput, "[" . date('c') . "] " . $message . "\n");
if (!is_null($this->logOutput)) {
fwrite($this->logOutput, "[" . date('c') . "] " . $message . "\n");
}
} else {
$this->logger->addDebug($message, $extra);
}
Expand Down