From cda41fbd460947e8e8f1d8cc9ca4e43c8991c2db Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 17 Dec 2021 18:04:34 +1100 Subject: [PATCH 1/3] Tests for redis vs database performance --- app/Console/Commands/CheckDb.php | 9 +-- app/Console/Commands/CreateSingleAccount.php | 25 ++----- app/Console/Commands/CreateTestData.php | 17 ++--- app/Console/Commands/DemoMode.php | 17 +---- app/Console/Commands/ParallelCheckData.php | 2 +- app/Console/Commands/PostUpdate.php | 1 - app/Console/Kernel.php | 11 ++- app/Services/Invoice/ApplyNumber.php | 1 - tests/Unit/RedisVsDatabaseTest.php | 76 ++++++++++++++++++++ 9 files changed, 101 insertions(+), 58 deletions(-) create mode 100644 tests/Unit/RedisVsDatabaseTest.php diff --git a/app/Console/Commands/CheckDb.php b/app/Console/Commands/CheckDb.php index 63e4754f5b..a543f9f019 100644 --- a/app/Console/Commands/CheckDb.php +++ b/app/Console/Commands/CheckDb.php @@ -67,20 +67,13 @@ use Symfony\Component\Console\Input\InputOption; */ class CheckDb extends Command { - /** - * @var string - */ + protected $signature = 'ninja:check-db'; - /** - * @var string - */ protected $description = 'Check MultiDB'; - protected $log = ''; - private $entities = [ Account::class, Activity::class, diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index fe5c16f348..6f508b06e3 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -56,13 +56,9 @@ use stdClass; class CreateSingleAccount extends Command { use MakesHash, GeneratesCounter; - /** - * @var string - */ + protected $description = 'Create Single Sample Account'; - /** - * @var string - */ + protected $signature = 'ninja:create-single-account {gateway=all} {--database=db-ninja-01}'; protected $invoice_repo; @@ -71,18 +67,6 @@ class CreateSingleAccount extends Command protected $gateway; - /** - * Create a new command instance. - * - * @param InvoiceRepository $invoice_repo - */ - public function __construct(InvoiceRepository $invoice_repo) - { - parent::__construct(); - - $this->invoice_repo = $invoice_repo; - } - /** * Execute the console command. * @@ -94,6 +78,11 @@ class CreateSingleAccount extends Command if(config('ninja.is_docker')) return; + if (!$this->confirm('Are you sure you want to inject dummy data?')) + return; + + $this->invoice_repo = new InvoiceRepository(); + MultiDB::setDb($this->option('database')); $this->info(date('r').' Create Single Sample Account...'); diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 6d1426c5ed..67dc7e5cd3 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -59,18 +59,6 @@ class CreateTestData extends Command protected $invoice_repo; - /** - * Create a new command instance. - * - * @param InvoiceRepository $invoice_repo - */ - public function __construct(InvoiceRepository $invoice_repo) - { - parent::__construct(); - - $this->invoice_repo = $invoice_repo; - } - /** * Execute the console command. * @@ -80,6 +68,11 @@ class CreateTestData extends Command { if(config('ninja.is_docker')) return; + + if (!$this->confirm('Are you sure you want to inject dummy data?')) + return; + + $this->invoice_repo = new InvoiceRepository(); $this->info(date('r').' Running CreateTestData...'); $this->count = $this->argument('count'); diff --git a/app/Console/Commands/DemoMode.php b/app/Console/Commands/DemoMode.php index 3119ac21b6..5e5cfcbd70 100644 --- a/app/Console/Commands/DemoMode.php +++ b/app/Console/Commands/DemoMode.php @@ -53,28 +53,15 @@ class DemoMode extends Command { use MakesHash, GeneratesCounter; - protected $name = 'ninja:demo-mode'; - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'ninja:demo-mode'; - /** - * The console command description. - * - * @var string - */ protected $description = 'Setup demo mode'; protected $invoice_repo; - public function __construct(InvoiceRepository $invoice_repo) + public function __construct() { parent::__construct(); - - $this->invoice_repo = $invoice_repo; } /** @@ -89,6 +76,8 @@ class DemoMode extends Command if(config('ninja.is_docker')) return; + $this->invoice_repo = new InvoiceRepository(); + $cached_tables = config('ninja.cached_tables'); foreach ($cached_tables as $name => $class) { diff --git a/app/Console/Commands/ParallelCheckData.php b/app/Console/Commands/ParallelCheckData.php index ca432b4f72..0c6d778f5c 100644 --- a/app/Console/Commands/ParallelCheckData.php +++ b/app/Console/Commands/ParallelCheckData.php @@ -39,7 +39,7 @@ class ParallelCheckData extends Command /** * @var string */ - protected $name = 'ninja:pcheck-data'; + protected $signature = 'ninja:pcheck-data'; /** * @var string diff --git a/app/Console/Commands/PostUpdate.php b/app/Console/Commands/PostUpdate.php index 1e5025dac2..491a60c7fd 100644 --- a/app/Console/Commands/PostUpdate.php +++ b/app/Console/Commands/PostUpdate.php @@ -17,7 +17,6 @@ use Illuminate\Support\Facades\Artisan; class PostUpdate extends Command { - protected $name = 'ninja:post-update'; /** * The name and signature of the console command. * diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index caf7f5a58c..e03d3b78f6 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -80,10 +80,14 @@ class Kernel extends ConsoleKernel /* Run hosted specific jobs */ if (Ninja::isHosted()) { - $schedule->job(new AdjustEmailQuota)->dailyAt('23:00')->withoutOverlapping(); + $schedule->job(new AdjustEmailQuota)->dailyAt('23:30')->withoutOverlapping(); + $schedule->job(new SendFailedEmails)->daily()->withoutOverlapping(); - $schedule->command('ninja:check-data --database=db-ninja-01')->daily()->withoutOverlapping(); - $schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('00:05')->withoutOverlapping(); + + $schedule->command('ninja:check-data --database=db-ninja-01')->daily('00:50')->withoutOverlapping(); + + $schedule->command('ninja:check-data --database=db-ninja-02')->dailyAt('00:55')->withoutOverlapping(); + $schedule->command('ninja:s3-cleanup')->dailyAt('23:15')->withoutOverlapping(); } @@ -91,6 +95,7 @@ class Kernel extends ConsoleKernel if(config('queue.default') == 'database' && Ninja::isSelfHost() && config('ninja.internal_queue_enabled') && !config('ninja.is_docker')) { $schedule->command('queue:work')->everyMinute()->withoutOverlapping(); + $schedule->command('queue:restart')->everyFiveMinutes()->withoutOverlapping(); } diff --git a/app/Services/Invoice/ApplyNumber.php b/app/Services/Invoice/ApplyNumber.php index 71f0c81ea2..786051fb18 100644 --- a/app/Services/Invoice/ApplyNumber.php +++ b/app/Services/Invoice/ApplyNumber.php @@ -48,7 +48,6 @@ class ApplyNumber extends AbstractService break; default: - // code... break; } diff --git a/tests/Unit/RedisVsDatabaseTest.php b/tests/Unit/RedisVsDatabaseTest.php new file mode 100644 index 0000000000..f7b1b76746 --- /dev/null +++ b/tests/Unit/RedisVsDatabaseTest.php @@ -0,0 +1,76 @@ +markTestSkipped('Skip test no company gateways installed'); + + + } + + public function testRedisSpeed() + { + $start = microtime(true); + + $currencies = Cache::get('currencies'); + + $currency = $currencies->filter(function ($item) { + return $item->id == 17; + })->first(); + + + $total_time = microtime(true) - $start; + + $this->assertTrue(true); + // nlog($total_time); + //0.0012960433959961 + } + + + public function testDbSpeed() + { + $start = microtime(true); + + $currency = Currency::find(17); + + $total_time = microtime(true) - $start; + + + $this->assertTrue(true); + // nlog($total_time); + // 0.006152868270874 + + } + + + +} + + + + + \ No newline at end of file From c76cb3eb7c5000caebed3328fef5702a7e84717a Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 17 Dec 2021 18:57:26 +1100 Subject: [PATCH 2/3] Delay events slightly to prevent race conditions --- app/Listeners/Invoice/CreateInvoiceActivity.php | 2 ++ app/Listeners/Invoice/InvoiceArchivedActivity.php | 2 ++ app/Listeners/Invoice/InvoiceCancelledActivity.php | 2 ++ app/Listeners/Invoice/InvoiceCreatedNotification.php | 2 ++ app/Listeners/Invoice/InvoiceDeletedActivity.php | 2 ++ app/Listeners/Invoice/InvoiceEmailActivity.php | 2 ++ app/Listeners/Invoice/InvoiceEmailFailedActivity.php | 2 ++ app/Listeners/Invoice/InvoiceEmailedNotification.php | 2 ++ app/Listeners/Invoice/InvoiceFailedEmailNotification.php | 2 ++ app/Listeners/Invoice/InvoicePaidActivity.php | 2 ++ app/Listeners/Invoice/InvoiceReminderEmailActivity.php | 2 ++ app/Listeners/Invoice/InvoiceRestoredActivity.php | 2 ++ app/Listeners/Invoice/InvoiceReversedActivity.php | 2 ++ app/Listeners/Invoice/InvoiceViewedActivity.php | 2 ++ app/Listeners/Invoice/UpdateInvoiceActivity.php | 2 ++ app/Listeners/Invoice/UpdateInvoiceInvitations.php | 3 +++ app/Listeners/Payment/PaymentEmailFailureActivity.php | 2 ++ app/Listeners/Payment/PaymentEmailedActivity.php | 2 ++ app/Listeners/Payment/PaymentNotification.php | 3 +++ app/Listeners/Payment/PaymentRestoredActivity.php | 2 ++ app/Listeners/Quote/CreateQuoteInvitation.php | 2 ++ app/Listeners/Quote/QuoteApprovedActivity.php | 2 ++ app/Listeners/Quote/QuoteApprovedNotification.php | 2 ++ app/Listeners/Quote/QuoteApprovedWebhook.php | 3 ++- app/Listeners/Quote/QuoteArchivedActivity.php | 2 ++ app/Listeners/Quote/QuoteCreatedNotification.php | 2 ++ app/Listeners/Quote/QuoteDeletedActivity.php | 2 ++ app/Listeners/Quote/QuoteEmailActivity.php | 2 ++ app/Listeners/Quote/QuoteEmailedNotification.php | 3 +++ app/Listeners/Quote/QuoteRestoredActivity.php | 2 ++ app/Listeners/Quote/QuoteViewedActivity.php | 2 ++ app/Services/Payment/UpdateInvoicePayment.php | 9 ++++++++- app/Utils/Traits/MakesInvoiceValues.php | 6 +++--- tests/Unit/RedisVsDatabaseTest.php | 8 +++----- 34 files changed, 79 insertions(+), 10 deletions(-) diff --git a/app/Listeners/Invoice/CreateInvoiceActivity.php b/app/Listeners/Invoice/CreateInvoiceActivity.php index 991a095f31..062494a08d 100644 --- a/app/Listeners/Invoice/CreateInvoiceActivity.php +++ b/app/Listeners/Invoice/CreateInvoiceActivity.php @@ -21,6 +21,8 @@ class CreateInvoiceActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceArchivedActivity.php b/app/Listeners/Invoice/InvoiceArchivedActivity.php index ff4800f41c..1ceff8ede0 100644 --- a/app/Listeners/Invoice/InvoiceArchivedActivity.php +++ b/app/Listeners/Invoice/InvoiceArchivedActivity.php @@ -21,6 +21,8 @@ class InvoiceArchivedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceCancelledActivity.php b/app/Listeners/Invoice/InvoiceCancelledActivity.php index 80f1f55c7a..0e50683a3e 100644 --- a/app/Listeners/Invoice/InvoiceCancelledActivity.php +++ b/app/Listeners/Invoice/InvoiceCancelledActivity.php @@ -21,6 +21,8 @@ class InvoiceCancelledActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceCreatedNotification.php b/app/Listeners/Invoice/InvoiceCreatedNotification.php index 8d1ed9b386..4f6d117f81 100644 --- a/app/Listeners/Invoice/InvoiceCreatedNotification.php +++ b/app/Listeners/Invoice/InvoiceCreatedNotification.php @@ -24,6 +24,8 @@ class InvoiceCreatedNotification implements ShouldQueue { use UserNotifies; + public $delay = 5; + public function __construct() { } diff --git a/app/Listeners/Invoice/InvoiceDeletedActivity.php b/app/Listeners/Invoice/InvoiceDeletedActivity.php index f7bed8b892..cdaf1d335c 100644 --- a/app/Listeners/Invoice/InvoiceDeletedActivity.php +++ b/app/Listeners/Invoice/InvoiceDeletedActivity.php @@ -21,6 +21,8 @@ class InvoiceDeletedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceEmailActivity.php b/app/Listeners/Invoice/InvoiceEmailActivity.php index 7515709a2e..28d4253954 100644 --- a/app/Listeners/Invoice/InvoiceEmailActivity.php +++ b/app/Listeners/Invoice/InvoiceEmailActivity.php @@ -21,6 +21,8 @@ class InvoiceEmailActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceEmailFailedActivity.php b/app/Listeners/Invoice/InvoiceEmailFailedActivity.php index 530c4929e1..72a306b344 100644 --- a/app/Listeners/Invoice/InvoiceEmailFailedActivity.php +++ b/app/Listeners/Invoice/InvoiceEmailFailedActivity.php @@ -21,6 +21,8 @@ class InvoiceEmailFailedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 3918c630f1..dd1f02c401 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -24,6 +24,8 @@ class InvoiceEmailedNotification implements ShouldQueue { use UserNotifies; + public $delay = 5; + public function __construct() { } diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index ac89010cad..5603e9450a 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -24,6 +24,8 @@ class InvoiceFailedEmailNotification { use UserNotifies; + public $delay = 5; + public function __construct() { } diff --git a/app/Listeners/Invoice/InvoicePaidActivity.php b/app/Listeners/Invoice/InvoicePaidActivity.php index c1b9e68784..5bc5d42a6f 100644 --- a/app/Listeners/Invoice/InvoicePaidActivity.php +++ b/app/Listeners/Invoice/InvoicePaidActivity.php @@ -21,6 +21,8 @@ class InvoicePaidActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceReminderEmailActivity.php b/app/Listeners/Invoice/InvoiceReminderEmailActivity.php index aba48ce03c..e2a197339f 100644 --- a/app/Listeners/Invoice/InvoiceReminderEmailActivity.php +++ b/app/Listeners/Invoice/InvoiceReminderEmailActivity.php @@ -20,6 +20,8 @@ class InvoiceReminderEmailActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceRestoredActivity.php b/app/Listeners/Invoice/InvoiceRestoredActivity.php index b0671f45f0..fcf30eaeb3 100644 --- a/app/Listeners/Invoice/InvoiceRestoredActivity.php +++ b/app/Listeners/Invoice/InvoiceRestoredActivity.php @@ -21,6 +21,8 @@ class InvoiceRestoredActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceReversedActivity.php b/app/Listeners/Invoice/InvoiceReversedActivity.php index b7b26ba33e..671d1be983 100644 --- a/app/Listeners/Invoice/InvoiceReversedActivity.php +++ b/app/Listeners/Invoice/InvoiceReversedActivity.php @@ -21,6 +21,8 @@ class InvoiceReversedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/InvoiceViewedActivity.php b/app/Listeners/Invoice/InvoiceViewedActivity.php index c876b28fd6..091d3f53b3 100644 --- a/app/Listeners/Invoice/InvoiceViewedActivity.php +++ b/app/Listeners/Invoice/InvoiceViewedActivity.php @@ -21,6 +21,8 @@ class InvoiceViewedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/UpdateInvoiceActivity.php b/app/Listeners/Invoice/UpdateInvoiceActivity.php index 232dc69b0f..0228d6de5e 100644 --- a/app/Listeners/Invoice/UpdateInvoiceActivity.php +++ b/app/Listeners/Invoice/UpdateInvoiceActivity.php @@ -21,6 +21,8 @@ class UpdateInvoiceActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Invoice/UpdateInvoiceInvitations.php b/app/Listeners/Invoice/UpdateInvoiceInvitations.php index 8a0ef7173f..55c1c64682 100644 --- a/app/Listeners/Invoice/UpdateInvoiceInvitations.php +++ b/app/Listeners/Invoice/UpdateInvoiceInvitations.php @@ -16,6 +16,9 @@ use Illuminate\Contracts\Queue\ShouldQueue; class UpdateInvoiceInvitations implements ShouldQueue { + + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Payment/PaymentEmailFailureActivity.php b/app/Listeners/Payment/PaymentEmailFailureActivity.php index afd6292700..873d70ac18 100644 --- a/app/Listeners/Payment/PaymentEmailFailureActivity.php +++ b/app/Listeners/Payment/PaymentEmailFailureActivity.php @@ -19,6 +19,8 @@ class PaymentEmailFailureActivity implements ShouldQueue { use UserNotifies; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Payment/PaymentEmailedActivity.php b/app/Listeners/Payment/PaymentEmailedActivity.php index dfef8e2d1b..48bff9abf0 100644 --- a/app/Listeners/Payment/PaymentEmailedActivity.php +++ b/app/Listeners/Payment/PaymentEmailedActivity.php @@ -17,6 +17,8 @@ use Illuminate\Contracts\Queue\ShouldQueue; class PaymentEmailedActivity implements ShouldQueue { + public $delay = 5; + use UserNotifies; /** diff --git a/app/Listeners/Payment/PaymentNotification.php b/app/Listeners/Payment/PaymentNotification.php index afe1d40571..81e5edced9 100644 --- a/app/Listeners/Payment/PaymentNotification.php +++ b/app/Listeners/Payment/PaymentNotification.php @@ -26,6 +26,9 @@ class PaymentNotification implements ShouldQueue { use UserNotifies; + public $delay = 5; + + /** * Create the event listener. * diff --git a/app/Listeners/Payment/PaymentRestoredActivity.php b/app/Listeners/Payment/PaymentRestoredActivity.php index 5d89d7a8b7..842980a25a 100644 --- a/app/Listeners/Payment/PaymentRestoredActivity.php +++ b/app/Listeners/Payment/PaymentRestoredActivity.php @@ -21,6 +21,8 @@ class PaymentRestoredActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Quote/CreateQuoteInvitation.php b/app/Listeners/Quote/CreateQuoteInvitation.php index 9c5f08a254..d0d7d07974 100644 --- a/app/Listeners/Quote/CreateQuoteInvitation.php +++ b/app/Listeners/Quote/CreateQuoteInvitation.php @@ -21,6 +21,8 @@ class CreateQuoteInvitation implements ShouldQueue { use MakesHash; + public $delay = 5; + /** * Handle the event. * diff --git a/app/Listeners/Quote/QuoteApprovedActivity.php b/app/Listeners/Quote/QuoteApprovedActivity.php index 2e0c7c79f5..f79ceec5e8 100644 --- a/app/Listeners/Quote/QuoteApprovedActivity.php +++ b/app/Listeners/Quote/QuoteApprovedActivity.php @@ -21,6 +21,8 @@ class QuoteApprovedActivity implements ShouldQueue { protected $activity_repo; + public $delay = 5; + /** * Create the event listener. * diff --git a/app/Listeners/Quote/QuoteApprovedNotification.php b/app/Listeners/Quote/QuoteApprovedNotification.php index 591e2a8b81..f5911c48cf 100644 --- a/app/Listeners/Quote/QuoteApprovedNotification.php +++ b/app/Listeners/Quote/QuoteApprovedNotification.php @@ -25,6 +25,8 @@ class QuoteApprovedNotification implements ShouldQueue { use UserNotifies; + public $delay = 5; + public function __construct() { } diff --git a/app/Listeners/Quote/QuoteApprovedWebhook.php b/app/Listeners/Quote/QuoteApprovedWebhook.php index a15ad3e758..c5a230a26b 100644 --- a/app/Listeners/Quote/QuoteApprovedWebhook.php +++ b/app/Listeners/Quote/QuoteApprovedWebhook.php @@ -18,7 +18,8 @@ use Illuminate\Contracts\Queue\ShouldQueue; class QuoteApprovedWebhook implements ShouldQueue { - + public $delay = 5; + public function __construct() { } diff --git a/app/Listeners/Quote/QuoteArchivedActivity.php b/app/Listeners/Quote/QuoteArchivedActivity.php index 27add2bbad..72f2d9c242 100644 --- a/app/Listeners/Quote/QuoteArchivedActivity.php +++ b/app/Listeners/Quote/QuoteArchivedActivity.php @@ -19,6 +19,8 @@ use stdClass; class QuoteArchivedActivity implements ShouldQueue { + public $delay = 5; + protected $activity_repo; /** diff --git a/app/Listeners/Quote/QuoteCreatedNotification.php b/app/Listeners/Quote/QuoteCreatedNotification.php index 0336bf0365..3e9f688902 100644 --- a/app/Listeners/Quote/QuoteCreatedNotification.php +++ b/app/Listeners/Quote/QuoteCreatedNotification.php @@ -22,6 +22,8 @@ use Illuminate\Contracts\Queue\ShouldQueue; class QuoteCreatedNotification implements ShouldQueue { + public $delay = 5; + use UserNotifies; public function __construct() diff --git a/app/Listeners/Quote/QuoteDeletedActivity.php b/app/Listeners/Quote/QuoteDeletedActivity.php index a3b4d51f80..ea35eb093b 100644 --- a/app/Listeners/Quote/QuoteDeletedActivity.php +++ b/app/Listeners/Quote/QuoteDeletedActivity.php @@ -19,6 +19,8 @@ use stdClass; class QuoteDeletedActivity implements ShouldQueue { + public $delay = 5; + protected $activity_repo; /** diff --git a/app/Listeners/Quote/QuoteEmailActivity.php b/app/Listeners/Quote/QuoteEmailActivity.php index 4dcf747e59..cfdb3b523e 100644 --- a/app/Listeners/Quote/QuoteEmailActivity.php +++ b/app/Listeners/Quote/QuoteEmailActivity.php @@ -19,6 +19,8 @@ use stdClass; class QuoteEmailActivity implements ShouldQueue { + public $delay = 5; + protected $activity_repo; /** diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index 68d1264c11..bf7f348c7f 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -22,6 +22,9 @@ use Illuminate\Contracts\Queue\ShouldQueue; class QuoteEmailedNotification implements ShouldQueue { + + public $delay = 5; + use UserNotifies; public function __construct() diff --git a/app/Listeners/Quote/QuoteRestoredActivity.php b/app/Listeners/Quote/QuoteRestoredActivity.php index 11dc0152a8..b10d12cfb8 100644 --- a/app/Listeners/Quote/QuoteRestoredActivity.php +++ b/app/Listeners/Quote/QuoteRestoredActivity.php @@ -19,6 +19,8 @@ use stdClass; class QuoteRestoredActivity implements ShouldQueue { + public $delay = 5; + protected $activity_repo; /** diff --git a/app/Listeners/Quote/QuoteViewedActivity.php b/app/Listeners/Quote/QuoteViewedActivity.php index 8c7041f26a..5f5d7fd2ff 100644 --- a/app/Listeners/Quote/QuoteViewedActivity.php +++ b/app/Listeners/Quote/QuoteViewedActivity.php @@ -19,6 +19,8 @@ use stdClass; class QuoteViewedActivity implements ShouldQueue { + public $delay = 5; + protected $activity_repo; /** diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 3fb5e91428..4357077196 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -91,9 +91,16 @@ class UpdateInvoicePayment ->workFlow() ->save(); - event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); }); + /* Remove the event updater from within the loop to prevent race conditions */ + + $invoices->each(function ($invoice) { + + event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + + }); + $this->payment->saveQuietly(); return $this->payment; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 040f6fbff6..f284bcae94 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -330,17 +330,17 @@ trait MakesInvoiceValues // but that's no longer necessary. if (isset($item->tax_rate1)) { - $data[$key][$table_type.'.tax_rate1'] = round($item->tax_rate1, 2).'%'; + $data[$key][$table_type.'.tax_rate1'] = floatval($item->tax_rate1).'%'; $data[$key][$table_type.'.tax1'] = &$data[$key][$table_type.'.tax_rate1']; } if (isset($item->tax_rate2)) { - $data[$key][$table_type.'.tax_rate2'] = round($item->tax_rate2, 2).'%'; + $data[$key][$table_type.'.tax_rate2'] = floatval($item->tax_rate2).'%'; $data[$key][$table_type.'.tax2'] = &$data[$key][$table_type.'.tax_rate2']; } if (isset($item->tax_rate3)) { - $data[$key][$table_type.'.tax_rate3'] = round($item->tax_rate3, 2).'%'; + $data[$key][$table_type.'.tax_rate3'] = floatval($item->tax_rate3).'%'; $data[$key][$table_type.'.tax3'] = &$data[$key][$table_type.'.tax_rate3']; } diff --git a/tests/Unit/RedisVsDatabaseTest.php b/tests/Unit/RedisVsDatabaseTest.php index f7b1b76746..8a67f10fd6 100644 --- a/tests/Unit/RedisVsDatabaseTest.php +++ b/tests/Unit/RedisVsDatabaseTest.php @@ -38,12 +38,11 @@ class RedisVsDatabaseTest extends TestCase $currencies = Cache::get('currencies'); - $currency = $currencies->filter(function ($item) { + $currencies->filter(function ($item) { return $item->id == 17; })->first(); - - $total_time = microtime(true) - $start; + nlog(microtime(true) - $start); $this->assertTrue(true); // nlog($total_time); @@ -57,8 +56,7 @@ class RedisVsDatabaseTest extends TestCase $currency = Currency::find(17); - $total_time = microtime(true) - $start; - + nlog(microtime(true) - $start); $this->assertTrue(true); // nlog($total_time); From c77720672b2f1373f42e746039e82c699d728d31 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 17 Dec 2021 22:11:36 +1100 Subject: [PATCH 3/3] Fixes for atomic operations --- VERSION.txt | 2 +- app/Http/Controllers/ClientController.php | 4 +--- app/Http/Controllers/RecurringInvoiceController.php | 4 ++-- app/Http/Controllers/SubscriptionController.php | 2 -- .../Invoice/InvoiceFailedEmailNotification.php | 3 +-- app/Mail/DownloadBackup.php | 3 +++ app/Models/BaseModel.php | 4 ++-- app/PaymentDrivers/BaseDriver.php | 7 ++++--- app/Repositories/ClientRepository.php | 3 +-- app/Services/Credit/MarkSent.php | 3 +-- app/Services/Invoice/ApplyPaymentAmount.php | 6 +++--- app/Services/Invoice/AutoBillInvoice.php | 4 +--- app/Services/Invoice/MarkPaid.php | 11 ++++------- app/Services/Payment/UpdateInvoicePayment.php | 10 +++++----- app/Services/Quote/MarkSent.php | 4 ++-- app/Services/Quote/QuoteService.php | 4 ++-- config/ninja.php | 4 ++-- 17 files changed, 35 insertions(+), 43 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 0c37012d2c..7422b58bc0 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -5.3.37 \ No newline at end of file +5.3.38 \ No newline at end of file diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 631819765c..c46fbef9fe 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -382,9 +382,7 @@ class ClientController extends BaseController /* Set the client country to the company if none is set */ if(!$client->country_id && strlen($client->company->settings->country_id) > 1){ - $client->country_id = $client->company->settings->country_id; - - $client->save(); + $client->update(['country_id' => $client->company->settings->country_id]); } diff --git a/app/Http/Controllers/RecurringInvoiceController.php b/app/Http/Controllers/RecurringInvoiceController.php index 03bbe94cff..16e217c0af 100644 --- a/app/Http/Controllers/RecurringInvoiceController.php +++ b/app/Http/Controllers/RecurringInvoiceController.php @@ -204,8 +204,6 @@ class RecurringInvoiceController extends BaseController { $recurring_invoice = $this->recurring_invoice_repo->save($request->all(), RecurringInvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id)); - event(new RecurringInvoiceWasCreated($recurring_invoice, $recurring_invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $offset = $recurring_invoice->client->timezone_offset(); $recurring_invoice->next_send_date = Carbon::parse($recurring_invoice->next_send_date)->startOfDay()->addSeconds($offset); $recurring_invoice->saveQuietly(); @@ -214,6 +212,8 @@ class RecurringInvoiceController extends BaseController ->triggeredActions($request) ->save(); + event(new RecurringInvoiceWasCreated($recurring_invoice, $recurring_invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->itemResponse($recurring_invoice); } diff --git a/app/Http/Controllers/SubscriptionController.php b/app/Http/Controllers/SubscriptionController.php index 0cf6233a9c..051156343a 100644 --- a/app/Http/Controllers/SubscriptionController.php +++ b/app/Http/Controllers/SubscriptionController.php @@ -354,8 +354,6 @@ class SubscriptionController extends BaseController event(new SubscriptionWasUpdated($subscription, $subscription->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); -nlog($subscription->id); - return $this->itemResponse($subscription); } diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index 5603e9450a..fbaba6e97b 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -45,8 +45,7 @@ class InvoiceFailedEmailNotification $first_notification_sent = true; $invoice = $event->invitation->invoice; - $invoice->last_sent_date = now(); - $invoice->save(); + $invoice->update(['last_sent_date' => now()]); $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer( (new EntityFailedSendObject($event->invitation, 'invoice', $event->template, $event->message))->build() ); diff --git a/app/Mail/DownloadBackup.php b/app/Mail/DownloadBackup.php index b3f287c450..b70cb90b8d 100644 --- a/app/Mail/DownloadBackup.php +++ b/app/Mail/DownloadBackup.php @@ -16,6 +16,7 @@ use App\Models\Company; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\App; class DownloadBackup extends Mailable { @@ -36,6 +37,8 @@ class DownloadBackup extends Mailable */ public function build() { + App::setLocale($this->company->getLocale()); + $company = Company::where('company_key', $this->company->company_key)->first(); return $this->from(config('mail.from.address'), config('mail.from.name')) diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index bddc4ad0e5..2f82e3561b 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -113,10 +113,10 @@ class BaseModel extends Model * to persist the new settings we will also need to pass back a * reference to the parent class. * - * @param mixes $key The key of property + * @param $key The key of property * @return */ - public function getSettingsByKey(mixes $key) + public function getSettingsByKey($key) { /* Does Setting Exist @ client level */ if (isset($this->getSettings()->{$key})) { diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 0a52575631..1a5a1b837e 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -204,11 +204,13 @@ class BaseDriver extends AbstractPaymentDriver $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->withTrashed()->get(); $payment->invoices()->sync($invoices); + $payment->service()->applyNumber()->save(); + $invoices->each(function ($invoice) use ($payment) { event(new InvoiceWasPaid($invoice, $payment, $payment->company, Ninja::eventVars())); }); - return $payment->service()->applyNumber()->save(); + return $payment; } /** @@ -263,8 +265,7 @@ class BaseDriver extends AbstractPaymentDriver event('eloquent.created: App\Models\Payment', $payment); - if ($this->client->getSetting('client_online_payment_notification') && in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING - ])) + if ($this->client->getSetting('client_online_payment_notification') && in_array($status, [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING])) $payment->service()->sendEmail(); //todo diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index 4765256cde..cdf6305cb1 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -67,14 +67,13 @@ class ClientRepository extends BaseRepository if (!isset($client->number) || empty($client->number) || strlen($client->number) == 0) { $client->number = $this->getNextClientNumber($client); + $client->save(); } if (empty($data['name'])) { $data['name'] = $client->present()->name(); } - $client->save(); - $this->contact_repo->save($data, $client); return $client; diff --git a/app/Services/Credit/MarkSent.php b/app/Services/Credit/MarkSent.php index 0c062eb8a3..b02e85e50d 100644 --- a/app/Services/Credit/MarkSent.php +++ b/app/Services/Credit/MarkSent.php @@ -37,8 +37,6 @@ class MarkSent $this->credit->markInvitationsSent(); - event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $this->credit ->service() ->setStatus(Credit::STATUS_SENT) @@ -47,6 +45,7 @@ class MarkSent ->deletePdf() ->save(); + event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); return $this->credit; } diff --git a/app/Services/Invoice/ApplyPaymentAmount.php b/app/Services/Invoice/ApplyPaymentAmount.php index 1250bb506c..ee6a0f657e 100644 --- a/app/Services/Invoice/ApplyPaymentAmount.php +++ b/app/Services/Invoice/ApplyPaymentAmount.php @@ -88,8 +88,6 @@ class ApplyPaymentAmount extends AbstractService $payment->service()->sendEmail(); /* Update Invoice balance */ - event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); $payment->ledger() ->updatePaymentBalance($payment->amount * -1); @@ -104,7 +102,9 @@ class ApplyPaymentAmount extends AbstractService $this->invoice->service()->workFlow()->save(); event('eloquent.created: App\Models\Payment', $payment); - + event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->invoice; } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 646bbad5a1..2771c3409a 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -175,8 +175,6 @@ class AutoBillInvoice extends AbstractService } - event('eloquent.created: App\Models\Payment', $payment); - $payment->ledger() ->updatePaymentBalance($amount * -1) ->save(); @@ -192,7 +190,7 @@ class AutoBillInvoice extends AbstractService ->updateCreditBalance($amount * -1, "Credit {$current_credit->number} used to pay down Invoice {$this->invoice->number}") ->save(); - + event('eloquent.created: App\Models\Payment', $payment); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); return $this->invoice diff --git a/app/Services/Invoice/MarkPaid.php b/app/Services/Invoice/MarkPaid.php index a9b45ef1fa..3178eba978 100644 --- a/app/Services/Invoice/MarkPaid.php +++ b/app/Services/Invoice/MarkPaid.php @@ -91,13 +91,6 @@ class MarkPaid extends AbstractService ->deletePdf() ->save(); - // if ($this->invoice->client->getSetting('client_manual_payment_notification')) - // $payment->service()->sendEmail(); - - /* Update Invoice balance */ - event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $payment->ledger() ->updatePaymentBalance($payment->amount * -1); @@ -113,6 +106,10 @@ class MarkPaid extends AbstractService ->workFlow() ->save(); + /* Update Invoice balance */ + event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + event(new InvoiceWasPaid($this->invoice, $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->invoice; } diff --git a/app/Services/Payment/UpdateInvoicePayment.php b/app/Services/Payment/UpdateInvoicePayment.php index 4357077196..ca55342454 100644 --- a/app/Services/Payment/UpdateInvoicePayment.php +++ b/app/Services/Payment/UpdateInvoicePayment.php @@ -52,8 +52,8 @@ class UpdateInvoicePayment } /* Need to determine here is we have an OVER payment - if YES only apply the max invoice amount */ - if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance) - $paid_amount = $invoice->balance; + if($paid_amount > $invoice->partial && $paid_amount > $invoice->balance) + $paid_amount = $invoice->balance; /* Updates the company ledger */ $this->payment @@ -95,14 +95,14 @@ class UpdateInvoicePayment /* Remove the event updater from within the loop to prevent race conditions */ + $this->payment->saveQuietly(); + $invoices->each(function ($invoice) { event(new InvoiceWasUpdated($invoice, $invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - + }); - $this->payment->saveQuietly(); - return $this->payment; } } diff --git a/app/Services/Quote/MarkSent.php b/app/Services/Quote/MarkSent.php index a21a2d6b81..31cb5b9ac5 100644 --- a/app/Services/Quote/MarkSent.php +++ b/app/Services/Quote/MarkSent.php @@ -45,8 +45,6 @@ class MarkSent $this->quote->due_date = Carbon::parse($this->quote->date)->addDays($this->quote->client->getSetting('valid_until')); } - event(new QuoteWasMarkedSent($this->quote, $this->quote->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); - $this->quote ->service() ->setStatus(Quote::STATUS_SENT) @@ -54,6 +52,8 @@ class MarkSent ->deletePdf() ->save(); + event(new QuoteWasMarkedSent($this->quote, $this->quote->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); + return $this->quote; } } diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index b5b2088579..77b134103a 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -110,8 +110,6 @@ class QuoteService $contact = $this->quote->invitations->first()->contact; } - event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars())); - if ($this->quote->client->getSetting('auto_convert_quote')) { $this->convert(); @@ -123,6 +121,8 @@ class QuoteService } + event(new QuoteWasApproved($contact, $this->quote, $this->quote->company, Ninja::eventVars())); + return $this; } diff --git a/config/ninja.php b/config/ninja.php index 27066a5c48..4557bc9b84 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -14,8 +14,8 @@ return [ 'require_https' => env('REQUIRE_HTTPS', true), 'app_url' => rtrim(env('APP_URL', ''), '/'), 'app_domain' => env('APP_DOMAIN', 'invoicing.co'), - 'app_version' => '5.3.37', - 'app_tag' => '5.3.37', + 'app_version' => '5.3.38', + 'app_tag' => '5.3.38', 'minimum_client_version' => '5.0.16', 'terms_version' => '1.0.1', 'api_secret' => env('API_SECRET', ''),