framework: [8.x] Failed to send queued notifications with `RateLimited` middleware: `Exception: Serialization of 'Closure' is not allowed`
- Laravel Version: v8.19.0
- PHP Version: v7.4.8
- Database Driver & Version: N/A
Description:
The notification channel we are using is limiting 20 requests per minute. Since job middleware are supported for queued notifications (#30070), we would like to leverage Illuminate\Queue\Middleware\RateLimited middleware for the queued notification, as described in https://laravel.com/docs/8.x/queues#rate-limiting. But it throws exception: Serialization of 'Closure' is not allowed.
Steps To Reproduce:
- Define a
RateLimiterin the boot method ofAppServiceProvider
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
RateLimiter::for('notification-channel-xxx', function ($job) {
return Limit::perMinute(20);
});
}
- Attach the rate limiter to the notification
<?php
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Middleware\RateLimited;
class SomeMessage extends Notification implements ShouldQueue
{
use Queueable;
/**
* Get the middleware the job should pass through.
*
* @return array
*/
public function middleware()
{
return [new RateLimited('notification-channel-xxx')];
}
}
- Send notification
$user->notify(new SomeMessage('test'))
// Exception with message 'Serialization of 'Closure' is not allowed'
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (14 by maintainers)
I think the same problem is for php 8.x (for me 8.0.6) and Laravel 8.x (for me 8.43.0).
Fixed for PHP 7.4+ by this: https://github.com/laravel/framework/commit/f3d4dcb21dc66824611fdde95c8075b694825bf5