Laravel 5 simultaneous request and scheduled work

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)
{
...
// Set the client DB name and fire it up as the new default DB connection
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');//IMP
  // Set the client DB name and fire it up as the new default DB connection
  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, ?
+4
2

Illuminate\Config\Repository, , Config , , set

public function set($key, $value = null)
{
    if (is_array($key)) {
        foreach ($key as $innerKey => $innerValue) {
            Arr::set($this->items, $innerKey, $innerValue);
        }
    } else {
        Arr::set($this->items, $key, $value);
    }
}

, IO (.. ), . , CLI, , , HTTP-.

+3

Laravel:

, , .

, Config::set, , - concurrency.

. , / , . , - , , multi-insert, sqldump ( ).. ..

+5

Source: https://habr.com/ru/post/1648456/


All Articles