Skip to content

Commit e52ee4c

Browse files
committed
Apply old PR 135
Closes chrisboulton/php-resque#135
1 parent f87b002 commit e52ee4c

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed

lib/Resque.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ public static function size($queue)
213213
* @param string $class The name of the class that contains the code to execute the job.
214214
* @param array $args Any optional arguments that should be passed when the job is executed.
215215
* @param boolean $trackStatus Set to true to be able to monitor the status of a job.
216+
* @param string $prefix The prefix needs to be set for the status key
216217
*
217218
* @return string|boolean Job ID when the job was created, false if creation was cancelled due to beforeEnqueue
218219
*/
219-
public static function enqueue($queue, $class, $args = null, $trackStatus = false)
220+
public static function enqueue($queue, $class, $args = null, $trackStatus = false, $prefix = "")
220221
{
221222
$id = Resque::generateJobId();
222223
$hookParams = array(
@@ -232,7 +233,7 @@ public static function enqueue($queue, $class, $args = null, $trackStatus = fals
232233
return false;
233234
}
234235

235-
Resque_Job::create($queue, $class, $args, $trackStatus, $id);
236+
Resque_Job::create($queue, $class, $args, $trackStatus, $id, $prefix);
236237
Resque_Event::trigger('afterEnqueue', $hookParams);
237238

238239
return $id;

lib/Resque/Job.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ public function __construct($queue, $payload)
5353
* @param array $args Any optional arguments that should be passed when the job is executed.
5454
* @param boolean $monitor Set to true to be able to monitor the status of a job.
5555
* @param string $id Unique identifier for tracking the job. Generated if not supplied.
56+
* @param string $prefix The prefix needs to be set for the status key
5657
*
5758
* @return string
5859
* @throws \InvalidArgumentException
5960
*/
60-
public static function create($queue, $class, $args = null, $monitor = false, $id = null)
61+
public static function create($queue, $class, $args = null, $monitor = false, $id = null, $prefix = "")
6162
{
6263
if (is_null($id)) {
6364
$id = Resque::generateJobId();
@@ -69,14 +70,15 @@ public static function create($queue, $class, $args = null, $monitor = false, $i
6970
);
7071
}
7172
Resque::push($queue, array(
72-
'class' => $class,
73-
'args' => array($args),
74-
'id' => $id,
73+
'class' => $class,
74+
'args' => array($args),
75+
'id' => $id,
76+
'prefix' => $prefix,
7577
'queue_time' => microtime(true),
7678
));
7779

7880
if($monitor) {
79-
Resque_Job_Status::create($id);
81+
Resque_Job_Status::create($id, $prefix);
8082
}
8183

8284
return $id;
@@ -129,7 +131,7 @@ public function updateStatus($status, $result = null)
129131
return;
130132
}
131133

132-
$statusInstance = new Resque_Job_Status($this->payload['id']);
134+
$statusInstance = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
133135
$statusInstance->update($status, $result);
134136
}
135137

@@ -140,7 +142,7 @@ public function updateStatus($status, $result = null)
140142
*/
141143
public function getStatus()
142144
{
143-
$status = new Resque_Job_Status($this->payload['id']);
145+
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
144146
return $status->get();
145147
}
146148

@@ -237,13 +239,13 @@ public function fail($exception)
237239
*/
238240
public function recreate()
239241
{
240-
$status = new Resque_Job_Status($this->payload['id']);
242+
$status = new Resque_Job_Status($this->payload['id'], $this->payload['prefix']);
241243
$monitor = false;
242244
if($status->isTracking()) {
243245
$monitor = true;
244246
}
245247

246-
return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor);
248+
return self::create($this->queue, $this->payload['class'], $this->getArguments(), $monitor, $this->payload['prefix']);
247249
}
248250

