horizon icon indicating copy to clipboard operation
horizon copied to clipboard

Cannot get the cancelled() batch inside a job class

Open extrabright opened this issue 2 years ago • 0 comments

  • Horizon Version: 5.9
  • Laravel Version: 9.11
  • PHP Version: 8.1.10
  • Redis Driver & Version: predis/phpredis 2.0
  • Database Driver & Version: mysql

Description:

In the docs, it says you should add the Illuminate\Bus\Batchable trait to the job class. This trait provides access to a batch method which may be used to retrieve the current batch that the job is executing within.

Steps To Reproduce:

I have a job (MainJob.php) that scans a list with many entries and creates smaller chunks that are added to the batch with their job class (e.g: ProcessItemsJob.php).

However, when I use the method below in the ProcessItemsJob class:

use Illuminate\Bus\Batchable;

public function handle() {
   if ($this->batch()->cancelled()) {
      // Determine if the batch has been canceled...
      return;
   }
   // Import a portion of the CSV file...
}

I get this error:

Call to a member function cancelled() on null

I checked in the job_batches table and the canceled_at columns are correctly set with the timestamp.

Any ideas?

extrabright avatar Oct 15 '22 09:10 extrabright