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

Terminating pipelines (#527)

* Return false from CreateDatabase job

* Terminating pipeline tests
This commit is contained in:
Samuel Štancl 2020-11-15 16:26:26 +01:00 committed by GitHub
parent 8f12dd8829
commit 126afcd0dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 6 deletions

View file

@ -31,12 +31,15 @@ class CreateDatabase implements ShouldQueue
{
event(new CreatingDatabase($this->tenant));
if ($this->tenant->getInternal('create_database') !== false) {
$databaseManager->ensureTenantCanBeCreated($this->tenant);
$this->tenant->database()->makeCredentials();
$this->tenant->database()->manager()->createDatabase($this->tenant);
event(new DatabaseCreated($this->tenant));
// Terminate execution of this job & other jobs in the pipeline
if ($this->tenant->getInternal('create_database') === false) {
return false;
}
$databaseManager->ensureTenantCanBeCreated($this->tenant);
$this->tenant->database()->makeCredentials();
$this->tenant->database()->manager()->createDatabase($this->tenant);
event(new DatabaseCreated($this->tenant));
}
}

View file

@ -17,6 +17,7 @@ use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\UpdatingDomain;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Jobs\MigrateDatabase;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\QueueableListener;
use Stancl\Tenancy\Tenancy;
@ -127,6 +128,77 @@ class EventListenerTest extends TestCase
$this->assertSame([DatabaseTenancyBootstrapper::class], array_map('get_class', tenancy()->getBootstrappers()));
}
/** @test */
public function individual_job_pipelines_can_terminate_while_leaving_others_running()
{
$executed = [];
Event::listen(
TenantCreated::class,
JobPipeline::make([
function () use (&$executed) {
$executed[] = 'P1J1';
},
function () use (&$executed) {
$executed[] = 'P1J2';
},
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Event::listen(
TenantCreated::class,
JobPipeline::make([
function () use (&$executed) {
$executed[] = 'P2J1';
return false;
},
function () use (&$executed) {
$executed[] = 'P2J2';
},
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Tenant::create();
$this->assertSame([
'P1J1',
'P1J2',
'P2J1', // termminated after this
// P2J2 was not reached
], $executed);
}
/** @test */
public function database_is_not_migrated_if_creation_is_disabled()
{
Event::listen(
TenantCreated::class,
JobPipeline::make([
CreateDatabase::class,
function () {
$this->fail("The job pipeline didn't exit.");
},
MigrateDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener()
);
Tenant::create([
'tenancy_create_database' => false,
'tenancy_db_name' => 'already_created',
]);
$this->assertFalse($this->hasFailed());
}
}
class FooListener extends QueueableListener