Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
oanhnn committed Mar 16, 2016
1 parent 108261d commit 41eb134
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
33 changes: 17 additions & 16 deletions lib/Resque/Event.php
Expand Up @@ -18,26 +18,27 @@ class Resque_Event
*
* @param string $event Name of event to be raised.
* @param mixed $data Optional, any data that should be passed to each callback.
* @return true
* @return int
*/
public static function trigger($event, $data = null)
{
if (!is_array($data)) {
$data = array($data);
}
$fired = 0;

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

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

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

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

return $fired;
}

/**
Expand Down
10 changes: 5 additions & 5 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 @@ -171,11 +171,11 @@ public function getInstance()
);
}
$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,7 +188,7 @@ public function getInstance()
*/
public function perform()
{
$instance = $this->getInstance();
$instance = $this->getInstance();
try {
Resque_Event::trigger('beforePerform', $this);

Expand Down
26 changes: 22 additions & 4 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,9 @@ 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 +263,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 @@ -374,8 +380,12 @@ protected function startup()
*/
protected function updateProcLine($status)
{
if (function_exists('setproctitle')) {
setproctitle('resque-' . Resque::VERSION . ': ' . $status);
$processTitle = 'resque-' . Resque::VERSION . ': ' . $status;

if (function_exists('cli_set_process_title')) {
cli_set_process_title($processTitle);
} elseif (function_exists('setproctitle')) {
setproctitle($processTitle);
}
}

Expand Down Expand Up @@ -509,9 +519,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 @@ -597,6 +611,10 @@ public function log($message, $code = self::LOG_TYPE_INFO)
return false;
}

if (null === $this->logger && null === $this->logOutput) {
return false;
}

/*if ($this->logger === null) {
if ($this->logLevel === self::LOG_NORMAL && $code !== self::LOG_TYPE_DEBUG) {
fwrite($this->logOutput, "*** " . $message['message'] . "\n");
Expand Down

0 comments on commit 41eb134

Please sign in to comment.