From 126afcd0dd897073481dc87e0aba701bafe2ee3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 15 Nov 2020 16:26:26 +0100 Subject: [PATCH] Terminating pipelines (#527) * Return false from CreateDatabase job * Terminating pipeline tests --- src/Jobs/CreateDatabase.php | 15 ++++---- tests/EventListenerTest.php | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/Jobs/CreateDatabase.php b/src/Jobs/CreateDatabase.php index 51ce8c210..3a74534d2 100644 --- a/src/Jobs/CreateDatabase.php +++ b/src/Jobs/CreateDatabase.php @@ -31,12 +31,15 @@ public function handle(DatabaseManager $databaseManager) { 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)); } } diff --git a/tests/EventListenerTest.php b/tests/EventListenerTest.php index 87096d436..4a45205cd 100644 --- a/tests/EventListenerTest.php +++ b/tests/EventListenerTest.php @@ -17,6 +17,7 @@ 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 @@ public function ing_events_can_be_used_to_cancel_tenancy_bootstrapping() $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