1313
1414use Exception ;
1515use think \App ;
16+ use think \helper \Arr ;
1617
1718abstract 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 /**
0 commit comments