1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:34:04 +00:00
This commit is contained in:
Samuel Štancl 2022-06-01 15:12:52 +02:00
commit 1e75221e12
26 changed files with 680 additions and 14 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
.env .env
.DS_Store
composer.lock composer.lock
vendor/ vendor/
.vscode/ .vscode/

View file

@ -49,6 +49,12 @@ 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
Events\CreatingPendingTenant::class => [],
Events\PendingTenantCreated::class => [],
Events\PullingPendingTenant::class => [],
Events\PendingTenantPulled::class => [],
// Domain events // Domain events
Events\CreatingDomain::class => [], Events\CreatingDomain::class => [],
Events\DomainCreated::class => [], Events\DomainCreated::class => [],

View file

@ -35,6 +35,37 @@ return [
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
], ],
/**
* Pending tenancy config.
* This is useful if you're looking for a way to always have a tenant ready to be used.
*/
'pending' => [
/**
* If disabled, pending tenants will be excluded from all tenant queries. Unless if
* told otherwise with ::withPending() or ::onlyPending().
* Note: when disabled, this will also ignore tenants when runnings any tenants commands (migration, seed, etc.)
*/
'include_in_queries' => true,
/**
* Defines how many tenants you want to be in a pending state.
* This value should be changed depending on how often a new tenant is created
* and how often you run the `tenancy:pending` command via the scheduler.
*/
'count' => env('TENANCY_PENDING_COUNT', 5),
/**
* If needed, you can define a time limite after when an unused pending tenant
* will automatically be deleted.
* For this to work automatically, make sure to call the `tenancy:pending-clear` command in the scheduler.
*
* If both values are set to null, not time limit will be set and all pending tenants will be deleted.
*/
'older_than_days' => env('TENANCY_PENDING_OLDER_THAN_DAYS', null),
'older_than_hours' => env('TENANCY_PENDING_OLDER_THAN_HOURS', null),
],
/** /**
* Database tenancy config. Used by DatabaseTenancyBootstrapper. * Database tenancy config. Used by DatabaseTenancyBootstrapper.
*/ */

View file

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
class ClearPendingTenants extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
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}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Removes any pending tenants';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$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();
// 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);
}
if ($olderThanHours = $this->option('older-hours') ?? config('tenancy.pending.older_than_hours')) {
$expireDate->subHours($olderThanHours);
}
}
$deletedPendingCount = tenancy()
->query()
->onlyPending()
->when($savedExpiredDate->notEqualTo($expireDate), function (Builder $query) use ($expireDate) {
$query->where('data->pending_since', '<', $expireDate->timestamp);
})
->get()
->each // This makes sure the events or triggered on the model
->delete()
->count();
$this->info("$deletedPendingCount pending tenant(s) deleted.");
}
}

View file

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
class CreatePendingTenants extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tenants:pending {--count= : The number of tenant to be in a pending state}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Deploy tenants until the pending count is achieved.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Deploying pending tenants.');
$pendingObjectifCount = (int) ($this->option('count') ?? config('tenancy.pending.count'));
$pendingCurrentCount = $this->getPendingTenantCount();
$deployedCount = 0;
while ($pendingCurrentCount < $pendingObjectifCount) {
tenancy()->model()::createPending();
// We update the number of pending tenants every time with a query to get a live count.
// this prevents to deploy too many tenants if pending tenants are being created simultaneous somewhere else
// during the runtime of this command.
$pendingCurrentCount = $this->getPendingTenantCount();
$deployedCount++;
}
$this->info("$deployedCount tenants deployed, $pendingObjectifCount tenant(s) are ready to be used.");
return 1;
}
/**
* Calculates the number of pending tenants currently deployed
* @return int
*/
private function getPendingTenantCount(): int
{
return tenancy()
->query()
->onlyPending()
->count();
}
}

View file

@ -57,6 +57,6 @@ class Migrate extends MigrateCommand
parent::handle(); parent::handle();
event(new DatabaseMigrated($tenant)); event(new DatabaseMigrated($tenant));
}); }, $this->withPending());
} }
} }

View file

@ -49,7 +49,7 @@ final class MigrateFresh extends Command
'--tenants' => [$tenant->getTenantKey()], '--tenants' => [$tenant->getTenantKey()],
'--force' => true, '--force' => true,
]); ]);
}); }, $this->withPending());
$this->info('Done.'); $this->info('Done.');
} }

View file