249251
/**

lib/Resque/Job/Status.php

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Resque_Job_Status
1313
const STATUS_FAILED = 3;
1414
const STATUS_COMPLETE = 4;
1515

16+
/**
17+
* @var string The prefix of the job status id.
18+
*/
19+
private $prefix;
20+
1621
/**
1722
* @var string The ID of the job this status class refers back to.
1823
*/
@@ -37,9 +42,10 @@ class Resque_Job_Status
3742
*
3843
* @param string $id The ID of the job to manage the status for.
3944
*/
40-
public function __construct($id)
45+
public function __construct($id, $prefix = '')
4146
{
4247
$this->id = $id;
48+
$this->prefix = empty($prefix) ? '' : "${prefix}_";
4349
}
4450

4551
/**
@@ -48,14 +54,18 @@ public function __construct($id)
4854
*
4955
* @param string $id The ID of the job to monitor the status of.
5056
*/
51-
public static function create($id)
57+
public static function create($id, $prefix = "")
5258
{
59+
$status = new self($id, $prefix);
5360
$statusPacket = array(
54-
'status' => self::STATUS_WAITING,
61+
'status' => self::STATUS_WAITING,
5562
'updated' => time(),
5663
'started' => time(),
64+
'result' => null,
5765
);
58-
Resque::redis()->set('job:' . $id . ':status', json_encode($statusPacket));
66+
Resque::redis()->set((string) $status, json_encode($statusPacket));
67+
68+
return $status;
5969
}
6070

6171
/**
@@ -91,9 +101,10 @@ public function update($status, $result = null)
91101
}
92102

93103
$statusPacket = array(
94-
'status' => $status,
104+
'status' => $status,
95105
'updated' => time(),
96-
'result' => $result,
106+
'started' => $this->getValue('started'),
107+
'result' => $result,
97108
);
98109
Resque::redis()->set((string)$this, json_encode($statusPacket));
99110

@@ -104,12 +115,12 @@ public function update($status, $result = null)
104115
}
105116

106117
/**
107-
* Fetch the status for the job being monitored.
118+
* Fetch a value from the status packet for the job being monitored.
108119
*
109-
* @return mixed False if the status is not being monitored, otherwise the status
110-
* as an integer, based on the Resque_Job_Status constants.
120+
* @return mixed False if the status is not being monitored, otherwise the
121+
* requested value from the status packet.
111122
*/
112-
public function get()
123+
protected function getValue($value = null)
113124
{
114125
if(!$this->isTracking()) {
115126
return false;
@@ -120,7 +131,18 @@ public function get()
120131
return false;
121132
}
122133

123-
return $statusPacket['status'];
134+
return empty($value) ? $statusPacket : $statusPacket[$value];
135+
}
136+
137+
/**
138+
* Fetch the status for the job being monitored.
139+
*
140+
* @return mixed False if the status is not being monitored, otherwise the status
141+
* as an integer, based on the Resque_Job_Status constants.
142+
*/
143+
public function get()
144+
{
145+
return $this->getValue('status');
124146
}
125147

126148
/**
@@ -131,32 +153,9 @@ public function get()
131153
*/
132154
public function getResult()
133155
{
134-
if(!$this->isTracking()) {
135-
return false;
136-
}
137-
138-
$statusPacket = json_decode(Resque::redis()->get((string)$this), true);
139-
if(!$statusPacket) {
140-
return false;
141-
}
142-
143-
return $statusPacket['result'];
156+
return $this->getValue('result');
144157
}
145158

146-
/**
147-
* Delete the job monitoring from the queue
148-
*
149-
* @return boolean|int
150-
*/
151-
public function del()
152-
{
153-
if(!$this->isTracking()) {
154-
return false;
155-
}
156-
157-
return Resque::redis()->del((string)$this);
158-
}
159-
160159
/**
161160
* Stop tracking the status of a job.
162161
*/
@@ -172,6 +171,6 @@ public function stop()
172171
*/
173172
public function __toString()
174173
{
175-
return 'job:' . $this->id . ':status';
174+
return 'job:' . $this->prefix . $this->id . ':status';
176175
}
177176
}

0 commit comments

Comments
 (0)