@@ -98,19 +98,31 @@ public function lock($job)
9898
9999 case self ::ACTIVITY :
100100 $ locked = false ;
101- if ($ workflowSemaphore === 0 ) {
102- $ job ->key = $ this ->getActivitySemaphoreKey () . ': ' . (string ) Str::uuid ();
103- $ locked = $ this ->compareAndSet (
104- $ this ->getActivitySemaphoreKey (),
105- $ activitySemaphores ,
106- array_merge ($ activitySemaphores , [$ job ->key ])
107- );
108- if ($ locked ) {
109- if ($ this ->expiresAfter ) {
110- $ this ->cache ->put ($ job ->key , 1 , $ this ->expiresAfter );
111- } else {
112- $ this ->cache ->put ($ job ->key , 1 );
101+ $ maxAttempts = 5 ;
102+ for ($ attempt = 0 ; $ attempt < $ maxAttempts ; $ attempt ++) {
103+ if ($ attempt > 0 ) {
104+ $ workflowSemaphore = (int ) $ this ->cache ->get ($ this ->getWorkflowSemaphoreKey (), 0 );
105+ if ($ workflowSemaphore !== 0 ) {
106+ break ;
107+ }
108+ $ activitySemaphores = $ this ->cache ->get ($ this ->getActivitySemaphoreKey (), []);
109+ }
110+ if ($ workflowSemaphore === 0 ) {
111+ $ job ->key = $ this ->getActivitySemaphoreKey () . ': ' . (string ) Str::uuid ();
112+ $ locked = $ this ->compareAndSet (
113+ $ this ->getActivitySemaphoreKey (),
114+ $ activitySemaphores ,
115+ array_merge ($ activitySemaphores , [$ job ->key ])
116+ );
117+ if ($ locked ) {
118+ if ($ this ->expiresAfter ) {
119+ $ this ->cache ->put ($ job ->key , 1 , $ this ->expiresAfter );
120+ } else {
121+ $ this ->cache ->put ($ job ->key , 1 );
122+ }
123+ break ;
113124 }
125+ usleep (500 );
114126 }
115127 }
116128 break ;
@@ -153,7 +165,7 @@ private function unlockActivity($job): bool
153165 $ retries = 0 ;
154166
155167 while ($ retries < $ maxRetries ) {
156- $ lock = $ this ->cache ->lock ($ this ->getLockKey ());
168+ $ lock = $ this ->cache ->lock ($ this ->getLockKey (), 5 );
157169
158170 if (! $ lock ->get ()) {
159171 $ retries ++;
@@ -185,25 +197,35 @@ private function unlockActivity($job): bool
185197
186198 private function compareAndSet ($ key , $ expectedValue , $ newValue , $ expiresAfter = 0 )
187199 {
188- $ lock = $ this ->cache ->lock ($ this ->getLockKey ());
200+ $ maxRetries = 10 ;
201+ $ retries = 0 ;
189202
190- if ($ lock ->get ()) {
191- try {
192- $ currentValue = $ this ->cache ->get ($ key , $ expectedValue );
203+ while ($ retries < $ maxRetries ) {
204+ $ lock = $ this ->cache ->lock ($ this ->getLockKey (), 5 );
193205
194- $ currentValue = is_int ($ expectedValue ) ? (int ) $ currentValue : $ currentValue ;
206+ if ($ lock ->get ()) {
207+ try {
208+ $ currentValue = $ this ->cache ->get ($ key , $ expectedValue );
195209
196- if ($ currentValue === $ expectedValue ) {
197- if ($ expiresAfter ) {
198- $ this ->cache ->put ($ key , $ newValue , $ expiresAfter );
199- } else {
200- $ this ->cache ->put ($ key , $ newValue );
210+ $ currentValue = is_int ($ expectedValue ) ? (int ) $ currentValue : $ currentValue ;
211+
212+ if ($ currentValue === $ expectedValue ) {
213+ if ($ expiresAfter ) {
214+ $ this ->cache ->put ($ key , $ newValue , $ expiresAfter );
215+ } else {
216+ $ this ->cache ->put ($ key , $ newValue );
217+ }
218+ return true ;
201219 }
202- return true ;
220+ } finally {
221+ $ lock ->release ();
203222 }
204- } finally {
205- $ lock -> release () ;
223+
224+ return false ;
206225 }
226+
227+ $ retries ++;
228+ usleep (1000 );
207229 }
208230
209231 return false ;
0 commit comments