mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 18:34:04 +00:00
Update comments and naming
This commit is contained in:
parent
b5b0799750
commit
a9146ae00d
4 changed files with 25 additions and 25 deletions
|
|
@ -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 => [],
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue