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

[1.8.0] Add Queue support & Horizon integration (#104)

This commit is contained in:
Samuel Štancl 2019-08-19 14:37:57 +02:00 committed by GitHub
parent d04f4bb2a1
commit 8d8c177dac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 0 deletions

View file

@ -50,6 +50,7 @@ class TenancyServiceProvider extends ServiceProvider
$this->app->register(TenantRouteServiceProvider::class);
$this->registerTenantRedirectMacro();
$this->makeQueuesTenantAware();
}
public function registerTenantRedirectMacro()
@ -65,6 +66,31 @@ class TenancyServiceProvider extends ServiceProvider
});
}
public function makeQueuesTenantAware()
{
$this->app['queue']->createPayloadUsing(function () {
if (tenancy()->initialized) {
[$uuid, $domain] = tenant()->get(['uuid', 'domain']);
return [
'tenant_uuid' => $uuid,
'tags' => [
"tenant:$uuid",
"domain:$domain",
],
];
}
return [];
});
$this->app['events']->listen(\Illuminate\Queue\Events\JobProcessing::class, function ($event) {
if (\array_key_exists('tenant_uuid', $event->job->payload())) {
tenancy()->initById($event->job->payload()['tenant_uuid']);
}
});
}
/**
* Register services.
*

59
tests/QueueTest.php Normal file
View file

@ -0,0 +1,59 @@
<?php
namespace Stancl\Tenancy\Tests;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class QueueTest extends TestCase
{
/** @test */
public function queues_use_non_tenant_db_connection()
{
// todo finish this test. requires using the db driver
$this->markTestIncomplete();
}
/** @test */
public function tenancy_is_initialized_inside_queues()
{
$this->loadLaravelMigrations(['--database' => 'tenant']);
Event::fake();
dispatch(new TestJob());
Event::assertDispatched(JobProcessing::class, function ($event) {
return $event->job->payload()['tenant_uuid'] === tenant('uuid');
});
}
}
class TestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
logger(\json_encode(\DB::table('users')->get()));
}
}