@ -66,6 +66,6 @@ class Rollback extends RollbackCommand
parent::handle(); parent::handle();
event(new DatabaseRolledBack($tenant)); event(new DatabaseRolledBack($tenant));
}); }, $this->withPending());
} }
} }

View file

@ -5,9 +5,11 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands; namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Stancl\Tenancy\Concerns\HasATenantsOption;
class Run extends Command class Run extends Command
{ {
use HasATenantsOption;
/** /**
* The console command description. * The console command description.
* *
@ -21,7 +23,6 @@ class Run extends Command
* @var string * @var string
*/ */
protected $signature = "tenants:run {commandname : The command's name.} protected $signature = "tenants:run {commandname : The command's name.}
{--tenants=* : The tenant(s) to run the command for. Default: all}
{--argument=* : The arguments to pass to the command. Default: none} {--argument=* : The arguments to pass to the command. Default: none}
{--option=* : The options to pass to the command. Default: none}"; {--option=* : The options to pass to the command. Default: none}";
@ -52,6 +53,6 @@ class Run extends Command
// Run command // Run command
$this->call($this->argument('commandname'), array_merge($arguments, $options)); $this->call($this->argument('commandname'), array_merge($arguments, $options));
}); }, $this->withPending());
} }
} }

View file

@ -59,6 +59,6 @@ class Seed extends SeedCommand
parent::handle(); parent::handle();
event(new DatabaseSeeded($tenant)); event(new DatabaseSeeded($tenant));
}); }, $this->withPending());
} }
} }

View file

@ -13,6 +13,7 @@ trait HasATenantsOption
{ {
return array_merge([ return array_merge([
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null], ['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query', null],
], parent::getOptions()); ], parent::getOptions());
} }
@ -26,6 +27,11 @@ trait HasATenantsOption
->cursor(); ->cursor();
} }
protected function withPending(): ?bool
{
return $this->option('with-pending') ? true : null;
}
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();

View file

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\CreatingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantCreated;
/**
* @property $pending_since
*
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withPending(bool $withPending = true)
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyPending()
* @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutPending()
*/
trait HasPending
{
/**
* Boot the has pending trait for a model.
*
* @return void
*/
public static function bootHasPending()
{
static::addGlobalScope(new PendingScope());
}
/**
* Initialize the has pending trait for an instance.
*
* @return void
*/
public function initializeHasPending()
{
$this->casts['pending_since'] = 'timestamp';
}
/**
* Determine if the model instance is in a pending state.
*
* @return bool
*/
public function pending()
{
return !is_null($this->pending_since);
}
public static function createPending($attributes = []): void
{
$tenant = static::create($attributes);
event(new CreatingPendingTenant($tenant));
// We add the pending value only after the model has then been created.
// this ensures the model is not marked as pending until the migrations, seeders, etc. are done
$tenant->update([
'pending_since' => now()->timestamp
]);
event(new PendingTenantCreated($tenant));
}
public static function pullPendingTenant(bool $firstOrCreate = false): ?Tenant
{
if (!static::onlyPending()->exists()) {
if (!$firstOrCreate) {
return null;
}
static::createPending();
}
// At this point we can guarantee a pending tenant is free and can be called.
$tenant = static::onlyPending()->first();
event(new PullingPendingTenant($tenant));
$tenant->update([
'pending_since' => null
]);
event(new PendingTenantPulled($tenant));
return $tenant;
}
}

View file

