lorisleiva / laravel-actions

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

Cannot access properties in getJobTags #172

Closed Patabugen closed 2 years ago

Patabugen commented 2 years ago

I'm not sure if this is a bug, a feature or just a bit of missing documentation!

The Problem When using LaravelActions and makeJob() I can't access my action properties when setting tags, as one can usually do with a Laravel Job.

https://laravel.com/docs/8.x/horizon#manually-tagging-jobs

We could add a test case for this here: https://github.com/lorisleiva/laravel-actions/blob/main/tests/AsJobWithCustomNameAndTagsTest.php

The Cause I think the difference comes down to how native Laravel Jobs are created vs how Laravel Action creates jobs - specifically Laravel passes arguments into the __construct but, from reading the source, Laravel Actions only passes them in to the handle() or asJob() methods.

https://github.com/lorisleiva/laravel-actions/blob/main/src/Decorators/JobDecorator.php#L41

This means that the object properties are not set when tags() is called on the job (and passed to jobTags() by the decorator).

Solutions... I'm not really sure how this can be resolved without some conversations about architecture - presumably part of the problem is that the different features Laravel Actions can become treat the constructor differently. Or perhaps the JobDecorator constructor could pass the parameters to a handler of some sort ( public function jobConstruct(...) for example?

lorisleiva commented 2 years ago

Hi there 👋

The JobDecorator will give you all the job parameters when calling the getJobTags method on your action.

To take back the example in the Laravel documentation:

// This, in a normal job...
public function tags()
{
    return ['render', 'video:'.$this->video->id];
}

// ... can be written like this, in an action.
public function getJobTags($video)
{
    return ['render', 'video:'.$video->id];
}

This is not documented in the guides of the documentation but it is explained in the references.

If you're curious, this is how this is done in the JobDecorator.

https://github.com/lorisleiva/laravel-actions/blob/4e60c9fbdfcea7d9977d882f3104c8996b43c169/src/Decorators/JobDecorator.php#L162-L165

I hope this helps. 🍀

Patabugen commented 2 years ago

Ah apologies, you are correct!

I keep forgetting that the Jobs via LaravelActions work slightly differently from the main Jobs.

I think an "Overview/Concept" section in the docs could be useful to help clarify/explain the overall differences between native Jobs and Actions Jobs. Something along the lines of the "Architecture Concepts" from the Laravel Docs. For example a bit explaining that makeJob() makes a job which is similar - but not identical - to a native Laravel one.

I would be happy to write one and submit a PR if you agree in principle.

lorisleiva commented 2 years ago

No worries. There's already a guide that explains how actions are wrapped inside decorators. Have you seen this?

Patabugen commented 2 years ago

Right you are! Thanks for the help!