lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.41k stars 117 forks source link

FR: Expose the `fail` method #245

Closed edalzell closed 1 year ago

edalzell commented 1 year ago

I need to manually fail a job/action, but I can't call $this->fail like I could on a "real" job.

Can you please add this?

chris-sev commented 1 year ago

Saw this asked in another issue. The solution is to use the asJob() method

https://github.com/lorisleiva/laravel-actions/issues/158#issuecomment-1024735186

edalzell commented 1 year ago

Saw this asked in another issue. The solution is to use the asJob() method

#158 (comment)

How can I fail the job when it's running though, which will be in the handle method, likely?

The as.... methods are only called on instantiation, I need access when the task is running.

chris-sev commented 1 year ago

I figured you would use the asJob() and then pass that job itself to the handle like:

public function asJob(JobDecorator $job)
{
    $this->handle($job);
}

But I'm trying right now and can't get it to work.

lorisleiva commented 1 year ago

Hi there,

The as.... methods are only called on instantiation

@edalzell This is not true, the asJob method, just like the handle method, is called at runtime. See code below.

https://github.com/lorisleiva/laravel-actions/blob/02b773160d6ea0ea8a6755663b0e5b906494a70f/src/Decorators/JobDecorator.php#L54-L63

As you can see, both resolve parameters using the getPrependedParameters method which will pass in the JobDecorator if it is type-hinted. So you could even do the following, should you only need your action as a job.

public function handle(JobDecorator $job)
{
  // ...
  $job->fail();
}

But I'm trying right now and can't get it to work.

@chris-sev Your code should work. Could you let me know the error / unexpected behaviour you are getting? Brownie points for a PR that provides a regression test. 🙃

edalzell commented 1 year ago

Oh thanks, I didn’t know that!

edalzell commented 1 year ago

Closing cuz it looks like I can do it, thanks!