Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Job tracking improvements #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 77 additions & 16 deletions lib/Resque/Job/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Resque_Job_Status
*/
public function __construct($id)
{
$this->id = $id;
$this->id = self::generateId($id);
}

/**
Expand All @@ -53,9 +53,9 @@ public static function create($id)
$statusPacket = array(
'status' => self::STATUS_WAITING,
'updated' => time(),
'started' => time(),
'started' => time()
);
Resque::redis()->set('job:' . $id . ':status', json_encode($statusPacket));
Resque::redis()->set(self::generateId($id), json_encode($statusPacket));
}

/**
Expand All @@ -70,7 +70,7 @@ public function isTracking()
return false;
}

if(!Resque::redis()->exists((string)$this)) {
if(!Resque::redis()->exists($this->id)) {
$this->isTracking = false;
return false;
}
Expand All @@ -86,19 +86,26 @@ public function isTracking()
*/
public function update($status)
{
$status = (int)$status;

if(!$this->isTracking()) {
return;
}

if($status < 1 || $status > 4) {
return;
Copy link
Author

Choose a reason for hiding this comment

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

should we throw an exception if an invalid status is passed to update?

It is probably only ever called internally from php-resque, but still...

}

$statusPacket = array(
'status' => $status,
'updated' => time(),
'started' => $this->fetch('started')
Copy link
Author

Choose a reason for hiding this comment

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

we were discarding started on update previously.

);
Resque::redis()->set((string)$this, json_encode($statusPacket));
Resque::redis()->set($this->id, json_encode($statusPacket));

// Expire the status for completed jobs after 24 hours
if(in_array($status, self::$completeStatuses)) {
Resque::redis()->expire((string)$this, 86400);
Resque::redis()->expire($this->id, 86400);
}
}

Expand All @@ -110,24 +117,46 @@ public function update($status)
*/
public function get()
{
if(!$this->isTracking()) {
return false;
}
return $this->status();
}

$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
if(!$statusPacket) {
return false;
}
/**
* Fetch the status for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the status as
* as an integer, based on the Resque_Job_Status constants.
*/
public function status()
{
return $this->fetch('status');
}

/**
* Fetch the updated timestamp for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the updated timestamp
*/
public function updated()
{
return $this->fetch('updated');
}

return $statusPacket['status'];
/**
* Fetch the started timestamp for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the created timestamp
*/
public function started()
{
return $this->fetch('started');
}

/**
* Stop tracking the status of a job.
*/
public function stop()
{
Resque::redis()->del((string)$this);
Resque::redis()->del($this->id);
}

/**
Expand All @@ -137,6 +166,38 @@ public function stop()
*/
public function __toString()
{
return 'job:' . $this->id . ':status';
return $this->id;
}

/**
* generate job status id key in a consistent manner
*
* @return string String redis key for the current job status
*/
protected static function generateId($id)
{
return 'job:' . $id . ':status';
}

/**
* Fetch the status packet for the job being monitored.
* @param optional string $field The field to get from the status packet
*
* @return mixed False if the status is not being monitored, otherwise the status packet array or the individual field
*/
protected function fetch($field = false)
{
$statusPacket = Resque::redis()->get($this->id);
if($statusPacket) {
$statusPacket = json_decode($statusPacket, true);
if($field) {
if(isset($statusPacket[$field])) {
return (int)$statusPacket[$field];
}
} else {
return $statusPacket;
}
}
return false;
}
}