From aea8289f105d3463605bb14dcbd0a29be7212d0a Mon Sep 17 00:00:00 2001 From: "quentin.schmick" Date: Mon, 14 Jun 2021 07:12:06 -0400 Subject: [PATCH 1/3] Updated stamp creation to leverage the retry helper method --- src/ProcessStamp.php | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/ProcessStamp.php b/src/ProcessStamp.php index a19c70f..3fa71b4 100644 --- a/src/ProcessStamp.php +++ b/src/ProcessStamp.php @@ -3,10 +3,10 @@ namespace OrisIntel\ProcessStamps; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\DB; class ProcessStamp extends Model { @@ -42,6 +42,8 @@ public function getTable() : string * @param null|string $hash * * @return ProcessStamp + * + * @throws ModelNotFoundException */ public static function firstOrCreateByProcess(array $process, ?string $hash = null) : self { @@ -59,23 +61,31 @@ public static function firstOrCreateByProcess(array $process, ?string $hash = nu $parent = static::firstOrCreateByProcess(static::getProcessName($process['type'], $process['parent_name'])); } - $stamp = static::where('hash', $hash)->first(); + 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. - */ - 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 (null === $stamp) { + throw new ModelNotFoundException(); + } - return $stamp; + return $stamp; + }, 25); } /** From f00c1cbfe9fc8fc1852362d0c7b0a348cf15b9c1 Mon Sep 17 00:00:00 2001 From: Tom Schlick Date: Tue, 15 Jun 2021 02:38:27 +0000 Subject: [PATCH 2/3] rename the vendor from orisintel to always-open --- README.md | 12 ++++++------ SECURITY.md | 2 +- composer.json | 25 ++++++------------------- src/ProcessStamp.php | 2 +- src/ProcessStampable.php | 2 +- src/ProcessStampsServiceProvider.php | 2 +- tests/Fakes/Post.php | 4 ++-- tests/ParentTest.php | 6 +++--- tests/ProcessStampModelTest.php | 4 ++-- tests/TestCase.php | 4 ++-- 10 files changed, 25 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 765c88e..566a505 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Laravel Process Stamps -[![Latest Version on Packagist](https://img.shields.io/packagist/v/orisintel/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-process-stamps) -[![Build Status](https://img.shields.io/github/workflow/status/orisintel/laravel-process-stamps/tests?style=flat-square)](https://github.com/orisintel/laravel-process-stamps/actions?query=workflow%3Atests) -[![Total Downloads](https://img.shields.io/packagist/dt/orisintel/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/orisintel/laravel-process-stamps) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/always-open/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/always-open/laravel-process-stamps) +[![Build Status](https://img.shields.io/github/workflow/status/always-open/laravel-process-stamps/tests?style=flat-square)](https://github.com/always-open/laravel-process-stamps/actions?query=workflow%3Atests) +[![Total Downloads](https://img.shields.io/packagist/dt/always-open/laravel-process-stamps.svg?style=flat-square)](https://packagist.org/packages/always-open/laravel-process-stamps) It is sometimes very useful to know which process created or modified a particular record in your database. This package provides a trait to add to your Laravel models which automatically logs that for you. @@ -11,13 +11,13 @@ It is sometimes very useful to know which process created or modified a particul You can install the package via composer: ```bash -composer require orisintel/laravel-process-stamps +composer require always-open/laravel-process-stamps ``` ## Configuration ``` php -php artisan vendor:publish --provider="\OrisIntel\ProcessStamps\ProcessStampsServiceProvider" +php artisan vendor:publish --provider="\AlwaysOpen\ProcessStamps\ProcessStampsServiceProvider" ``` Running the above command will publish both the migration and the config file. @@ -84,7 +84,7 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. ### Security -If you discover any security related issues, please email [opensource@orisintel.com](mailto:opensource@orisintel.com) instead of using the issue tracker. +If you discover any security related issues, please email @tomschlick or @qschmick directly instead of using the issue tracker. ## Credits diff --git a/SECURITY.md b/SECURITY.md index 7498fa5..d75931c 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -2,4 +2,4 @@ ## Reporting a Vulnerability -If you encounter what you believe to be a security vulnerability, please report it immediately to opensource@orisintel.com. Please include your name, your email address, the url of the package, and a description of the suspected issue. +If you encounter what you believe to be a security vulnerability, please report it immediately to @tomschlick or @qschmick. Please include your name, your email address, the url of the package, and a description of the suspected issue. diff --git a/composer.json b/composer.json index 3ef9fb8..7536d06 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,14 @@ { - "name": "orisintel/laravel-process-stamps", + "name": "always-open/laravel-process-stamps", "description": "Logs which process created or modified a record", "keywords": [ - "orisintel", + "always-open", "laravel-process-stamps", "laravel", "logging" ], - "homepage": "https://github.com/orisintel/laravel-process-stamps", + "homepage": "https://github.com/always-open/laravel-process-stamps", "license": "MIT", - "authors": [ - { - "name": "Tom Schlick", - "email": "tschlick@orisintel.com", - "role": "Developer" - }, - { - "name": "ORIS Intelligence", - "email": "developers@orisintel.com", - "homepage": "https://orisintel.com", - "role": "Organization" - } - ], "require": { "php": "^7.3|^8.0", "laravel/framework": "^8.0" @@ -35,12 +22,12 @@ }, "autoload": { "psr-4": { - "OrisIntel\\ProcessStamps\\": "src" + "AlwaysOpen\\ProcessStamps\\": "src" } }, "autoload-dev": { "psr-4": { - "OrisIntel\\ProcessStamps\\Tests\\": "tests" + "AlwaysOpen\\ProcessStamps\\Tests\\": "tests" } }, "scripts": { @@ -54,7 +41,7 @@ "extra": { "laravel": { "providers": [ - "OrisIntel\\ProcessStamps\\ProcessStampsServiceProvider" + "AlwaysOpen\\ProcessStamps\\ProcessStampsServiceProvider" ] } } diff --git a/src/ProcessStamp.php b/src/ProcessStamp.php index a19c70f..ab2b25d 100644 --- a/src/ProcessStamp.php +++ b/src/ProcessStamp.php @@ -1,6 +1,6 @@ Date: Tue, 15 Jun 2021 10:23:04 -0400 Subject: [PATCH 3/3] Update tests.yml --- .github/workflows/tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d03ca3c..4453cc9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -2,6 +2,7 @@ name: tests on: push: + pull_request: schedule: - cron: '0 0 * * *'