-
Notifications
You must be signed in to change notification settings - Fork 759
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
Aeon
wants to merge
1
commit into
chrisboulton:master
Choose a base branch
from
Aeon:job-tracking-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ class Resque_Job_Status | |
*/ | ||
public function __construct($id) | ||
{ | ||
$this->id = $id; | ||
$this->id = self::generateId($id); | ||
} | ||
|
||
/** | ||
|
@@ -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)); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
@@ -86,19 +86,26 @@ public function isTracking() | |
*/ | ||
public function update($status) | ||
{ | ||
$status = (int)$status; | ||
|
||
if(!$this->isTracking()) { | ||
return; | ||
} | ||
|
||
if($status < 1 || $status > 4) { | ||
return; | ||
} | ||
|
||
$statusPacket = array( | ||
'status' => $status, | ||
'updated' => time(), | ||
'started' => $this->fetch('started') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we were discarding |
||
); | ||
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); | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...