即使未失败,Laravel Scheduler作业也会触发失败事件

时间:2019-12-19 12:25:32

标签: php laravel

我正在使用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函数只是写到日志中,指出它没有找到任何内容,然后作业完成。

0 个答案:

没有答案