Open
Description
Description
Hi @ALL
I have a problem when queuing emails.
I use Horizon to process my queue and hyn in version 5.5.0.
Steps to reproduce for my problem:
- 2 tenants to configure
- In config/app.php, the name value is
foo
. (First tenant use this value by default) - I overloaded this value with
bar
on the second tenant - I put several emails for each tenant in the queue
- Shuffle, I receive the mail but the values (app.name) do not match
Have you ever encountered the problem?
I think the problem comes from LoadConfigs file that overwrites the default values of each tenant but if the first tenant has no overloaded values and the job is dispatched after the second, the configuration is not reset and therefore the value remains the value of the second tenant.
For now I have patched on my side by creating a middleware on the job and resetting the basic configuration and updating the tenant.
This is my middleware:
<?php
namespace App\Jobs\Middleware;
use App\Entities\Tenancy\Website;
use App\Services\ConfigurationLoader;
use Hyn\Tenancy\Environment;
class ApplyTenant
{
/** @var int */
private $website_id;
public function __construct(int $website_id)
{
$this->website_id = $website_id;
}
public function handle($job, $next)
{
$website = Website::find($this->website_id);
if ($website) {
app()->call(ConfigurationLoader::class . '@reset');
app(Environment::class)->tenant($website);
app('mailer')->alwaysFrom(config('mail.from.address'), config('mail.from.name'));
}
return $next($job);
}
}
<?php
namespace App\Services;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
class ConfigurationLoader extends LoadConfiguration
{
public function reset(Application $app)
{
config($this->items($app));
}
protected function items(Application $app)
{
$cached = $app->getCachedConfigPath();
if (file_exists($cached)) {
return require $cached;
}
$items = [];
foreach ($this->getConfigurationFiles($app) as $key => $file) {
$items[$key] = require $file;
}
return $items;
}
}
Information
- hyn/multi-tenant version: 5.5.0
- laravel version: 6.5.1
- database driver and version: PostgreSQL 11.5
- php version: 7.3.11