Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to fetch the underlying Job model from Job event hooks #25189

Closed
stephan-v opened this issue Aug 12, 2018 · 2 comments
Closed

Easier way to fetch the underlying Job model from Job event hooks #25189

stephan-v opened this issue Aug 12, 2018 · 2 comments

Comments

@stephan-v
Copy link
Contributor

stephan-v commented Aug 12, 2018

  • Laravel Version: 5.6
  • PHP Version: 7.1
  • Database Driver & Version:

Description:

Essentially what I want is a way to mark certain jobs to be picked up by the Job event hooks.

The Jobs that I want to get picked up extend from an Abstract class called AbstractTask. I was hoping since I could perform an instanceof check on the Job like so:

Queue::before(function (JobProcessing $event) {
    $job = $event->job;
    
    dd($job instanceof AbstractTask);
});

This turned on to not be the case since the job that is actually being referred here it not the actual but a driver specific Job like a syncJob which holds the actual job in its data.

This actually seems to work because the actual job is within the payload -> data -> command

Queue::before(function (JobProcessing $event) {
    $job = $event->job;
    
    $payload = json_decode($job->getRawBody());
    $data = unserialize($payload->data->command);

    if ($data instanceof AbstractTask) {
        dd('this works');
    }
});

I was hoping since this was Laravel there would be an easier way but checking out the underlying classes that does not seem like the case. Am I missing something here or is there no easier way to actually get the underlying Job which is actually used to initiate this? If there is nothing there I would love to add a method to simplify this process.

Currently I am using these hooks to create status updates for tasks so I know when certain tasks have started, are finished or have failed.

Any thoughts on an easier way to get this done or perhaps this could be an occasion to add an easier way to get this done?

@lagbox
Copy link
Contributor

lagbox commented Aug 13, 2018

Not sure if it is easier, but by looking at the code you may not have to deal with the payload directly yourself.

You can try getting the job name from $job there:

$class = $event->job->resolveName();

Once you have the name you can resolve an instance or just check if it inherits from what you want or is of what you want:

// with an instance
$instance = app($class);
$instance instanceof AbstractTask

// no instance
is_subclass_of($class, AbstractTask::class);
// or to be more inclusive
is_a($class, AbstractTask::class, true);

@driesvints
Copy link
Member

Heya, thanks for submitting this. This seems like a feature request or an improvement. It's best to post these at the laravel/ideas repository to get support for your idea. After that you may send a PR to the framework. Please only use this issue tracker to report bugs and issues with the framework.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants