【发布时间】:2016-03-23 21:07:27
【问题描述】:
我正在尝试开发一个多租户多数据库应用程序,这基本上意味着每个租户都有自己的数据库、自己的用户、资源等。
当请求进来时,Laravel 需要知道使用哪个数据库连接,所以我编写了一个中间件,它基本上解析请求中的 JWT 并查找租户 ID 或用户名,然后简单地连接到租户的数据库。
p>但是现在我正在处理队列,并且我正在尝试覆盖 laravel 5 的默认行为,该行为连接到主数据库并插入失败的作业记录。
当我挖掘供应商文件时,我发现了一个FailedJobProvider接口:
<?php
namespace Illuminate\Queue\Failed;
interface FailedJobProviderInterface
{
/**
* Log a failed job into storage.
*
* @param string $connection
* @param string $queue
* @param string $payload
* @return void
*/
public function log($connection, $queue, $payload);
/**
* Get a list of all of the failed jobs.
*
* @return array
*/
public function all();
/**
* Get a single failed job.
*
* @param mixed $id
* @return array
*/
public function find($id);
/**
* Delete a single failed job from storage.
*
* @param mixed $id
* @return bool
*/
public function forget($id);
/**
* Flush all of the failed jobs from storage.
*
* @return void
*/
public function flush();
}
还有一个实现该接口的 DatabaseFailedJobProvider 类:
<?php
namespace Illuminate\Queue\Failed;
use Carbon\Carbon;
use Illuminate\Database\ConnectionResolverInterface;
class DatabaseFailedJobProvider implements FailedJobProviderInterface
{
/**
* The connection resolver implementation.
*
* @var \Illuminate\Database\ConnectionResolverInterface
*/
protected $resolver;
/**
* The database connection name.
*
* @var string
*/
protected $database;
/**
* The database table.
*
* @var string
*/
protected $table;
/**
* Create a new database failed job provider.
*
* @param \Illuminate\Database\ConnectionResolverInterface $resolver
* @param string $database
* @param string $table
* @return void
*/
public function __construct(ConnectionResolverInterface $resolver, $database, $table)
{
$this->table = $table;
$this->resolver = $resolver;
$this->database = $database;
}
/**
* Log a failed job into storage.
*
* @param string $connection
* @param string $queue
* @param string $payload
* @return void
*/
public function log($connection, $queue, $payload)
{
$failed_at = Carbon::now();
$this->getTable()->insert(compact('connection', 'queue', 'payload', 'failed_at'));
}
/**
* Get a list of all of the failed jobs.
*
* @return array
*/
public function all()
{
return $this->getTable()->orderBy('id', 'desc')->get();
}
/**
* Get a single failed job.
*
* @param mixed $id
* @return array
*/
public function find($id)
{
return $this->getTable()->find($id);
}
/**
* Delete a single failed job from storage.
*
* @param mixed $id
* @return bool
*/
public function forget($id)
{
return $this->getTable()->where('id', $id)->delete() > 0;
}
/**
* Flush all of the failed jobs from storage.
*
* @return void
*/
public function flush()
{
$this->getTable()->delete();
}
/**
* Get a new query builder instance for the table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function getTable()
{
return $this->resolver->connection($this->database)->table($this->table);
}
}
所以我想如果我编写自己的提供程序或者可以覆盖这个提供程序,我将能够在插入失败的作业之前告诉 laravel 连接到哪个数据库。但我对 SOLID 或 OOP 不太熟悉,并且对在这种情况下该怎么做感到困惑。
如何编写我自己的提供程序或覆盖这个提供程序以随时更改数据库连接?
【问题讨论】:
标签: php laravel laravel-5 queue