[13.x] Release the unique lock when pushing a job to the queue fails
Merged
SU
19d ago+47+47+01234567890123456789-8-8-0123456789
When pushing a unique job to the queue throws, for example because Redis is unreachable or SQS is throttling, the unique lock acquired just before stays held. Once the queue is back, the job can't be dispatched again until the lock expires, and with the default uniqueFor of 0 it never does:
class SyncInventory implements ShouldQueue, ShouldBeUnique
{
//
}
SyncInventory::dispatch(); // RedisException: Connection refused
// later, with the queue back up
SyncInventory::dispatch(); // skipped, the lock is still held
PendingDispatch acquires the lock in shouldDispatch() and then pushes the job, but nothing releases it if the push fails. The unique job information added to the context is also left behind in that case.
This releases the lock when dispatching throws and rethrows the exception, and removes the context information in a finally. The release goes through UniqueLock::release(), so it only releases the lock this dispatch owns.