Skip to content

Commit

Permalink
Terminating pipelines (#527)
Browse files Browse the repository at this point in the history
* Return false from CreateDatabase job

* Terminating pipeline tests
  • Loading branch information
stancl authored Nov 15, 2020
1 parent 8f12dd8 commit 126afcd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Jobs/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
72 changes: 72 additions & 0 deletions tests/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 126afcd

Please sign in to comment.