From a9146ae00d99d964c727b754849d7f727b191469 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 25 Jul 2022 14:46:56 +0200 Subject: [PATCH] Update comments and naming --- assets/TenancyServiceProvider.stub.php | 2 +- src/Commands/ClearPendingTenants.php | 22 +++++++++++----------- src/Commands/CreatePendingTenants.php | 20 ++++++++++---------- src/Database/Concerns/HasPending.php | 6 +++--- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 3e887789..b4e24ba5 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -55,7 +55,7 @@ class TenancyServiceProvider extends ServiceProvider })->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production. ], - // Pending events + // Pending tenant events Events\CreatingPendingTenant::class => [], Events\PendingTenantCreated::class => [], Events\PullingPendingTenant::class => [], diff --git a/src/Commands/ClearPendingTenants.php b/src/Commands/ClearPendingTenants.php index 089f9db5..6d28b77c 100644 --- a/src/Commands/ClearPendingTenants.php +++ b/src/Commands/ClearPendingTenants.php @@ -16,15 +16,15 @@ class ClearPendingTenants extends Command */ protected $signature = 'tenants:pending-clear {--all : Override the default settings and deletes all pending tenants} - {--older-days= : Deletes all pending older than the amount of days} - {--older-hours= : Deletes all pending older than the amount of hours}'; + {--older-days= : Deletes all pending tenants older than the amount of days} + {--older-hours= : Deletes all pending tenants older than the amount of hours}'; /** * The console command description. * * @var string */ - protected $description = 'Removes any pending tenants'; + protected $description = 'Remove pending tenants.'; /** * Execute the console command. @@ -33,29 +33,29 @@ class ClearPendingTenants extends Command { $this->info('Cleaning pending tenants.'); - $expireDate = now(); - // At the end, we will check if the value has been changed by comparing the two dates - $savedExpiredDate = $expireDate->copy()->toImmutable(); + $expirationDate = now(); + // We compare the original and the new expiration date to check if the new one is different later + $originalExpirationDate = $expirationDate->copy()->toImmutable(); // If the all option is given, skip the expiry date configuration if (! $this->option('all')) { if ($olderThanDays = $this->option('older-days') ?? config('tenancy.pending.older_than_days')) { - $expireDate->subDays($olderThanDays); + $expirationDate->subDays($olderThanDays); } if ($olderThanHours = $this->option('older-hours') ?? config('tenancy.pending.older_than_hours')) { - $expireDate->subHours($olderThanHours); + $expirationDate->subHours($olderThanHours); } } $deletedPendingCount = tenancy() ->query() ->onlyPending() - ->when($savedExpiredDate->notEqualTo($expireDate), function (Builder $query) use ($expireDate) { - $query->where('data->pending_since', '<', $expireDate->timestamp); + ->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) { + $query->where('data->pending_since', '<', $expirationDate->timestamp); }) ->get() - ->each // Make sure the model events are triggered by deleting the tenants one by one + ->each // Trigger the model events by deleting the tenants one by one ->delete() ->count(); diff --git a/src/Commands/CreatePendingTenants.php b/src/Commands/CreatePendingTenants.php index 06f74062..18b53806 100644 --- a/src/Commands/CreatePendingTenants.php +++ b/src/Commands/CreatePendingTenants.php @@ -20,37 +20,37 @@ class CreatePendingTenants extends Command * * @var string */ - protected $description = 'Deploy tenants until the pending count is achieved.'; + protected $description = 'Create tenants until the pending count is achieved.'; /** * Execute the console command. */ public function handle() { - $this->info('Deploying pending tenants.'); + $this->info('Creating pending tenants.'); $maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count')); - $pendingTenantCount = $this->getPendingTenantCount(); + $createdCount = 0; - $deployedCount = 0; while ($pendingTenantCount < $maxPendingTenantCount) { tenancy()->model()::createPending(); - // Update the number of pending tenants every time with a query to get a live count - // To prevent deploying too many tenants if pending tenants are being created simultaneously somewhere else - // While running this command + + // Fetch the count each time to prevent creating too many tenants if pending tenants + // Are being created somewhere else while running this command $pendingTenantCount = $this->getPendingTenantCount(); - $deployedCount++; + + $createdCount++; } - $this->info($deployedCount . ' ' . str('tenant')->plural($deployedCount) . ' deployed.'); + $this->info($createdCount . ' ' . str('tenant')->plural($createdCount) . ' created.'); $this->info($maxPendingTenantCount . ' ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.'); return 1; } /** - * Calculate the number of currently deployed pending tenants. + * Calculate the number of currently available pending tenants. */ private function getPendingTenantCount(): int { diff --git a/src/Database/Concerns/HasPending.php b/src/Database/Concerns/HasPending.php index 5231695a..28e9b944 100644 --- a/src/Database/Concerns/HasPending.php +++ b/src/Database/Concerns/HasPending.php @@ -55,8 +55,8 @@ trait HasPending event(new CreatingPendingTenant($tenant)); - // Add the pending value only after creating the model - // To ensure it's not marked as pending until finishing running the migrations, seeders, etc. + // Update the pending_since value only after the model is created to ensure that + // It's not marked as pending until finishing running the migrations, seeders, etc. $tenant->update([ 'pending_since' => now()->timestamp, ]); @@ -73,7 +73,7 @@ trait HasPending static::createPending(); } - // At this point, we can guarantee a pending tenant is free and can be called + // At this point, we can guarantee a pending tenant is available $tenant = static::onlyPending()->first(); event(new PullingPendingTenant($tenant));