amitshahc
2 weeks ago
76
16
Laravel

$schedule->job alway goes to onFailure

Posted 2 weeks ago by amitshahc

I have this code as one of the scheduler task in my Kernel.php file:

$schedule->job(new SubscriptionRenewal($this->repo))->everyMinute()
            ->before(function () {
                echo "\r\n\nSubscriptionRenewal job is started..\n";
            })
            ->after(function () {
                echo "\r\n\nFinished.!\n";
                // $this->_runDBqueue($schedule);
            })
            ->onSuccess(function () use ($schedule) {
                echo "\r\n\nSuccess\n";
                // $this->_runDBqueue($schedule);
            })
            ->onFailure(function () {
                echo "\r\n\nFailed\n";
            })
        // ->withoutOverlapping()
            ->appendOutputTo($this->logfile)
            ->emailOutputTo(config('constants.scheduler.report_email'));

When i run php artisan schedule:run from CLI it always print the Failed message even if the Queued notification i am executing in that job are being added in jobs table. So the task is successfully adding the jobs in table but still it is being considered as Failed task. no log can be found anywhere in the system.

This is my SubscriptionRenewal job class:

<?php

namespace Modules\SuperAdmin\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\SuperAdmin\Entities\Repository\Contract\CronjobRepository;
use Modules\SuperAdmin\Notifications\SubscriptionRenewal as RenewalNotification;

class SubscriptionRenewal implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $repo;
    
    // public $tries   = 10; // Max tries
    // public $timeout = 60; // Timeout seconds

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(CronjobRepository $repo)
    {
        $this->repo = $repo;
        // $this->connection = 'database';
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $shopowners_expiring = $this->repo->getExpiringSubscriptions($days = 7);
        // dd($shopowners_expiring->toArray());
        $shopowners_expiring->each(function ($shopowner) {

            \Notification::send($shopowner, new RenewalNotification);

            // \Mail::to($shopowner)->send((new SubscriptionRenewal($shopowner))->onConnection('database'));

            // dump($shopowner->toArray());
        });

        return true;
    }

    public function failed(\Exception $e)
    {
        dump($e);
        report($e);
    }
}

Please sign in or create an account to participate in this conversation.