1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-15 06:44:03 +00:00

parallel commands: core # autodetect, bugfixes, improved output

This commit is contained in:
Samuel Štancl 2024-09-27 23:02:03 +02:00
parent b4a055315b
commit 39bcbda5d0
6 changed files with 138 additions and 19 deletions

View file

@ -16,6 +16,7 @@ use Stancl\Tenancy\Concerns\ParallelCommand;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Events\MigratingDatabase;
use Symfony\Component\Console\Output\OutputInterface as OI;
class Migrate extends MigrateCommand
{
@ -52,7 +53,7 @@ class Migrate extends MigrateCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants($chunk->all());
return $this->getTenants($chunk);
}));
}
@ -80,9 +81,25 @@ class Migrate extends MigrateCommand
$tenant->run(function ($tenant) use (&$success) {
event(new MigratingDatabase($tenant));
// Migrate
if (parent::handle() !== 0) {
$success = false;
$verbosity = (int) $this->output->getVerbosity();
if ($this->runningConcurrently) {
// The output gets messy when multiple processes are writing to the same stdout
$this->output->setVerbosity(OI::VERBOSITY_QUIET);
}
try {
// Migrate
if (parent::handle() !== 0) {
$success = false;
}
} finally {
$this->output->setVerbosity($verbosity);
}
if ($this->runningConcurrently) {
// todo@cli the Migrating info above always has extra spaces, and the success below does WHEN there is work that got done by the block above. same in Rollback
$this->components->success("Migrated tenant {$tenant->getTenantKey()}");
}
event(new DatabaseMigrated($tenant));

View file

@ -38,7 +38,7 @@ class MigrateFresh extends BaseCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants($chunk->all());
return $this->getTenants($chunk);
}));
}
@ -81,6 +81,8 @@ class MigrateFresh extends BaseCommand
}
/**
* Only used when running concurrently.
*
* @param LazyCollection<covariant int|string, \Stancl\Tenancy\Contracts\Tenant&\Illuminate\Database\Eloquent\Model> $tenants
*/
protected function migrateFreshTenants(LazyCollection $tenants): bool
@ -89,6 +91,8 @@ class MigrateFresh extends BaseCommand
foreach ($tenants as $tenant) {
try {
$this->components->info("Migrating (fresh) tenant {$tenant->getTenantKey()}");
$tenant->run(function ($tenant) use (&$success) {
$this->components->info("Wiping database of tenant {$tenant->getTenantKey()}", OI::VERBOSITY_VERY_VERBOSE);
if ($this->wipeDB()) {
@ -105,6 +109,8 @@ class MigrateFresh extends BaseCommand
$success = false;
$this->components->error("Migrating database of tenant {$tenant->getTenantKey()} failed!");
}
$this->components->success("Migrated (fresh) tenant {$tenant->getTenantKey()}");
});
} catch (TenantDatabaseDoesNotExistException|QueryException $e) {
$this->components->error("Migration failed for tenant {$tenant->getTenantKey()}: {$e->getMessage()}");

View file

@ -15,6 +15,7 @@ use Stancl\Tenancy\Concerns\ParallelCommand;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
use Stancl\Tenancy\Events\DatabaseRolledBack;
use Stancl\Tenancy\Events\RollingBackDatabase;
use Symfony\Component\Console\Output\OutputInterface as OI;
class Rollback extends RollbackCommand
{
@ -42,7 +43,7 @@ class Rollback extends RollbackCommand
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants($chunk->all());
return $this->getTenants($chunk);
}));
}
@ -70,14 +71,29 @@ class Rollback extends RollbackCommand
foreach ($tenants as $tenant) {
try {
$this->components->info("Tenant {$tenant->getTenantKey()}");
$this->components->info("Rolling back tenant {$tenant->getTenantKey()}");
$tenant->run(function ($tenant) use (&$success) {
event(new RollingBackDatabase($tenant));
// Rollback
if (parent::handle() !== 0) {
$success = false;
$verbosity = (int) $this->output->getVerbosity();
if ($this->runningConcurrently) {
// The output gets messy when multiple processes are writing to the same stdout
$this->output->setVerbosity(OI::VERBOSITY_QUIET);
}
try {
// Rollback
if (parent::handle() !== 0) {
$success = false;
}
} finally {
$this->output->setVerbosity($verbosity);
}
if ($this->runningConcurrently) {
$this->components->success("Rolled back tenant {$tenant->getTenantKey()}");
}
event(new DatabaseRolledBack($tenant));