Laravel Scheduler作业触发失败事件,即使它没有失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Laravel Scheduler作业触发失败事件,即使它没有失败相关的知识,希望对你有一定的参考价值。

我正在使用Laravel调度程序来运行作业,如果作业失败,我想写入日志并发送电子邮件。但是,即使作业运行良好,一旦我进行设置,我就开始收到电子邮件并在日志中输出,因此出于某种原因,Laravel将作业视为失败,如果没有失败。另外,如果我在Telescope中查看工作,则状态只会保持待处理状态,而不会显示为完成或失败。我将同步用作队列驱动程序。

这是我的kernel.php文件:

<?php

namespace App\Console;

use Log;
use App\Company;
use App\Jobs\ProcessPayments;
use App\Jobs\ProcessCompanyTiers;
use Illuminate\Console\Scheduling\Schedule;
use App\Jobs\ProcessPartialDiscountPayments;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel

    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\ProcessPartialDiscountPayments::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    
        $schedule->job(new ProcessPartialDiscountPayments)
            ->onSuccess(function () 
                Log::info('ProcessPartialDiscountPayments scheduled task successfully ran');
            )
            ->onFailure(function () 
                Log::info('ProcessPartialDiscountPayments errored and did not complete');
            )
            ->emailOutputOnFailure(config('app.webmaster_email'));
    

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    

    /**
     * Get the timezone that should be used by default for scheduled events.
     *
     * @return \DateTimeZone|string|null
     */
    protected function scheduleTimezone()
    
        return 'Europe/London';
    

这是作业文件:

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Jobs\ProcessCompanyPayment;
use App\ Company, DiscountCode ;
use Log;
use Carbon\Carbon;

class ProcessPartialDiscountPayments

    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $code_datetime;
    protected $datetime_to;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    
        Log::info('Running ProcessPartialDiscountPayments');

        $datetime = Carbon::now();
        $this->code_datetime = $datetime->format('Y-m-d H:i');
        $this->datetime_to = $datetime->format('Y-m-d H:i:s.v');
    

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    
        $discount_codes = DiscountCode::whereRaw("left(convert(varchar(20), ValidTo, 120), 16) = '".$this->code_datetime."'")->get();
        Log::info('Found '.$discount_codes->count().' discount codes');

        if(!empty($discount_codes)) 
            foreach($discount_codes as $discount_code) 
                if($discount_code->companies->count() > 0) 
                    foreach($discount_code->companies as $company) 

                        $active_code = $company->discount_code($company->balance_at($this->datetime_to), $this->code_datetime);

                        if($active_code && $active_code->Id == $discount_code->Id) 
                            Log::info('Processing #'.$company->Id.': '.$company->Name);
                            ProcessCompanyPayment::dispatch($company, $this->datetime_to);
                        

                    
                
            
        
    

这里是运行时生成的日志条目:

[2019-12-19 12:32:03] local.INFO: Scheduler running  
[2019-12-19 12:32:03] local.INFO: Running ProcessPartialDiscountPayments  
[2019-12-19 12:32:03] local.INFO: Found 0 discount codes  
[2019-12-19 12:32:03] local.INFO: ProcessPartialDiscountPayments errored and did not complete  

当前$discount_codes始终为空,因此预期输出应为handle函数只是写入日志,表明它未找到任何内容,然后作业完成。

答案

首先,您计划的作业在每次Cron运行时都会执行-您需要它吗? (我想,每分钟)。请分享您的日志文件内容吗?

以上是关于Laravel Scheduler作业触发失败事件,即使它没有失败的主要内容,如果未能解决你的问题,请参考以下文章

Quartz Scheduler作业在触发后连续运行

Laravel Scheduler and Cron-Laravel如何知道不运行基于daily(),hourly()等的计划作业

如果我的 Google Cloud Scheduler 作业失败,如何发送电子邮件提醒?

Cloud Run 完成,但 Cloud Scheduler 认为该作业已失败

使用 Cloud Scheduler 触发 Cloud Functions 的 HTTP

mysql 启动事件 启动触发器功能