mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 06:14:04 +00:00
merge
This commit is contained in:
commit
1e75221e12
26 changed files with 680 additions and 14 deletions
67
src/Commands/ClearPendingTenants.php
Normal file
67
src/Commands/ClearPendingTenants.php
Normal 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.");
|
||||
}
|
||||
}
|
||||
64
src/Commands/CreatePendingTenants.php
Normal file
64
src/Commands/CreatePendingTenants.php
Normal 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();
|
||||
}
|
||||
}
|
||||
|
|
@ -57,6 +57,6 @@ class Migrate extends MigrateCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseMigrated($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ final class MigrateFresh extends Command
|
|||
'--tenants' => [$tenant->getTenantKey()],
|
||||
'--force' => true,
|
||||
]);
|
||||
});
|
||||
}, $this->withPending());
|
||||
|
||||
$this->info('Done.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,6 @@ class Rollback extends RollbackCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseRolledBack($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Stancl\Tenancy\Concerns\HasATenantsOption;
|
||||
|
||||
class Run extends Command
|
||||
{
|
||||
use HasATenantsOption;
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
|
|
@ -21,7 +23,6 @@ class Run extends Command
|
|||
* @var string
|
||||
*/
|
||||
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}
|
||||
{--option=* : The options to pass to the command. Default: none}";
|
||||
|
||||
|
|
@ -52,6 +53,6 @@ class Run extends Command
|
|||
|
||||
// Run command
|
||||
$this->call($this->argument('commandname'), array_merge($arguments, $options));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,6 +59,6 @@ class Seed extends SeedCommand
|
|||
parent::handle();
|
||||
|
||||
event(new DatabaseSeeded($tenant));
|
||||
});
|
||||
}, $this->withPending());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ trait HasATenantsOption
|
|||
{
|
||||
return array_merge([
|
||||
['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null],
|
||||
['with-pending', null, InputOption::VALUE_NONE, 'include pending tenants in query', null],
|
||||
], parent::getOptions());
|
||||
}
|
||||
|
||||
|
|
@ -26,6 +27,11 @@ trait HasATenantsOption
|
|||
->cursor();
|
||||
}
|
||||
|
||||
protected function withPending(): ?bool
|
||||
{
|
||||
return $this->option('with-pending') ? true : null;
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
|
|
|||
90
src/Database/Concerns/HasPending.php
Normal file
90
src/Database/Concerns/HasPending.php
Normal 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;
|
||||
}
|
||||
}
|
||||
101
src/Database/Concerns/PendingScope.php
Normal file
101
src/Database/Concerns/PendingScope.php
Normal 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -26,7 +26,8 @@ class Tenant extends Model implements Contracts\Tenant
|
|||
Concerns\HasDataColumn,
|
||||
Concerns\HasInternalKeys,
|
||||
Concerns\TenantRun,
|
||||
Concerns\InvalidatesResolverCache;
|
||||
Concerns\InvalidatesResolverCache,
|
||||
Concerns\HasPending;
|
||||
|
||||
protected $table = 'tenants';
|
||||
protected $primaryKey = 'id';
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ use Stancl\Tenancy\Contracts\Tenant;
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
9
src/Events/CreatingPendingTenant.php
Normal file
9
src/Events/CreatingPendingTenant.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class CreatingPendingTenant extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/PendingTenantCreated.php
Normal file
9
src/Events/PendingTenantCreated.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class PendingTenantCreated extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/PendingTenantPulled.php
Normal file
9
src/Events/PendingTenantPulled.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class PendingTenantPulled extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
9
src/Events/PullingPendingTenant.php
Normal file
9
src/Events/PullingPendingTenant.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class PullingPendingTenant extends Contracts\TenantEvent
|
||||
{
|
||||
}
|
||||
28
src/Jobs/ClearPendingTenants.php
Normal file
28
src/Jobs/ClearPendingTenants.php
Normal 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);
|
||||
}
|
||||
}
|
||||
28
src/Jobs/CreatePendingTenants.php
Normal file
28
src/Jobs/CreatePendingTenants.php
Normal 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
use Illuminate\Support\Traits\Macroable;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Database\Concerns\PendingScope;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById;
|
||||
|
||||
class Tenancy
|
||||
|
|
@ -135,10 +136,15 @@ class Tenancy
|
|||
* @param callable $callback
|
||||
* @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
|
||||
$tenants = is_null($tenants) ? $this->model()->cursor() : $tenants;
|
||||
$tenants = is_null($tenants) ? $query->cursor() : $tenants;
|
||||
|
||||
// Convert incrementing int ids to strings
|
||||
$tenants = is_int($tenants) ? (string) $tenants : $tenants;
|
||||
|
|
@ -146,8 +152,8 @@ class Tenancy
|
|||
// Wrap string in array
|
||||
$tenants = is_string($tenants) ? [$tenants] : $tenants;
|
||||
|
||||
// Use all tenants if $tenants is falsey
|
||||
$tenants = $tenants ?: $this->model()->cursor();
|
||||
// Use all tenants if $tenants is false
|
||||
$tenants = $tenants ?: $query->cursor();
|
||||
|
||||
$originalTenant = $this->tenant;
|
||||
|
||||
|
|
|
|||
|
|
@ -89,6 +89,8 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
Commands\Rollback::class,
|
||||
Commands\TenantList::class,
|
||||
Commands\MigrateFresh::class,
|
||||
Commands\CreatePendingTenants::class,
|
||||
Commands\ClearPendingTenants::class,
|
||||
]);
|
||||
|
||||
$this->publishes([
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue