phplaravel-5queuejobsdispatch

L5.5 - dispatch() throws an exception. How to continue the process?


I'm using L5.5 and I wrote a simple Controller that runs a dispatch() method.

Something like this:

class MyController extends Controller {
  public function insertData($data) {
    // do something before
    dispatch(new SetRegionJob([$data->id]));
    // do something after
  }
}

Sometimes, the dispatch() method throws an Exception (I'm investigating it... but this is not the problem now) and this Exception stops the process and do something after code, is not executed.

My idea is to use dispatch() to run a Job, but if an Exception is thrown for dispatch(), the process should continue running do something after code.

How I can modify the code to operate as I want?

Thank you.


Solution

  • You can use try catch to handle the exception, as an example:

    try {
       dispatch(new SetRegionJob([$data->id]));
    } catch (\Exception $e) {
       // do something like logging the exception
    }
    // do something after