@@ -14,7 +14,7 @@ abstract class Job implements Dispatchable
1414 /**
1515 * Data to pass to the job
1616 */
17- protected static $ data = [];
17+ protected array $ data = [];
1818
1919 /**
2020 * Queue instance
@@ -43,24 +43,10 @@ abstract class Job implements Dispatchable
4343 protected $ delayBeforeRetry = 0 ;
4444
4545 /**
46- * Number of seconds to wait before archiving a job that has not yet been processed
46+ * Number of seconds to wait before archiving a job that has not yet been processed.
47+ * Set to 0 to never expire.
4748 */
48- protected $ expire = 60 ;
49-
50- /**
51- * Force the worker to process the job, even if it has expired or has reached its maximum number of retries
52- */
53- protected $ force = false ;
54-
55- /**
56- * The maximum amount of memory the job is allowed to consume (MB)
57- */
58- protected $ memory = 128 ;
59-
60- /**
61- * The number of seconds a child process can run before being killed.
62- */
63- protected $ timeout = 60 ;
49+ protected $ expire = 3600 ;
6450
6551 /**
6652 * The maximum number of times the job may be attempted.
@@ -83,16 +69,11 @@ public function fromQueue($job, $config, $queue)
8369 $ this ->job = $ job ;
8470 $ this ->queue = $ queue ;
8571
86- if (isset ($ config ['data ' ])) {
87- static ::$ data = $ config ['data ' ];
88- }
72+ $ this ->data = $ config ['data ' ] ?? [];
8973
9074 $ this ->delay = $ config ['delay ' ] ?? 0 ;
9175 $ this ->delayBeforeRetry = $ config ['delayBeforeRetry ' ] ?? 0 ;
92- $ this ->expire = $ config ['expire ' ] ?? 60 ;
93- $ this ->force = $ config ['force ' ] ?? false ;
94- $ this ->memory = $ config ['memory ' ] ?? 128 ;
95- $ this ->timeout = $ config ['timeout ' ] ?? 60 ;
76+ $ this ->expire = $ config ['expire ' ] ?? 3600 ;
9677 $ this ->tries = $ config ['tries ' ] ?? 3 ;
9778
9879 return $ this ;
@@ -108,21 +89,16 @@ public function getJobId()
10889 return $ this ->job ['id ' ];
10990 }
11091
111- /**
112- * Handle delay for job
113- */
114- public function handleDelay ()
115- {
116- echo "Job # {$ this ->job ['id ' ]} is delayed for {$ this ->delay } seconds \n" ;
117- sleep ($ this ->delay );
118- }
119-
12092 /**
12193 * Check if job has expired
12294 */
12395 public function hasExpired ()
12496 {
125- return $ this ->job ['created_at ' ] < (time () - $ this ->expire );
97+ if ((int ) $ this ->expire === 0 ) {
98+ return false ;
99+ }
100+
101+ return ((int ) ($ this ->job ['created_at ' ] ?? time ())) < (time () - $ this ->expire );
126102 }
127103
128104 /**
@@ -131,7 +107,7 @@ public function hasExpired()
131107 public function handleExpiry ()
132108 {
133109 echo "Job # {$ this ->job ['id ' ]} has expired \n" ;
134- $ this ->queue -> pop ( $ this -> job [ ' id ' ] );
110+ $ this ->setStatus ( ' expired ' );
135111 }
136112
137113 /**
@@ -151,12 +127,41 @@ public function setStatus($status)
151127 }
152128
153129 /**
154- * Retry job
130+ * Retry job. If the retry limit has been reached, the job
131+ * is marked as failed with the exception recorded instead.
132+ *
133+ * @param \Throwable|null $exception The exception that caused the failure
134+ */
135+ public function retry (?\Throwable $ exception = null )
136+ {
137+ if (($ this ->job ['retry_count ' ] + 1 ) >= $ this ->tries ) {
138+ $ this ->fail ($ exception );
139+
140+ return ;
141+ }
142+
143+ $ this ->queue ->retryFailedJob (
144+ $ this ->job ['id ' ],
145+ $ this ->job ['retry_count ' ],
146+ $ this ->delayBeforeRetry ?? 0
147+ );
148+ }
149+
150+ /**
151+ * Mark the job as permanently failed and record the exception
152+ *
153+ * @param \Throwable|null $exception The exception that caused the failure
155154 */
156- public function retry ( )
155+ public function fail (? \ Throwable $ exception = null )
157156 {
158- sleep ($ this ->delayBeforeRetry ?? 0 );
159- $ this ->queue ->retryFailedJob ($ this ->job ['id ' ], $ this ->job ['retry_count ' ]);
157+ $ exceptionDump = null ;
158+
159+ if ($ exception ) {
160+ $ trace = explode ("\n" , $ exception ->getTraceAsString ());
161+ $ exceptionDump = $ exception ->getMessage () . "\n" . implode ("\n" , array_slice ($ trace , 0 , 5 ));
162+ }
163+
164+ $ this ->queue ->markJobAsFailed ($ this ->job ['id ' ], $ exceptionDump );
160165 }
161166
162167 /**
@@ -167,30 +172,29 @@ public function retry()
167172 */
168173 public function release ($ delay = 0 )
169174 {
170- sleep ($ delay );
171-
172175 $ this ->queue ->push ([
173176 'class ' => $ this ->job ['class ' ],
174177 'status ' => 'pending ' ,
175178 'retry_count ' => $ this ->job ['retry_count ' ] + 1 ,
179+ 'available_at ' => time () + $ delay ,
176180 'config ' => json_encode ([
177181 'delay ' => $ this ->delay ,
178182 'delayBeforeRetry ' => $ this ->delayBeforeRetry ,
179183 'expire ' => $ this ->expire ,
180- 'force ' => $ this ->force ,
181- 'memory ' => $ this ->memory ,
182- 'timeout ' => $ this ->timeout ,
183184 'tries ' => $ this ->tries ,
184- 'data ' => static :: $ data ,
185+ 'data ' => $ this -> data ,
185186 ]),
186187 ]);
188+
189+ $ this ->removeFromQueue ();
187190 }
188191
189192 public static function with ($ data )
190193 {
191- static ::$ data [] = $ data ;
194+ $ instance = new static ();
195+ $ instance ->data = [$ data ];
192196
193- return new static () ;
197+ return $ instance ;
194198 }
195199
196200 public function stack ()
@@ -204,18 +208,15 @@ public function getConfig()
204208 'delay ' => $ this ->delay ,
205209 'delayBeforeRetry ' => $ this ->delayBeforeRetry ,
206210 'expire ' => $ this ->expire ,
207- 'force ' => $ this ->force ,
208- 'memory ' => $ this ->memory ,
209- 'timeout ' => $ this ->timeout ,
210211 'tries ' => $ this ->tries ,
211- 'data ' => static :: $ data ,
212+ 'data ' => $ this -> data ,
212213 ];
213214 }
214215
215216 public function trigger ()
216217 {
217218 $ this ->queue ->setJobStatus ($ this ->job ['id ' ], 'processing ' );
218- $ this ->handle (...static :: $ data );
219+ $ this ->handle (...$ this -> data );
219220 }
220221
221222 public function removeFromQueue ()
0 commit comments