Skip to content

[13.x] Allow Jobs to include context#60243

Open
jackbayliss wants to merge 2 commits into
laravel:13.xfrom
jackbayliss:13.x-allow-jobs-to-include-context
Open

[13.x] Allow Jobs to include context#60243
jackbayliss wants to merge 2 commits into
laravel:13.xfrom
jackbayliss:13.x-allow-jobs-to-include-context

Conversation

@jackbayliss
Copy link
Copy Markdown
Contributor

@jackbayliss jackbayliss commented May 22, 2026

Happy bday week!

When inspecting queued jobs or handling job events, you often just want a bit more context , especially during deployments. This is different from global "world" illuminate:log:context and more about the job context itself the job doesn't have to run for us to have this context just be dispatched.

I made a video to also cover this... (audio on, bit boring)
Screen.Recording.2026-05-26.at.12.53.59.mov

This PR adds a HasContext interface, which adds context into the job payload, implement it on your job and return whatever is useful:

class AgentJob implements ShouldQueue, HasContext
{
    public function context(): array
    {
        return [
            'provider' => $this->provider,
            'model'    => $this->model,
        ];
    }
}

Then when inspecting the queue we can access it:

  Queue::reservedJobs()->first()->context

Or in a listener access the context we need... no need to dig in the payload

  public function handle(JobProcessing $event): void
  {
      Log::error('Processing Job', [
          'job' => $event->job->resolveName(),
          ...$event->job->context() ?? [],
      ]);
  }

The context is completely controlled in userland, and completely opt in.

Used an interface cause I can't think of a nicer non b/c way , perhaps in 14.x we could method_exists if you don't like it, but I thought it was ok for now.

This is my eyes would also be very cool to surface to monitoring services too such as Telescope, etc.

This means we can go logging from reservedJobs from:

[11:50:11] Deployment waiting for App\Jobs\AgentJob

To

[11:50:11] Deployment waiting for App\Jobs\AgentJob {"provider":"anthropic","model":"claude-sonnet-4-20250514"}

Specific to the job, and all contained within that job...

Any adjustments, have at it, or let me know where to tweak ... happy to rename too maybe metadata is more fitting?

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss force-pushed the 13.x-allow-jobs-to-include-context branch from 392f281 to 5b99c8c Compare May 23, 2026 22:53
Update Job.php

this would be useful

Update RedisQueueTest.php

lil test improvements

no empty array...

cs

oop

created should be last tbf
@jackbayliss jackbayliss marked this pull request as ready for review May 27, 2026 07:16
@jackbayliss jackbayliss marked this pull request as draft May 27, 2026 16:07
@jackbayliss jackbayliss marked this pull request as ready for review May 27, 2026 16:27
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

Successfully merging this pull request may close these issues.

1 participant