diff --git a/lib/Resque/Event.php b/lib/Resque/Event.php index 20072ff..704a505 100644 --- a/lib/Resque/Event.php +++ b/lib/Resque/Event.php @@ -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; } /** diff --git a/lib/Resque/Job.php b/lib/Resque/Job.php index ef7191b..f059acd 100755 --- a/lib/Resque/Job.php +++ b/lib/Resque/Job.php @@ -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).' ); } @@ -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; } @@ -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])) + { + $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); diff --git a/lib/Resque/Worker.php b/lib/Resque/Worker.php index 0d0e4d3..1b920c7 100755 --- a/lib/Resque/Worker.php +++ b/lib/Resque/Worker.php @@ -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. @@ -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); } @@ -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(); @@ -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; } @@ -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: @@ -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); }