Skip to content

Commit

Permalink
Revert to version that matches prod (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
tspencer244 authored Nov 13, 2023
1 parent 464ab32 commit 1a7c35e
Show file tree
Hide file tree
Showing 20 changed files with 175 additions and 1,310 deletions.
19 changes: 2 additions & 17 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,9 @@
"homepage": "https://github.com/HealthengineAU/php-resque",
"license": "MIT",
"authors": [
{
"name": "Dan Hunsaker",
"email": "[email protected]",
"role": "Maintainer"
},
{
"name": "Rajib Ahmed",
"homepage": "https://github.com/rajibahmed",
"role": "Maintainer"
},
{
"name": "Steve Klabnik",
"email": "[email protected]",
"role": "Maintainer"
},
{
"name": "Chris Boulton",
"email": "[email protected]",
"role": "Creator"
"email": "[email protected]"
},
{
"email": "[email protected]",
Expand All @@ -36,6 +20,7 @@
"ext-pcntl": "*",
"ext-posix": "*",
"ext-redis": "*",
"colinmollenhour/credis": "^1.0",
"psr/log": "^2.0 || ^3.0"
},
"scripts": {
Expand Down
40 changes: 9 additions & 31 deletions lib/Resque.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ class Resque
*/
protected static $redisDatabase = 0;

/**
* @var string auth of Redis database
*/
protected static $auth;

/**
* Given a host/port combination separated by a colon, set it as
* the redis server that Resque will talk to.
Expand All @@ -42,13 +37,11 @@ class Resque
* and returns a Resque_Redis instance, or
* a nested array of servers with host/port pairs.
* @param int $database
* @param string $auth
*/
public static function setBackend($server, $database = 0, $auth = null)
public static function setBackend($server, $database = 0)
{
self::$redisServer = $server;
self::$redisDatabase = $database;
self::$auth = $auth;
self::$redis = null;
}

Expand All @@ -69,10 +62,6 @@ public static function redis()
self::$redis = new Resque_Redis(self::$redisServer, self::$redisDatabase);
}

if (!empty(self::$auth)) {
self::$redis->auth(self::$auth);
}

return self::$redis;
}

Expand All @@ -87,6 +76,10 @@ public static function redis()
*/
public static function fork()
{
if(!function_exists('pcntl_fork')) {
return false;
}

// Close the connection to Redis before forking.
// This is a workaround for issues phpredis has.
self::$redis = null;
Expand Down Expand Up @@ -220,11 +213,10 @@ public static function size($queue)
* @param string $class The name of the class that contains the code to execute the job.
* @param array $args Any optional arguments that should be passed when the job is executed.
* @param boolean $trackStatus Set to true to be able to monitor the status of a job.
* @param string $prefix The prefix needs to be set for the status key
*
* @return string|boolean Job ID when the job was created, false if creation was cancelled due to beforeEnqueue
*/
public static function enqueue($queue, $class, $args = null, $trackStatus = false, $prefix = "")
public static function enqueue($queue, $class, $args = null, $trackStatus = false)
{
$id = Resque::generateJobId();
$hookParams = array(
Expand All @@ -239,7 +231,7 @@ public static function enqueue($queue, $class, $args = null, $trackStatus = fals
return false;
}

Resque_Job::create($queue, $class, $args, $trackStatus, $id, $prefix);
Resque_Job::create($queue, $class, $args, $trackStatus, $id);
Resque_Event::trigger('afterEnqueue', $hookParams);

return $id;
Expand Down Expand Up @@ -270,20 +262,6 @@ public static function queues()
return $queues;
}

/**
* Retrieve all the items of a queue with Redis
*
* @return array Array of items.
*/
public static function items($queue, $start = 0, $stop = -1)
{
$list = self::redis()->lrange('queue:' . $queue, $start, $stop);
if(!is_array($list)) {
$list = array();
}
return $list;
}

/**
* Remove Items from the queue
* Safely moving each item to a temporary queue before processing it
Expand Down Expand Up @@ -338,7 +316,7 @@ private static function removeItems($queue, $items = array())

/**
* matching item
* item can be ['class'] or ['class' => 'id'] or ['class' => {'foo' => 1, 'bar' => 2}]
* item can be ['class'] or ['class' => 'id'] or ['class' => {:foo => 1, :bar => 2}]
* @private
*
* @params string $string redis result in json
Expand All @@ -360,7 +338,7 @@ private static function matchItem($string, $items)
} elseif (is_array($val)) {
$decodedArgs = (array)$decoded['args'][0];
if ($decoded['class'] == $key &&
count($decodedArgs) > 0 && count(array_diff($decodedArgs, $val)) == 0) {
count($decodedArgs) > 0 && count(array_diff($decodedArgs, $val)) == 0) {
return true;
}
# class name with ID, example: item[0] = ['class' => 'id']
Expand Down
14 changes: 0 additions & 14 deletions lib/Resque/Failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@ public static function create($payload, Exception $exception, Resque_Worker $wor
new $backend($payload, $exception, $worker, $queue);
}

/**
* Create a new failed job on the backend from PHP 7 errors.
*
* @param object $payload The contents of the job that has just failed.
* @param \Error $exception The PHP 7 error generated when the job failed to run.
* @param \Resque_Worker $worker Instance of Resque_Worker that was running this job when it failed.
* @param string $queue The name of the queue that this job was fetched from.
*/
public static function createFromError($payload, Error $exception, Resque_Worker $worker, $queue)
{
$backend = self::getBackend();
new $backend($payload, $exception, $worker, $queue);
}

/**
* Return an instance of the backend for saving job failures.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Resque/Failure/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Resque_Failure_Redis implements Resque_Failure_Interface
public function __construct($payload, $exception, $worker, $queue)
{
$data = new stdClass();
$data->failed_at = date('c');
$data->failed_at = date('D M d H:i:s T Y');
$data->payload = $payload;
$data->exception = get_class($exception);
$data->error = $exception->getMessage();
Expand Down
80 changes: 25 additions & 55 deletions lib/Resque/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ public function __construct($queue, $payload)
* @param array $args Any optional arguments that should be passed when the job is executed.
* @param boolean $monitor Set to true to be able to monitor the status of a job.
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
* @param string $prefix The prefix needs to be set for the status key
*
* @return string
* @throws \InvalidArgumentException
*/
public static function create($queue, $class, $args = null, $monitor = false, $id = null, $prefix = "")
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
{
if (is_null($id)) {
$id = Resque::generateJobId();
Expand All @@ -70,15 +69,14 @@ public static function create($queue, $class, $args = null, $monitor = false, $i
);
}
Resque::push($queue, array(
'class' => $class,
'args' => array($args),
'id' => $id,
'prefix' => $prefix,
'class' => $class,
'args' => array($args),
'id' => $id,
'queue_time' => microtime(true),
));

if($monitor) {
Resque_Job_Status::create($id, $prefix);
Resque_Job_Status::create($id);
}

return $id;
Expand Down Expand Up @@ -125,28 +123,24 @@ public static function reserveBlocking(array $queues, $timeout = null)
*
* @param int $status Status constant from Resque_Job_Status indicating the current status of a job.
*/
public function updateStatus($status, $result = null)
public function updateStatus($status)
{
if(empty($this->payload['id'])) {
return;
}

$statusInstance = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
$statusInstance->update($status, $result);
$statusInstance = new Resque_Job_Status($this->payload['id']);
$statusInstance->update($status);
}

/**
* Return the status of the current job.
*
* @return int|null The status of the job as one of the Resque_Job_Status constants or null if job is not being tracked.
* @return int The status of the job as one of the Resque_Job_Status constants.
*/
public function getStatus()
{
if(empty($this->payload['id'])) {
return null;
}

$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
$status = new Resque_Job_Status($this->payload['id']);
return $status->get();
}

Expand Down Expand Up @@ -189,29 +183,28 @@ public function getInstance()
*/
public function perform()
{
$result = true;
try {
Resque_Event::trigger('beforePerform', $this);

$instance = $this->getInstance();
if(is_callable([$instance, 'setUp'])) {
if(method_exists($instance, 'setUp')) {
$instance->setUp();
}

$result = $instance->perform();
$instance->perform();

if(is_callable([$instance, 'tearDown'])) {
if(method_exists($instance, 'tearDown')) {
$instance->tearDown();
}

Resque_Event::trigger('afterPerform', $this);
}
// beforePerform/setUp have said don't perform this job. Return.
catch(Resque_Job_DontPerform $e) {
$result = false;
return false;
}

return $result;
return true;
}

/**
Expand All @@ -227,21 +220,12 @@ public function fail($exception)
));

$this->updateStatus(Resque_Job_Status::STATUS_FAILED);
if ($exception instanceof Error) {
Resque_Failure::createFromError(
$this->payload,
$exception,
$this->worker,
$this->queue
);
} else {
Resque_Failure::create(
$this->payload,
$exception,
$this->worker,
$this->queue
);
}
Resque_Failure::create(
$this->payload,
$exception,
$this->worker,
$this->queue
);
Resque_Stat::incr('failed');
Resque_Stat::incr('failed:' . $this->worker);
}
Expand All @@ -252,15 +236,13 @@ public function fail($exception)
*/
public function recreate()
{
$status = new Resque_Job_Status($this->payload['id']);
$monitor = false;
if (!empty($this->payload['id'])) {
$status = new Resque_Job_Status($this->payload['id'], $this->getPrefix());
if($status->isTracking()) {
$monitor = true;
}
if($status->isTracking()) {
$monitor = true;
}

return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor, null, $this->getPrefix());
return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor);
}

/**
Expand Down Expand Up @@ -304,16 +286,4 @@ public function getJobFactory()
}
return $this->jobFactory;
}

/**
* @return string
*/
private function getPrefix()
{
if (isset($this->payload['prefix'])) {
return $this->payload['prefix'];
}

return '';
}
}
Loading

0 comments on commit 1a7c35e

Please sign in to comment.