Skip to content

Commit

Permalink
Add better context to the lock timeout exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
jared-cannon committed Jan 24, 2025
1 parent 9fc0add commit cca8ec4
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Listeners/CreateRewindVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AvocetShores\LaravelRewind\Events\RewindVersionCreating;
use AvocetShores\LaravelRewind\Models\RewindVersion;
use Illuminate\Contracts\Cache\LockTimeoutException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

Expand Down Expand Up @@ -106,15 +107,28 @@ public function handle(RewindVersionCreating $event): void
event(new RewindVersionCreated($model, $rewindVersion));

} catch (LockTimeoutException) {
// If we can't get the lock, just skip this version
Log::warning('Laravel Rewind: Could not acquire lock to record version for '.get_class($model).' with ID '.$model->getKey());

// If we cannot acquire a lock, something is most likely wrong with the environment
$this->handleLockTimeoutException($model);
return;
} finally {
optional($lock)->release();
}
}

protected function handleLockTimeoutException($model): void
{
// Just log for now, but need to consider remediation options for this edge case.
Log::error(sprintf(
'Failed to acquire lock for RewindVersion creation on %s:%s, your versions may be out of sync.',
get_class($model),
$model->getKey(),
), [
'model' => get_class($model),
'model_key' => $model->getKey(),
'changes' => $model->getDirty(),
]);
}

protected function rebuildHeadVersion($model): array
{
$data = [];
Expand Down

0 comments on commit cca8ec4

Please sign in to comment.