I am trying to work on a multi tenant multi database application which basically means that every tenant has its own database, own users, resources etc.
Naturally when a request comes in, Laravel needs to know which DB connection to use so I wrote a middleware which basically parses the JWT in the requests and looks for the tenant id or username then simply connects to the tenant's database.
But now I am working with queues, and I am trying to over ride the default behaviour of laravel 5 which connects to the main DB and inserts the failed job record.
When I dug into the vendor files, I found a FailedJobProvider interface:
<?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();
}
And a DatabaseFailedJobProvider class which implements that interface:
<?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);
}
}
So I guess if I write my own provider or could over ride this one, I would be able to tell laravel which database to connect to before inserting the failed job. But I am not so seasoned with SOLID or OOP, and get confused what to do in situations like these.
How would write my own provider or over ride this one to change the database connection on the go?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire