I use Laravel 5with php 5.5.9-1ubuntu4.14and mysql 5.5.47. I have a multi-tenant environment with each tenant having a separate database. Therefore, for each HTTP request, I establish the appropriate DB connection in BeforeMiddlewarethis way:
public function handle($request, Closure $next)
{
...
Config::set('database.connections.mysql_client.host', $db_host);
Config::set('database.connections.mysql_client.database', $db_database);
Config::set('database.connections.mysql_client.username', $db_username);
Config::set('database.connections.mysql_client.password', $db_password);
DB::setDefaultConnection('mysql_client');
...
}
This works correctly even for concurrent HTTP requests.
But now I want to add scheduled tasks to my application. These tasks will send notifications to users belonging to all tenants. Therefore, I again need to connect to the relevant tenant databases. But this connection will not be through an HTTP request. Let's say I have a CommunicationControllerfunction inside it to send notifications.
public function sendNotification()
{
...
DB::purge('mysql_client');
Config::set('database.connections.mysql_client.host', $db_host);
Config::set('database.connections.mysql_client.database', $db_database);
Config::set('database.connections.mysql_client.username', $db_username);
Config::set('database.connections.mysql_client.password', $db_password);
DB::setDefaultConnection('mysql_client');
...
}
Config. Laravel Scheduler.
:
- , HTTP-?
concurrency ,
Config HTTP, ? - HTTP- DB, ?