Skip to content

Chore/refactor hash locking #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
fail-fast: true
matrix:
php: [7.3, 7.4, 8.0, 8.1]
stability:
stability:
- prefer-stable

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}
Expand Down
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,34 @@
"name": "always-open/laravel-process-stamps",
"description": "Logs which process created or modified a record",
"keywords": [
"always open",
"always-open",
"laravel-process-stamps",
"laravel",
"logging"
],
"homepage": "https://github.com/always-open/laravel-process-stamps",
"license": "MIT",
"authors": [
{
"name": "Tom Schlick",
"role": "Developer"
},
{
"name": "Quentin Schmick",
"email": "[email protected]",
"role": "Developer"
},
{
"name": "Lucas Graciano",
"role": "Developer"
},
{
"name": "Always Open",
"homepage": "https://github.com/always-open",
"role": "Organization"
}
],
"require": {
"php": "^7.3|^8.0",
"laravel/framework": "^8.0|^9.0"
Expand Down
43 changes: 23 additions & 20 deletions src/ProcessStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AlwaysOpen\ProcessStamps;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down Expand Up @@ -38,12 +39,12 @@ public function getTable() : string
}

/**
* @param array $process
* @param array $process
* @param null|string $hash
*
* @return ProcessStamp
*
* @throws ModelNotFoundException
* @throws ModelNotFoundException|Exception
*/
public static function firstOrCreateByProcess(array $process, ?string $hash = null) : self
{
Expand All @@ -55,31 +56,33 @@ public static function firstOrCreateByProcess(array $process, ?string $hash = nu
$hash = static::makeProcessHash($process);
}

$parent = null;
return retry(4, function() use ($hash, $process) {
$parent = null;

if (config('process-stamps.resolve_recursive') && ! empty($process['parent_name'])) {
$parent = static::firstOrCreateByProcess(static::getProcessName($process['type'], $process['parent_name']));
}
if (config('process-stamps.resolve_recursive') && ! empty($process['parent_name'])) {
$parent = static::firstOrCreateByProcess(static::getProcessName($process['type'], $process['parent_name']));
}

$lock = Cache::lock('process-stamps-hash-create-' . $hash, 12);
$lock->block(3);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember trying something like this before and we ran into deadlocks but I was never able to reproduce it. I think it had something to do with Observers 🤔


return retry(4, function() use ($hash, $process, $parent) {
$stamp = static::firstWhere('hash', $hash);

/*
* If stamp does not exist in the database yet, go ahead and obtain a lock to create it.
* This specifically doesn't lock as the first step to avoid all calls obtaining a lock from the cache if
* the item already exists in the DB.
*/
if (! $stamp) {
Cache::lock('process-stamps-hash-create-' . $hash, 10)
->get(function () use (&$stamp, $hash, $process, $parent) {
$stamp = static::firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
});
/*
* If stamp does not exist in the database yet, go ahead and obtain a lock to create it.
* This specifically doesn't lock as the first step to avoid all calls obtaining a lock from the cache
* if the item already exists in the DB.
*/
$stamp = static::firstOrCreate(['hash' => $hash], [
'name' => trim($process['name']),
'type' => $process['type'],
'parent_id' => optional($parent)->getKey(),
]);
}

$lock->release();

if (null === $stamp) {
throw new ModelNotFoundException();
}
Expand Down