@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class PendingScope implements Scope
{
/**
* All of the extensions to be added to the builder.
*
* @var string[]
*/
protected $extensions = ['WithPending', 'WithoutPending', 'OnlyPending'];
/**
* Apply the scope to a given Eloquent query builder.
*
* @param Builder $builder
* @param Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->when(!config('tenancy.pending.include_in_queries'), function (Builder $builder){
$builder->whereNull('data->pending_since');
});
}
/**
* Extend the query builder with the needed functions.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
public function extend(Builder $builder)
{
foreach ($this->extensions as $extension) {
$this->{"add{$extension}"}($builder);
}
}
/**
* Add the with-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithPending(Builder $builder)
{
$builder->macro('withPending', function (Builder $builder, $withPending = true) {
if (! $withPending) {
return $builder->withoutPending();
}
return $builder->withoutGlobalScope($this);
});
}
/**
* Add the without-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addWithoutPending(Builder $builder)
{
$builder->macro('withoutPending', function (Builder $builder) {
// Only use whereNull('data->pending_since') when Laravel 6 support is dropped
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
$builder->withoutGlobalScope($this)
->where('data->pending_since', 'like', 'null')
->orWhereNull('data');
return $builder;
});
}
/**
* Add the only-pending extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addOnlyPending(Builder $builder)
{
$builder->macro('onlyPending', function (Builder $builder) {
// Use whereNotNull when Laravel 6 is dropped
// Issue fixed in Laravel 7 https://github.com/laravel/framework/pull/32417
$builder->withoutGlobalScope($this)->where('data->pending_since', 'not like', 'null');
return $builder;
});
}
}

View file

@ -26,7 +26,8 @@ class Tenant extends Model implements Contracts\Tenant
Concerns\HasDataColumn, Concerns\HasDataColumn,
Concerns\HasInternalKeys, Concerns\HasInternalKeys,
Concerns\TenantRun, Concerns\TenantRun,
Concerns\InvalidatesResolverCache; Concerns\InvalidatesResolverCache,
Concerns\HasPending;
protected $table = 'tenants'; protected $table = 'tenants';
protected $primaryKey = 'id'; protected $primaryKey = 'id';

View file

@ -16,9 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant;
*/ */
class TenantCollection extends Collection class TenantCollection extends Collection
{ {
public function runForEach(callable $callable): self public function runForEach(callable $callable, bool $withPending = null): self
{ {
tenancy()->runForMultiple($this->items, $callable); tenancy()->runForMultiple($this->items, $callable, $withPending);
return $this; return $this;
} }

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class CreatingPendingTenant extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class PendingTenantCreated extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class PendingTenantPulled extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Events;
class PullingPendingTenant extends Contracts\TenantEvent
{
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
class ClearPendingTenants implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Artisan::call(\Stancl\Tenancy\Commands\ClearPendingTenants::class);
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Artisan;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
class CreatePendingTenants implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Artisan::call(\Stancl\Tenancy\Commands\CreatePendingTenants::class);
}
}

View file

@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Traits\Macroable;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Database\Concerns\PendingScope;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById;
class Tenancy class Tenancy
@ -135,10 +136,15 @@ class Tenancy
* @param callable $callback * @param callable $callback
* @return void * @return void
*/ */
public function runForMultiple($tenants, callable $callback) public function runForMultiple($tenants, callable $callback, bool $withPending = null)
{ {
$query = $this->model()->newQuery();
if (is_bool($withPending) && $this->model()::hasGlobalScope(PendingScope::class)){
$query->withPending($withPending);
}
// Convert null to all tenants // Convert null to all tenants
$tenants = is_null($tenants) ? $this->model()->cursor() : $tenants; $tenants = is_null($tenants) ? $query->cursor() : $tenants;
// Convert incrementing int ids to strings // Convert incrementing int ids to strings
$tenants = is_int($tenants) ? (string) $tenants : $tenants; $tenants = is_int($tenants) ? (string) $tenants : $tenants;
@ -146,8 +152,8 @@ class Tenancy
// Wrap string in array // Wrap string in array
$tenants = is_string($tenants) ? [$tenants] : $tenants; $tenants = is_string($tenants) ? [$tenants] : $tenants;
// Use all tenants if $tenants is falsey // Use all tenants if $tenants is false
$tenants = $tenants ?: $this->model()->cursor(); $tenants = $tenants ?: $query->cursor();
$originalTenant = $this->tenant; $originalTenant = $this->tenant;

View file

@ -89,6 +89,8 @@ class TenancyServiceProvider extends ServiceProvider
Commands\Rollback::class, Commands\Rollback::class,
Commands\TenantList::class, Commands\TenantList::class,
Commands\MigrateFresh::class, Commands\MigrateFresh::class,
Commands\CreatePendingTenants::class,
Commands\ClearPendingTenants::class,
]); ]);
$this->publishes([ $this->publishes([

View file

@ -7,9 +7,10 @@ namespace Stancl\Tenancy\Tests\Etc;
use Stancl\Tenancy\Contracts\TenantWithDatabase; use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase; use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains; use Stancl\Tenancy\Database\Concerns\HasDomains;
use Stancl\Tenancy\Database\Concerns\HasPending;
use Stancl\Tenancy\Database\Models; use Stancl\Tenancy\Database\Models;
class Tenant extends Models\Tenant implements TenantWithDatabase class Tenant extends Models\Tenant implements TenantWithDatabase
{ {
use HasDatabase, HasDomains; use HasDatabase, HasDomains, HasPending;
} }

View file

@ -0,0 +1 @@
{"tenant_id":"The current tenant id is: acme"}

View file

@ -0,0 +1,196 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Commands\ClearPendingTenants;
use Stancl\Tenancy\Commands\CreatePendingTenants;
use Stancl\Tenancy\Events\PullingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantPulled;
use Stancl\Tenancy\Events\CreatingPendingTenant;
use Stancl\Tenancy\Events\PendingTenantCreated;
use Stancl\Tenancy\Tests\Etc\Tenant;
class PendingTenantsTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}
/** @test */
public function a_tenant_is_correctly_identified_as_pending()
{
Tenant::createPending();
$this->assertCount(1, Tenant::onlyPending()->get());
Tenant::onlyPending()->first()->update([
'pending_since' => null
]);
$this->assertCount(0, Tenant::onlyPending()->get());
}
/** @test */
public function pending_trait_imports_query_scopes()
{
Tenant::createPending();
Tenant::create();
Tenant::create();
$this->assertCount(1, Tenant::onlyPending()->get());
$this->assertCount(3, Tenant::withPending(true)->get());
$this->assertCount(2, Tenant::withPending(false)->get());
$this->assertCount(2, Tenant::withoutPending()->get());
}
/** @test */
public function pending_tenants_are_created_and_deleted_from_the_commands()
{
config(['tenancy.pending.count' => 4]);
Artisan::call(CreatePendingTenants::class);
$this->assertCount(4, Tenant::onlyPending()->get());
Artisan::call(ClearPendingTenants::class);
$this->assertCount(0, Tenant::onlyPending()->get());
}
/** @test */
public function clear_pending_tenants_command_only_delete_pending_tenants_older_than()
{
config(['tenancy.pending.count' => 2]);
Artisan::call(CreatePendingTenants::class);
config(['tenancy.pending.older_than_days' => 2]);
tenancy()->model()->query()->onlyPending()->first()->update([
'pending_since' => now()->subDays(5)->timestamp
]);
Artisan::call(ClearPendingTenants::class);
$this->assertCount(1, Tenant::onlyPending()->get());
}
/** @test */
public function clear_pending_tenants_command_all_option_overrides_config()
{
Tenant::createPending();
Tenant::createPending();
tenancy()->model()->query()->onlyPending()->first()->update([
'pending_since' => now()->subDays(10)
]);
config(['tenancy.pending.older_than_days' => 4]);
Artisan::call(ClearPendingTenants::class, [
'--all' => true
]);
$this->assertCount(0, Tenant::onlyPending()->get());
}
/** @test */
public function tenancy_can_check_for_rpending_tenants()
{
Tenant::query()->delete();
$this->assertFalse(Tenant::onlyPending()->exists());
Tenant::createPending();
$this->assertTrue(Tenant::onlyPending()->exists());
}
/** @test */
public function tenancy_can_pull_a_pending_tenant()
{
$this->assertNull(Tenant::pullPendingTenant());
Tenant::createPending();
$this->assertInstanceOf(Tenant::class, Tenant::pullPendingTenant(true));
}
/** @test */
public function tenancy_can_create_if_none_are_pending()
{
$this->assertCount(0, Tenant::all());
Tenant::pullPendingTenant(true);
$this->assertCount(1, Tenant::all());
}
/** @test */
public function pending_tenants_global_scope_config_can_include_or_exclude()
{
Tenant::createPending();
config(['tenancy.pending.include_in_queries' => false]);
$this->assertCount(0, Tenant::all());
config(['tenancy.pending.include_in_queries' => true]);
$this->assertCount(1, Tenant::all());
Tenant::all();
}
/** @test */
public function pending_events_are_triggerred()
{
Event::fake([
CreatingPendingTenant::class,
PendingTenantCreated::class,
PullingPendingTenant::class,
PendingTenantPulled::class,
]);
Tenant::createPending();
Event::assertDispatched(CreatingPendingTenant::class);
Event::assertDispatched(PendingTenantCreated::class);
Tenant::pullPendingTenant();
Event::assertDispatched(PullingPendingTenant::class);
Event::assertDispatched(PendingTenantPulled::class);
}
/** @test */
public function tenancy_run_for_multiple_can_scope_pending_tenants()
{
config(['tenancy.pending.include_in_queries' => false]);
Tenant::createPending();
Tenant::create();
$executedCount = 0;
tenancy()->runForMultiple([], function () use (&$executedCount){
$executedCount++;
}, false);
self::assertEquals(1, $executedCount);
$executedCount = 0;
tenancy()->runForMultiple([], function () use (&$executedCount){
$executedCount++;
}, true);
self::assertEquals(2, $executedCount);
}
}