代码之家  ›  专栏  ›  技术社区  ›  good_afternoon

Laravel:使用“later()”方法对电子邮件进行排队似乎可以立即发送

  •  0
  • good_afternoon  · 技术社区  · 4 年前

    在我的应用程序中,用户有可以发送到的电子邮件列表。他们的帐户设置了一天中自动发送电子邮件的时间和所在时区。

    我想在全球范围内为碳排放设定一个假时间。

    public/index.php ,我试图设置:

    $time = Carbon::create('2020-09-16 00:00:00');
            Carbon::setTestNow($time);
    

    但我的应用程序不受影响。

    有没有一种全球性的方法来设定假时间?


    在我的应用程序中,用户有可以发送到的电子邮件列表。他们的帐户设置了一天中自动发送电子邮件的时间和所在时区。

    在听众内部 handle 方法如下所示:

    public function handle(ReviewRequested $event)
        {
            
            $time = Carbon::create(2020, 9, 15, 0);
            Carbon::setTestNow($time);  
    
            $reviewRequest = $event->reviewRequest;
            Log::info('email sending at ' . $reviewRequest->sent_at . ' and current time is ' . Carbon::now());
            Mail::to($reviewRequest->customer->email)
                ->later($reviewRequest->sent_at, new ReviewRequestMailer($reviewRequest));
    
        }
    

    请注意,我用碳纤维来伪装时间,并将其设置为午夜。在本例中,电子邮件应该在上午9点发送。记录的信息如下:

    local.INFO: email sending at 2020-09-15 09:00:00 and current time is 2020-09-15 00:00:00

    所以现在的时间是凌晨12点,我在排队等待9点的邮件。

    我一跑 php artisan queue:work ,将立即运行并发送挂起的作业(电子邮件)。为什么会这样?他们应该一直排队到上午9点。

    也许排队是在使用系统时间,而不关心我在碳排放中设置了什么?我如何解决这个问题?

    我忘了用Redis

    1 回复  |  直到 4 年前
        1
  •  0
  •   IGP    4 年前

    检查您在 .env 文件。 QUEUE_CONNECTION=sync

    解决此问题的最快方法是执行以下操作:

    • 将驱动程序更改为数据库 QUEUE_CONNECTION=database
    • 清除缓存的配置 php artisan config:clear
    • php artisan queue:table
    • php artisan migrate

    在完成这些步骤之后,现在可以在使用运行队列时延迟执行 php artisan queue:work

        2
  •  0
  •   Asim Raza    4 年前

    我认为你应该用拉威尔·克罗恩的工作来达到这个目的。你应该在App/Console/Commands中创建一个文件/你的cronjobfile.php

         <?php
    
         namespace App\Console\Commands;
    
         use App\TestingCron;
         use Illuminate\Console\Command;
         use Illuminate\Support\Facades\DB;
    
         class TestingCronJob extends Command
       {
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'send:Mail';
    
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command is use for test cron jobs.';
    
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        DB::table('testing_cron')->insert(['created_at' => now(),'updated_at' => now()]);
    }
    }
    

    然后转到目录应用程序/控制台/内核.php

     <?php
    
     namespace App\Console;
    
     use Illuminate\Console\Scheduling\Schedule;
     use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
     class Kernel extends ConsoleKernel
    {
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\TestingCronJob::class
    ];
    
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('send:Mail')->dailyAt('09:00');
    }
    
    /**
     * RegisterController the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
    
        require base_path('routes/console.php');
    }
    }
    

    https://laravel.com/docs/7.x/scheduling