framework icon indicating copy to clipboard operation
framework copied to clipboard

`Batchable` doesn't work properly without `InteractsWithQueue` trait

Open rahimi-ali opened this issue 1 year ago • 1 comments

Laravel Version

10.43.0

PHP Version

8.2.10

Database Driver & Version

No response

Description

As far as I know it is possible to have working jobs without using the InteractsWithQueue trait but when using the job batching functionality using Bus::batch and Batchable trait this is required for the batch to work properly.

When the InteractsWithQueue trait is not present on the job class the batch will never be considered as finished. I believe this is due to the if statement in the ensureSuccessfulBatchJobIsRecorded method on CallQueuedHandler class which returns early if either the job class does not use the Batchable trait or the InteractsWithQueue trait. I'm not quite sure why this is.

I think one of these solutions should be implemented:

  1. Use the InteractsWithQueue trait in the Batchable trait itself so it is guaranteed that it will work.
  2. If the if statement in ensureSuccessfulBatchJobIsRecorded is somehow not needed it can be removed and then it should work. I am not too sure about this because I couldn't find any uses of the methods available on the InteractsWithQueue trait used in Batchable trait or the Illuminate\Bus\Batch class but I might not have looked in the right places.
  3. Explicitly Mention this requirement in the documentation.

I am interesed in contributing to this issue but not quite sure which of these solutions or any other solution is prefered by the maintainers.

Steps To Reproduce

class SampleBatchableJob implements Illuminate\Contracts\Queue\ShouldQueue
{
    use Illuminate\Bus\Queueable;
    use Illuminate\Foundation\Bus\Dispatchable;
    use Illuminate\Bus\Batchable;
    
    public function handle() {}    
}

Illuminate\Support\Facades\Bus::batch([
    new SampleBatchableJob,
    new SampleBatchableJob
])->dispatch();

// after the jobs are processed `pending_jobs` will remain 2 and `finished_at` will remain null in the `job_batches` table.

rahimi-ali avatar Feb 12 '24 17:02 rahimi-ali

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

github-actions[bot] avatar Feb 13 '24 05:02 github-actions[bot]

Looks like we're not going to make any changes here right now sorry.

driesvints avatar Mar 05 '24 13:03 driesvints