我开始在Laravel 5.4提供的Slack上使用通知系统,它太棒了(src:https://laravel.com/docs/5.4/notifications)!
但是我有一个问题是通过队列通知它会触发异常:序列化'关闭'不允许在/var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:125
你能帮帮我吗,我的错误是什么?
信息:
这是我在app / Notifications / Slack.php中的代码:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\SlackMessage;
class Slack extends Notification implements ShouldQueue
{
use Queueable;
private $channel;
private $username;
private $icon;
private $message;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(String $channel = '', String $username = '', String $icon = '', String $message = '')
{
$this->channel = $channel;
$this->username = $username;
$this->icon = $icon;
$this->message = $message;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['slack'];
}
/**
* Get the Slack representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->from($this->username, $this->icon)
->to($this->channel)
->content($this->message);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'channel' => $this->channel,
'username' => $this->username,
'icon' => $this->icon,
'message' => $this->message,
];
}
}
这是我在app / Providers / AppServiceProvider.php中使用松弛通知的代码:
namespace App\Providers;
use Queue;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Notifications\Notifiable;
use App\Notifications\Slack;
class AppServiceProvider extends ServiceProvider
{
use Notifiable;
/**
* Route notifications for the Slack channel.
*
* @return string
*/
public function routeNotificationForSlack()
{
return 'http://hook.slack.com/XXXXXx/my_slack_endpoint';
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// When a queue job fail
Queue::failing(function (JobFailed $event) {
// Notify team of failing job
$when = Carbon::now()->addSeconds(10);
$this->notify((new Slack('slack_channel', 'slack_username', 'slack_icon', 'Job Failed !'))->delay($when));
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
完整错误转储:
local.ERROR: Exception: Serialization of 'Closure' is not allowed in /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:125
Stack trace:
#0 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(125): serialize(Object(Illuminate\Notifications\SendQueuedNotifications))
#1 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(106): Illuminate\Queue\Queue->createObjectPayload(Object(Illuminate\Notifications\SendQueuedNotifications))
#2 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Queue.php(86): Illuminate\Queue\Queue->createPayloadArray(Object(Illuminate\Notifications\SendQueuedNotifications), '', NULL)
#3 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/DatabaseQueue.php(108): Illuminate\Queue\Queue->createPayload(Object(Illuminate\Notifications\SendQueuedNotifications), '')
#4 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(181): Illuminate\Queue\DatabaseQueue->later(Object(Carbon\Carbon), Object(Illuminate\Notifications\SendQueuedNotifications))
#5 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(159): Illuminate\Bus\Dispatcher->pushCommandToQueue(Object(Illuminate\Queue\DatabaseQueue), Object(Illuminate\Notifications\SendQueuedNotifications))
#6 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(73): Illuminate\Bus\Dispatcher->dispatchToQueue(Object(Illuminate\Notifications\SendQueuedNotifications))
#7 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(160): Illuminate\Bus\Dispatcher->dispatch(Object(Illuminate\Notifications\SendQueuedNotifications))
#8 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php(61): Illuminate\Notifications\NotificationSender->queueNotification(Array, Object(App\Notifications\Slack))
#9 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php(36): Illuminate\Notifications\NotificationSender->send(Array, Object(App\Notifications\Slack))
#10 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Notifications/RoutesNotifications.php(18): Illuminate\Notifications\ChannelManager->send(Object(App\Providers\AppServiceProvider), Object(App\Notifications\Slack))
#11 /var/www/project_test/app/Providers/AppServiceProvider.php(57): App\Providers\AppServiceProvider->notify(Object(App\Notifications\Slack))
#12 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(334): App\Providers\AppServiceProvider->App\Providers\{closure}(Object(Illuminate\Queue\Events\JobFailed))
#13 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('Illuminate\\Queu...', Array)
#14 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(172): Illuminate\Events\Dispatcher->dispatch('Illuminate\\Queu...', Array, false)
#15 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/FailingJob.php(36): Illuminate\Events\Dispatcher->fire(Object(Illuminate\Queue\Events\JobFailed))
#16 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(392): Illuminate\Queue\FailingJob::handle('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Swift_TransportException))
#17 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(378): Illuminate\Queue\Worker->failJob('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Swift_TransportException))
#18 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(321): Illuminate\Queue\Worker->markJobAsFailedIfWillExceedMaxAttempts('database', Object(Illuminate\Queue\Jobs\DatabaseJob), 3, Object(Swift_TransportException))
#19 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(295): Illuminate\Queue\Worker->handleJobException('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions), Object(Swift_TransportException))
#20 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(258): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#21 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(110): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#22 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(100): Illuminate\Queue\Worker->daemon('database', 'default,q-mail', Object(Illuminate\Queue\WorkerOptions))
#23 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(83): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'default,q-mail')
#24 [internal function]: Illuminate\Queue\Console\WorkCommand->fire()
#25 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#26 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#27 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#28 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Container/Container.php(524): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#29 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): Illuminate\Container\Container->call(Array)
#30 /var/www/project_test/vendor/symfony/console/Command/Command.php(265): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#31 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Console/Command.php(167): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#32 /var/www/project_test/vendor/symfony/console/Application.php(826): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#33 /var/www/project_test/vendor/symfony/console/Application.php(189): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#34 /var/www/project_test/vendor/symfony/console/Application.php(120): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#35 /var/www/project_test/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(123): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#36 /var/www/project_test/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#37 {main}
提前感谢您的回复,
Rergards
答案 0 :(得分:1)
像themsaid这样的Loook在laravel bug tracker上回答了您的问题请求。为了节省其他人的点击,如果你不介意,我会在这里引用这个回复:
不要使用Illuminate \ Notifications \ Notifiable,它的意思是 与Eloquent模型一起使用,现在Queue驱动程序将尝试进行serliaize 你的服务提供商认为这是通知,这是事情 你不想要。
相反,您可以创建一个MySlack类或使用的东西 需要通知并且您从服务提供商处发送通知 像:
Notification::Send(new MySlack(), new MyNotification());
答案 1 :(得分:0)
确保已实现ShouldQueue
并使用Queueable
特性:
class MyNotification extends Notification implements ShouldQueue{
use Queueable;
// ...
}