laravel-5.5laravel-jobs

Laravel 5.5 Job Chaining: Call to undefined method ::chain()


I have two jobs, first one needs to dispatch second before run, I find out job chain but can't make it work, all jobs set up from box, so in theory it should just work. Here is my first job:

namespace App\Jobs;

class MCSendJob
{
    use Dispatchable, InteractsWithQueue;

    public function handle(Config $config, MCReport $report)
    {
        if ($config->mcdb_status) {
            $report->setConnection('mcdb');
        } elseif ($config->rmcdb_status) {
            $report->setConnection('rmcdb');
        } else {
            $this->fail(new \Exception('No active MCDB connection found!'));
        }

        $models = collect([
            'appoitments' => Appointment::sent(false)->get(),
            'questions' => Question::sent(false)->get(),
            'price-clarifications' => PriceClarification::sent(false)->get(),
        ])->flatten(1);

        foreach ($models as $model) {
            $report->fill((new MCResource($model))->resolve());
            $report->save();

            $model->update(['mc_sent' => true]);
        }

        Log::info('MCSend done.');
    }
}

the second one:

namespace App\Jobs;

class MCCheckJob
{
    use Dispatchable, Queueable, MailerDriverMail;

    protected $dbConnection;

    public function __construct($dbConnection)
    {
        $this->dbConnection = $dbConnection;
    }

    public function handle(Config $config)
    {
        try {
            DB::connection($this->dbConnection)->getPdo();
            $config->{$this->dbConnection . '_status'} = true;
            $config->save();
            Log::info('MCCheck done.');
        } catch (PDOException $exception) {
            if ($this->dbConnection === 'mcdb') {
                Log::error('MCDB connection unavailable!');

                $config->mcdb_status = false;

                static::dispatch('rmcdb');
            } else {
                Log::error('RMCDB connection unavailable!');

                $config->rmcdb_status = false;
            }

            $config->save();

            Log::error('SQLSrv PDO Exception', ['error_code' => $exception->getErrorCode(), 'error_info' => $exception->getMessage()]);

            if(!empty($config->mailing_list_mc_reports)) {
                Mail::to($config->mailing_list_mc_reports)->send(new MCNoConnectionWarning($this->dbConnection));
            }
        }
    }
}

When I trying to dispatch job using chain, like this: MCSendJob::dispatch()->chain([new MCCheckJob('mcdb')]), or this: MCSendJob::withChain([new MCCheckJob('mcdb')])->dispatch(); I get next error: Call to undefined method App\Jobs\MCSendJob::chain(). I find those method in Illuminate\Foundation\Bus\PendingDispatch.

Can't figure out - where problem is.


Solution

  • You need to use the Queueable trait in both jobs