mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 08:24:04 +00:00
- [BC BREAK] Make pullPendingFromPool() $firstOrCreate arg default to false (pullPending() is now a direct alias for pullPendingFromPool() with default $firstOrCreate=true) - Resolve race conditions in pullPendingFromPool() - Make createPending() set pending_since regardless of exceptions - Make pullPending() accept $attributes - Fire PullingPendingTenant from within a DB transaction - Clarify --count arg description for CreatePendingTenants command - Add docblock to PullingPendingTenant with a notice
46 lines
1.5 KiB
PHP
46 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
class CreatePendingTenants extends Command
|
|
{
|
|
protected $signature = 'tenants:pending-create {--count= : The number of pending tenants to maintain}';
|
|
|
|
protected $description = 'Create pending tenants.';
|
|
|
|
public function handle(): int
|
|
{
|
|
$this->components->info('Creating pending tenants.');
|
|
|
|
$maxPendingTenantCount = (int) ($this->option('count') ?? config('tenancy.pending.count'));
|
|
$pendingTenantCount = $this->getPendingTenantCount();
|
|
$createdCount = 0;
|
|
|
|
while ($pendingTenantCount < $maxPendingTenantCount) {
|
|
tenancy()->model()::createPending();
|
|
|
|
// Fetching the pending tenant count in each iteration prevents creating too many tenants
|
|
// If pending tenants are being created somewhere else while running this command
|
|
$pendingTenantCount = $this->getPendingTenantCount();
|
|
|
|
$createdCount++;
|
|
}
|
|
|
|
$this->components->info($createdCount . ' pending ' . str('tenant')->plural($createdCount) . ' created.');
|
|
$this->components->info($maxPendingTenantCount . ' pending ' . str('tenant')->plural($maxPendingTenantCount) . ' ready to be used.');
|
|
|
|
return 0;
|
|
}
|
|
|
|
/** Calculate the number of currently available pending tenants. */
|
|
protected function getPendingTenantCount(): int
|
|
{
|
|
return tenancy()->query()
|
|
->onlyPending()
|
|
->count();
|
|
}
|
|
}
|