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:

  1. Define a RateLimiter in the boot method of AppServiceProvider
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);
    });
}
  1. 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')];
    }
}
  1. 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)

Most upvoted comments

I think the same problem is for php 8.x (for me 8.0.6) and Laravel 8.x (for me 8.43.0).