部署至AWS Elastic Beanstalk后邮件视图丢失问题求助
问题:部署到AWS Elastic Beanstalk后邮件视图找不到,30分钟后自动恢复
每次将最新代码推送到AWS Elastic Beanstalk生产服务器时,发送邮件都会出现「View [emails.user-verification] not found」错误,30分钟后错误自动消失,邮件可正常发送。
部署执行命令
sudo php artisan cache:clear sudo php artisan config:cache sudo php artisan view:cache sudo php artisan optimize:clear sudo php artisan route:cache sudo php artisan migrate --force sudo php artisan db:seed --force sudo php artisan optimize:clear sudo php artisan storage:link sudo php artisan queue:restart
邮件验证类代码
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class NewUserVerification extends Mailable { use Queueable, SerializesModels; public $theme = 'primary'; public $newUser; /** * Create a new message instance. * * @return void */ public function __construct($newUser) { $this->newUser= $newUser; } /** * Build the message. * * @return $this */ public function build() { return $this->subject('Verify Email Address - '.$this->newUser->name) ->markdown('emails.user-verification'); } }
邮件视图文件(路径:resources/views/emails/user-verification.blade.php)
@component('mail::layout') {{-- Header --}} @slot('header') @component('mail::header', ['url' => config('app.url')]) {{ config('app.name') }} @endcomponent @endslot {{-- Body --}} # Hi, Please verify your email @component('mail::button', ['url' => url('/s/verify/'.$newUser->email_verification_token), 'color' => 'primary']) Verify Email Address @endcomponent {{-- Footer --}} @slot('footer') @component('mail::footer') © {{ date('Y') }} {{ config('app.name') }}. @lang('All rights reserved.') @endcomponent @endslot @endcomponent
失败任务错误栈
{ "class": "InvalidArgumentException", "message": "View [emails.user-verification] not found.", "code": 0, "file": "/var/app/current/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:137", "trace": [ "/var/app/current/vendor/laravel/framework/src/Illuminate/View/FileViewFinder.php:79", "/var/app/current/vendor/laravel/framework/src/Illuminate/View/Factory.php:137", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:381", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:358", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:270", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:211", "/var/app/current/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php:19", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:212", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:307", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php:253", "/var/app/current/vendor/laravel/framework/src/Illuminate/Mail/PendingMail.php:124", "/var/app/current/app/Jobs/WelcomeUser.php:37", "/var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36", "/var/app/current/vendor/laravel/framework/src/Illuminate/Container/Util.php:41", "/var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93", "/var/app/current/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37", "/var/app/current/vendor/laravel/framework/src/Illuminate/Container/Container.php:651", "/var/app/current/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:128", "/var/app/current/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:141" ] }
解决方案
1. 调整缓存命令顺序
原部署命令中,view:cache执行后又多次调用optimize:clear,而optimize:clear会清除包括视图缓存在内的所有缓存,导致部署后视图缓存缺失。需要把view:cache放到最后一次optimize:clear之后,确保生成的视图缓存不会被清除:
sudo php artisan cache:clear sudo php artisan config:cache sudo php artisan route:cache sudo php artisan migrate --force sudo php artisan db:seed --force sudo php artisan optimize:clear sudo php artisan view:cache # 移到optimize:clear之后 sudo php artisan storage:link sudo php artisan queue:restart
2. 确保队列进程完全重启
邮件发送通过队列任务执行,即使执行了queue:restart,AWS Elastic Beanstalk的队列进程可能存在重启延迟,旧进程仍在使用旧缓存。可以在视图缓存生成后再执行queue:restart,或者通过Elastic Beanstalk控制台手动重启应用环境,确保所有进程加载最新的视图缓存。
3. 检查并修复视图文件权限
部署后确认视图文件及目录的权限,确保队列进程和Web服务器有读取权限。在部署脚本中添加权限设置命令:
sudo chmod -R 755 resources/views sudo chmod 644 resources/views/emails/user-verification.blade.php
4. 理解自动恢复的原因
30分钟后错误消失,是因为当视图被访问时,Laravel会自动重新生成视图缓存。部署后缺失缓存的这段时间,首次访问视图会失败,直到缓存重新生成。调整命令顺序后可以避免这个问题。
内容的提问来源于stack exchange,提问作者Navneet Pratap
相关产品推荐
相关产品推荐

