1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:54:05 +00:00

Update comments and naming

This commit is contained in:
lukinovec 2022-07-25 14:46:56 +02:00
parent b5b0799750
commit a9146ae00d
4 changed files with 25 additions and 25 deletions

View file

@ -55,7 +55,7 @@ class TenancyServiceProvider extends ServiceProvider
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production. })->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
], ],
// Pending events // Pending tenant events
Events\CreatingPendingTenant::class => [], Events\CreatingPendingTenant::class => [],
Events\PendingTenantCreated::class => [], Events\PendingTenantCreated::class => [],
Events\PullingPendingTenant::class => [], Events\PullingPendingTenant::class => [],

View file

@ -16,15 +16,15 @@ class ClearPendingTenants extends Command
*/ */
protected $signature = 'tenants:pending-clear protected $signature = 'tenants:pending-clear
{--all : Override the default settings and deletes all pending tenants} {--all : Override the default settings and deletes all pending tenants}
{--older-days= : Deletes all pending older than the amount of days} {--older-days= : Deletes all pending tenants older than the amount of days}
{--older-hours= : Deletes all pending older than the amount of hours}'; {--older-hours= : Deletes all pending tenants older than the amount of hours}';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Removes any pending tenants'; protected $description = 'Remove pending tenants.';
/** /**
* Execute the console command. * Execute the console command.
@ -33,29 +33,29 @@ class ClearPendingTenants extends Command
{ {
$this->info('Cleaning pending tenants.'); $this->info('Cleaning pending tenants.');
$expireDate = now(); $expirationDate = now();
// At the end, we will check if the value has been changed by comparing the two dates // We compare the original and the new expiration date to check if the new one is different later
$savedExpiredDate = $expireDate->copy()->toImmutable(); $originalExpirationDate = $expirationDate->copy()->toImmutable();
// If the all option is given, skip the expiry date configuration // If the all option is given, skip the expiry date configuration
if (! $this->option('all')) { if (! $this->option('all')) {
if ($olderThanDays = $this->option('older-days') ?? config('tenancy.pending.older_than_days')) { 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')) { if ($olderThanHours = $this->option('older-hours') ?? config('tenancy.pending.older_than_hours')) {
$expireDate->subHours($olderThanHours); $expirationDate->subHours($olderThanHours);
} }
} }
$deletedPendingCount = tenancy() $deletedPendingCount = tenancy()
->query() ->query()
->onlyPending() ->onlyPending()
->when($savedExpiredDate->notEqualTo($expireDate), function (Builder $query) use ($expireDate) { ->when($originalExpirationDate->notEqualTo($expirationDate), function (Builder $query) use ($expirationDate) {
$query->where('data->pending_since', '<', $expireDate->timestamp); $query->where('data->pending_since', '<', $expirationDate->timestamp);
}) })
->get() ->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() ->delete()
->count(); ->count();

View file

@ -20,37 +20,37 @@ class CreatePendingTenants extends Command
* *
* @var string * @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. * Execute the console command.
*/ */
public function handle() public function handle()
{ {
$this->info('Deploying pending tenants.'); $this->info('Creating pending tenants.');
$maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count')); $maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count'));
$pendingTenantCount = $this->getPendingTenantCount(); $pendingTenantCount = $this->getPendingTenantCount();
$createdCount = 0;
$deployedCount = 0;
while ($pendingTenantCount < $maxPendingTenantCount) { while ($pendingTenantCount < $maxPendingTenantCount) {
tenancy()->model()::createPending(); 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 // Fetch the count each time to prevent creating too many tenants if pending tenants
// While running this command // Are being created somewhere else while running this command
$pendingTenantCount = $this->getPendingTenantCount(); $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.'); $this->info($maxPendingTenantCount . ' ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
return 1; return 1;
} }
/** /**
* Calculate the number of currently deployed pending tenants. * Calculate the number of currently available pending tenants.
*/ */
private function getPendingTenantCount(): int private function getPendingTenantCount(): int
{ {

View file

@ -55,8 +55,8 @@ trait HasPending
event(new CreatingPendingTenant($tenant)); event(new CreatingPendingTenant($tenant));
// Add the pending value only after creating the model // Update the pending_since value only after the model is created to ensure that
// To ensure it's not marked as pending until finishing running the migrations, seeders, etc. // It's not marked as pending until finishing running the migrations, seeders, etc.
$tenant->update([ $tenant->update([
'pending_since' => now()->timestamp, 'pending_since' => now()->timestamp,
]); ]);
@ -73,7 +73,7 @@ trait HasPending
static::createPending(); 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(); $tenant = static::onlyPending()->first();
event(new PullingPendingTenant($tenant)); event(new PullingPendingTenant($tenant));