Skip to content

Commit c6ffb0c

Browse files
committed
改进:Job对象实例化时也传入任务参数
1 parent 6827f90 commit c6ffb0c

2 files changed

Lines changed: 57 additions & 49 deletions

File tree

src/queue/Job.php

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@
1313

1414
use Exception;
1515
use think\App;
16+
use think\helper\Arr;
1617

1718
abstract class Job
1819
{
1920

2021
/**
2122
* The job handler instance.
22-
* @var mixed
23+
* @var object
2324
*/
24-
protected $instance;
25+
private $instance;
26+
27+
/**
28+
* The JSON decoded version of "$job".
29+
* @var array
30+
*/
31+
private $payload;
2532

2633
/**
2734
* @var App
@@ -61,11 +68,17 @@ abstract class Job
6168
/**
6269
* Get the decoded body of the job.
6370
*
64-
* @return array
71+
* @return mixed
6572
*/
66-
public function payload()
73+
public function payload($name = null, $default = null)
6774
{
68-
return json_decode($this->getRawBody(), true);
75+
if (empty($this->payload)) {
76+
$this->payload = json_decode($this->getRawBody(), true);
77+
}
78+
if (empty($name)) {
79+
return $this->payload;
80+
}
81+
return Arr::get($this->payload, $name, $default);
6982
}
7083

7184
/**
@@ -74,13 +87,25 @@ public function payload()
7487
*/
7588
public function fire()
7689
{
77-
$payload = $this->payload();
90+
$instance = $this->getResolvedJob();
7891

79-
list($class, $method) = $this->parseJob($payload['job']);
92+
[, $method] = $this->getParsedJob();
8093

81-
$this->instance = $this->resolve($class);
82-
if ($this->instance) {
83-
$this->instance->{$method}($this, $payload['data']);
94+
$instance->{$method}($this, $this->payload('data'));
95+
}
96+
97+
/**
98+
* Process an exception that caused the job to fail.
99+
*
100+
* @param Exception $e
101+
* @return void
102+
*/
103+
public function failed($e)
104+
{
105+
$instance = $this->getResolvedJob();
106+
107+
if (method_exists($instance, 'failed')) {
108+
$instance->failed($this->payload('data'), $e);
84109
}
85110
}
86111

@@ -151,11 +176,11 @@ abstract public function getRawBody();
151176

152177
/**
153178
* Parse the job declaration into class and method.
154-
* @param string $job
155179
* @return array
156180
*/
157-
protected function parseJob($job)
181+
protected function getParsedJob()
158182
{
183+
$job = $this->payload('job');
159184
$segments = explode('@', $job);
160185

161186
return count($segments) > 1 ? $segments : [$segments[0], 'fire'];
@@ -166,20 +191,31 @@ protected function parseJob($job)
166191
* @param string $name
167192
* @return mixed
168193
*/
169-
protected function resolve($name)
194+
protected function resolve($name, $param)
170195
{
171196
if (strpos($name, '\\') === false) {
172197

173198
if (strpos($name, '/') === false) {
174199
$app = '';
175200
} else {
176-
list($app, $name) = explode('/', $name, 2);
201+
[$app, $name] = explode('/', $name, 2);
177202
}
178203

179204
$name = ($this->app->config->get('app.app_namespace') ?: 'app\\') . ($app ? strtolower($app) . '\\' : '') . 'job\\' . $name;
180205
}
181206

182-
return $this->app->make($name);
207+
return $this->app->make($name, [$param], true);
208+
}
209+
210+
public function getResolvedJob()
211+
{
212+
if (empty($this->instance)) {
213+
[$class] = $this->getParsedJob();
214+
215+
$this->instance = $this->resolve($class, $this->payload('data'));
216+
}
217+
218+
return $this->instance;
183219
}
184220

185221
/**
@@ -202,33 +238,14 @@ public function markAsFailed()
202238
$this->failed = true;
203239
}
204240

205-
/**
206-
* Process an exception that caused the job to fail.
207-
*
208-
* @param Exception $e
209-
* @return void
210-
*/
211-
public function failed($e)
212-
{
213-
$this->markAsFailed();
214-
215-
$payload = $this->payload();
216-
217-
list($class, $method) = $this->parseJob($payload['job']);
218-
219-
if (method_exists($this->instance = $this->resolve($class), 'failed')) {
220-
$this->instance->failed($payload['data'], $e);
221-
}
222-
}
223-
224241
/**
225242
* Get the number of times to attempt a job.
226243
*
227244
* @return int|null
228245
*/
229246
public function maxTries()
230247
{
231-
return $this->payload()['maxTries'] ?? null;
248+
return $this->payload('maxTries');
232249
}
233250

234251
/**
@@ -238,7 +255,7 @@ public function maxTries()
238255
*/
239256
public function timeout()
240257
{
241-
return $this->payload()['timeout'] ?? null;
258+
return $this->payload('timeout');
242259
}
243260

244261
/**
@@ -248,7 +265,7 @@ public function timeout()
248265
*/
249266
public function timeoutAt()
250267
{
251-
return $this->payload()['timeoutAt'] ?? null;
268+
return $this->payload('timeoutAt');
252269
}
253270

254271
/**
@@ -258,7 +275,7 @@ public function timeoutAt()
258275
*/
259276
public function getName()
260277
{
261-
return $this->payload()['job'];
278+
return $this->payload('job');
262279
}
263280

264281
/**

src/queue/job/Redis.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ class Redis extends Job
3030
*/
3131
protected $job;
3232

33-
/**
34-
* The JSON decoded version of "$job".
35-
*
36-
* @var array
37-
*/
38-
protected $decoded;
39-
4033
/**
4134
* The Redis job payload inside the reserved queue.
4235
*
@@ -52,8 +45,6 @@ public function __construct(App $app, RedisQueue $redis, $job, $reserved, $conne
5245
$this->connection = $connection;
5346
$this->redis = $redis;
5447
$this->reserved = $reserved;
55-
56-
$this->decoded = $this->payload();
5748
}
5849

5950
/**
@@ -62,7 +53,7 @@ public function __construct(App $app, RedisQueue $redis, $job, $reserved, $conne
6253
*/
6354
public function attempts()
6455
{
65-
return ($this->decoded['attempts'] ?? null) + 1;
56+
return $this->payload('attempts') + 1;
6657
}
6758

6859
/**
@@ -106,7 +97,7 @@ public function release($delay = 0)
10697
*/
10798
public function getJobId()
10899
{
109-
return $this->decoded['id'] ?? null;
100+
return $this->payload('id');
110101
}
111102

112103
/**

0 commit comments

Comments
 (0)