44
55use  Exception ;
66use  Illuminate \Bus \Batchable ;
7- use  Illuminate \Bus \UniqueLock ;
87use  Illuminate \Contracts \Bus \Dispatcher ;
9- use  Illuminate \Contracts \Cache \Repository  as  Cache ;
108use  Illuminate \Contracts \Container \Container ;
119use  Illuminate \Contracts \Encryption \Encrypter ;
1210use  Illuminate \Contracts \Queue \Job ;
13- use  Illuminate \Contracts \Queue \ShouldBeUnique ;
1411use  Illuminate \Contracts \Queue \ShouldBeUniqueUntilProcessing ;
1512use  Illuminate \Database \Eloquent \ModelNotFoundException ;
1613use  Illuminate \Pipeline \Pipeline ;
@@ -60,17 +57,19 @@ public function call(Job $job, array $data)
6057                $ job$ this getCommand ($ data
6158            );
6259        } catch  (ModelNotFoundException $ e
60+             $ this ensureUniqueJobLockIsReleased ($ data
61+ 
6362            return  $ this handleModelNotFound ($ job$ e
6463        }
6564
6665        if  ($ commandinstanceof  ShouldBeUniqueUntilProcessing) {
67-             $ this ensureUniqueJobLockIsReleased ($ command 
66+             $ this ensureUniqueJobLockIsReleased ($ data 
6867        }
6968
7069        $ this dispatchThroughMiddleware ($ job$ command
7170
7271        if  (! $ jobisReleased () && ! $ commandinstanceof  ShouldBeUniqueUntilProcessing) {
73-             $ this ensureUniqueJobLockIsReleased ($ command 
72+             $ this ensureUniqueJobLockIsReleased ($ data 
7473        }
7574
7675        if  (! $ jobhasFailed () && ! $ jobisReleased ()) {
@@ -83,6 +82,32 @@ public function call(Job $job, array $data)
8382        }
8483    }
8584
85+     /** 
86+      * Get the unserialized object from the given payload. 
87+      * 
88+      * @param  string  $key 
89+      * @param  array  $data 
90+      * @return mixed 
91+      */ 
92+     protected  function  getUnserializedItem (string  $ keyarray  $ data
93+     {
94+         if  (isset ($ data$ key
95+             if  (
96+                 str_starts_with ($ data$ key'O: ' ) ||
97+                 $ data$ key'N; ' 
98+             ) {
99+                 return  unserialize ($ data$ key
100+             }
101+ 
102+             if  ($ this container ->bound (Encrypter::class)) {
103+                 return  unserialize ($ this container [Encrypter::class]
104+                                         ->decrypt ($ data$ key
105+             }
106+         }
107+ 
108+         return  null ;
109+     }
110+ 
86111    /** 
87112     * Get the command from the given payload. 
88113     * 
@@ -93,17 +118,25 @@ public function call(Job $job, array $data)
93118     */ 
94119    protected  function  getCommand (array  $ data
95120    {
96-         if  (str_starts_with ($ data'command ' ], 'O: ' )) {
97-             return  unserialize ($ data'command ' ]);
98-         }
99- 
100-         if  ($ this container ->bound (Encrypter::class)) {
101-             return  unserialize ($ this container [Encrypter::class]->decrypt ($ data'command ' ]));
121+         $ command$ this getUnserializedItem ('command ' , $ data
122+         if  ($ commandnull ) {
123+             return  $ command
102124        }
103125
104126        throw  new  RuntimeException ('Unable to extract job payload. ' );
105127    }
106128
129+     /** 
130+      * Get the unique handler from the given payload. 
131+      * 
132+      * @param  array  $data 
133+      * @return \Illuminate\Queue\UniqueHandler|null 
134+      */ 
135+     protected  function  getUniqueHandler (array  $ data
136+     {
137+         return  $ this getUnserializedItem ('uniqueHandler ' , $ data
138+     }
139+ 
107140    /** 
108141     * Dispatch the given job / command through its specified middleware. 
109142     * 
@@ -196,13 +229,14 @@ protected function ensureSuccessfulBatchJobIsRecorded($command)
196229    /** 
197230     * Ensure the lock for a unique job is released. 
198231     * 
199-      * @param  mixed   $command  
232+      * @param  array   $data  
200233     * @return void 
201234     */ 
202-     protected  function  ensureUniqueJobLockIsReleased ($ command 
235+     protected  function  ensureUniqueJobLockIsReleased ($ data 
203236    {
204-         if  ($ commandinstanceof  ShouldBeUnique) {
205-             (new  UniqueLock ($ this container ->make (Cache::class)))->release ($ command
237+         $ handler$ this getUniqueHandler ($ data
238+         if  ($ handlernull ) {
239+             $ handlerwithContainer ($ this container )->release ();
206240        }
207241    }
208242
@@ -246,7 +280,7 @@ public function failed(array $data, $e, string $uuid)
246280        $ command$ this getCommand ($ data
247281
248282        if  (! $ commandinstanceof  ShouldBeUniqueUntilProcessing) {
249-             $ this ensureUniqueJobLockIsReleased ($ command 
283+             $ this ensureUniqueJobLockIsReleased ($ data 
250284        }
251285
252286        if  ($ commandinstanceof  \__PHP_Incomplete_Class) {
0 commit comments