Skip to content

Commit

Permalink
Apply old PR 210
Browse files Browse the repository at this point in the history
  • Loading branch information
danhunsaker committed Dec 11, 2018
1 parent e52ee4c commit dec0d9c
Showing 1 changed file with 67 additions and 19 deletions.
86 changes: 67 additions & 19 deletions lib/Resque/Job/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,20 @@ public function isTracking()
*/
public function update($status, $result = null)
{
$status = (int) $status;

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

if($status < self::STATUS_WAITING || $status > self::STATUS_COMPLETE) {
return;
}

$statusPacket = array(
'status' => $status,
'updated' => time(),
'started' => $this->getValue('started'),
'started' => $this->fetch('started'),
'result' => $result,
);
Resque::redis()->set((string)$this, json_encode($statusPacket));
Expand All @@ -115,23 +121,14 @@ public function update($status, $result = null)
}

/**
* Fetch a value from the status packet for the job being monitored.
* Fetch the status for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the
* requested value from the status packet.
* @return mixed False if the status is not being monitored, otherwise the status
* as an integer, based on the Resque_Job_Status constants.
*/
protected function getValue($value = null)
public function get()
{
if(!$this->isTracking()) {
return false;
}

$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
if(!$statusPacket) {
return false;
}

return empty($value) ? $statusPacket : $statusPacket[$value];
return $this->status();
}

/**
Expand All @@ -140,20 +137,42 @@ protected function getValue($value = null)
* @return mixed False if the status is not being monitored, otherwise the status
* as an integer, based on the Resque_Job_Status constants.
*/
public function get()
public function status()
{
return $this->getValue('status');
return $this->fetch('status');
}

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

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

/**
* Fetch the result of the job being monitored.
*
* @return mixed False if the job is not being monitored, otherwise the result
* as mixed
*/
public function getResult()
public function result()
{
return $this->getValue('result');
return $this->fetch('result');
}

/**
Expand All @@ -173,4 +192,33 @@ public function __toString()
{
return 'job:' . $this->prefix . $this->id . ':status';
}

/**
* Fetch a value from the status packet for the job being monitored.
*
* @return mixed False if the status is not being monitored, otherwise the
* requested value from the status packet.
*/
protected function fetch($value = null)
{
if(!$this->isTracking()) {
return false;
}

$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
if(!$statusPacket) {
return false;
}

if(empty($value)) {
return $statusPacket;
} else {
if(isset($statusPacket[$value])) {
return $statusPacket[$value];
} else {
return null;
}
}

}
}

2 comments on commit dec0d9c

@Aeon
Copy link

@Aeon Aeon commented on dec0d9c Dec 11, 2018

Choose a reason for hiding this comment

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

Finally :) too bad original commit credits didn’t stick, but at least it’s merged!

@danhunsaker
Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, sorry about that. There were a lot of repos I couldn't access directly to get the original commits and merge them that way, so I had to do it this way instead. Might be able to go back through and reconstruct attributions with commit amendments, but that'll be ... messy, so I'm holding off for the moment.

Please sign in to comment.