-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] New JobRetrying event dispatched #39097
[8.x] New JobRetrying event dispatched #39097
Conversation
Can you show me what you plan to do in the listener for this event? |
Sure. The necessity of an event For example, imagine a situation like so: class User extends Model {
use UsesTenantConnection; // It configures the tenant connection dynamically
}
class TenantUserJob {
public function __construct(TenantUser $user) {
// ...
}
} When you dispatch the job for the first time, the job has the connection configured and works well. But, what happens when the job is unserialized from the retry command? you won't have a connection configured, and an event like app('events')->listen(JobRetrying::class, function (JobRetrying $event) {
if (! array_key_exists('tenantId', $event->payload())) {
return;
}
$this->findTenant($event)->makeCurrent();
}); |
I don't understand how that solves your problem. What does |
// `$tenantConnectionName` contains the tenant connection name configured by the developers
// `$databaseName` is the database name that `makeCurrent` takes from the landlord database
config([
"database.connections.{$tenantConnectionName}.database" => $databaseName,
]);
app('db')->extend($tenantConnectionName, function ($config, $name) use ($databaseName) {
$config['database'] = $databaseName;
return app('db.factory')->make($config, $name);
});
DB::purge($tenantConnectionName); It reconfigures the tenant connection using a multi-database approach. Another important task executes by app('queue')->createPayloadUsing(function ($connectionName, $queue, $payload) {
return ['tenantId' => Tenant::current()?->id];
}); Finally the task listen for the Laravel event app('events')->listen(JobProcessing::class, function (JobProcessing $event) {
Tenant::find($event->job->payload()['tenantId'])?->makeCurrent();
}); Now, take a look here: class User extends Model {
use UsesTenantConnection; // It configures the tenant connection dynamically, with the connection name previously seen
}
class TenantUserJob {
public function __construct(TenantUser $user) {
// ...
}
} When a job fails, the job is serialized. Using the command |
But, how does this help you if that stuff happens in the I guess the |
Looking to the Laravel What happens if you try to unserialize a model that has a reference to a connection that doesn't exist? It fails, and you can't retry the job: Using app('events')->listen(JobRetry::class, function (JobRetry $event) {
Tenant::find($event->job->payload()['tenantId'])?->makeCurrent();
}); |
I renamed the event to |
Thanks |
* New JobRetrying event dispatched * Style fixes * formatitng Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
* New JobRetrying event dispatched * Style fixes * formatitng Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
The PR introduces the new event
JobRetrying
, dispatched calling thequeue:retry
command.For example, it's a magic bullet for the models strictly related to a dynamic tenant connection. To learn more about it, see here: spatie/laravel-multitenancy#259 (comment)