Skip to content

Commit 92664a9

Browse files
committed
feat: update job config
1 parent 8ec6bf7 commit 92664a9

File tree

1 file changed

+63
-22
lines changed

1 file changed

+63
-22
lines changed

src/Job.php

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
abstract class Job implements Dispatchable
88
{
9-
/**
10-
* Job config
11-
*/
12-
protected $config = [];
13-
149
/**
1510
* Current job
1611
*/
@@ -19,7 +14,7 @@ abstract class Job implements Dispatchable
1914
/**
2015
* Data to pass to the job
2116
*/
22-
protected $data = [];
17+
protected static $data = [];
2318

2419
/**
2520
* Queue instance
@@ -32,6 +27,41 @@ abstract class Job implements Dispatchable
3227
*/
3328
protected string $connection = 'default';
3429

30+
/**
31+
* Number of seconds to wait before processing a job
32+
*/
33+
protected int $delay = 0;
34+
35+
/**
36+
* Number of seconds to wait before retrying a job that has failed.
37+
*/
38+
protected $delayBeforeRetry = 0;
39+
40+
/**
41+
* Number of seconds to wait before archiving a job that has not yet been processed
42+
*/
43+
protected $expire = 60;
44+
45+
/**
46+
* Force the worker to process the job, even if it has expired or has reached its maximum number of retries
47+
*/
48+
protected $force = false;
49+
50+
/**
51+
* The maximum amount of memory the job is allowed to consume (MB)
52+
*/
53+
protected $memory = 128;
54+
55+
/**
56+
* The number of seconds a child process can run before being killed.
57+
*/
58+
protected $timeout = 60;
59+
60+
/**
61+
* The maximum number of times the job may be attempted.
62+
*/
63+
protected $tries = 3;
64+
3565
/**
3666
* Return configured connection
3767
*/
@@ -49,11 +79,16 @@ public function fromQueue($job, $config, $queue)
4979
$this->queue = $queue;
5080

5181
if (isset($config['data'])) {
52-
$this->data = $config['data'];
53-
unset($config['data']);
82+
static::$data = $config['data'];
5483
}
5584

56-
$this->config = $config;
85+
$this->delay = $config['delay'] ?? 0;
86+
$this->delayBeforeRetry = $config['delayBeforeRetry'] ?? 0;
87+
$this->expire = $config['expire'] ?? 60;
88+
$this->force = $config['force'] ?? false;
89+
$this->memory = $config['memory'] ?? 128;
90+
$this->timeout = $config['timeout'] ?? 60;
91+
$this->tries = $config['tries'] ?? 3;
5792

5893
return $this;
5994
}
@@ -73,15 +108,15 @@ public function getJobId()
73108
*/
74109
public function handleDelay()
75110
{
76-
sleep($this->config['delay']);
111+
sleep($this->delay);
77112
}
78113

79114
/**
80115
* Check if job has expired
81116
*/
82117
public function hasExpired()
83118
{
84-
return $this->job['created_at'] < time() - $this->config['expire'];
119+
return $this->job['created_at'] < (time() - $this->expire);
85120
}
86121

87122
/**
@@ -98,7 +133,7 @@ public function handleExpiry()
98133
*/
99134
public function hasReachedRetryLimit()
100135
{
101-
return $this->job['retry_count'] >= $this->config['tries'];
136+
return $this->job['retry_count'] >= $this->tries;
102137
}
103138

104139
/**
@@ -114,7 +149,7 @@ public function setStatus($status)
114149
*/
115150
public function retry()
116151
{
117-
sleep($this->config['delayBeforeRetry'] ?? 0);
152+
sleep($this->delayBeforeRetry ?? 0);
118153
$this->queue->retryFailedJob($this->job['id'], $this->job['retry_count']);
119154
}
120155

@@ -130,20 +165,26 @@ public function release($delay = 0)
130165

131166
$this->queue->push([
132167
'class' => $this->job['class'],
133-
'config' => json_encode($this->config),
134168
'status' => 'pending',
135169
'retry_count' => $this->job['retry_count'] + 1,
170+
'config' => json_encode([
171+
'delay' => $this->delay,
172+
'delayBeforeRetry' => $this->delayBeforeRetry,
173+
'expire' => $this->expire,
174+
'force' => $this->force,
175+
'memory' => $this->memory,
176+
'timeout' => $this->timeout,
177+
'tries' => $this->tries,
178+
'data' => static::$data,
179+
]),
136180
]);
137181
}
138182

139-
/**
140-
* Pass data to the job
141-
*/
142-
public function with($data)
183+
public static function with($data)
143184
{
144-
$this->data = $data;
185+
static::$data[] = $data;
145186

146-
return $this;
187+
return new static;
147188
}
148189

149190
public function stack()
@@ -163,14 +204,14 @@ public function getConfig()
163204
'sleep' => 3,
164205
'timeout' => 60,
165206
'tries' => 3,
166-
'data' => $this->data,
207+
'data' => static::$data,
167208
];
168209
}
169210

170211
public function trigger()
171212
{
172213
$this->queue->setJobStatus($this->job['id'], 'processing');
173-
$this->handle(...$this->data);
214+
$this->handle(...static::$data);
174215
}
175216

176217
public function removeFromQueue()

0 commit comments

Comments
 (0)