Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dhiman252's avatar

When to use Events and Jobs

I am using the laravel v5.2.* and really confused when should I use events and job. Since event listeners are also queued like jobs so when to user event and when to use jobs ?

Also does laravel supports the identification of the same job placed in the queue at top and bottom ?

0 likes
5 replies
saethor's avatar

Event should be fired when something is happening, like some user actions. When user is signing up, updating a profile or something like that.

Job should be a script that runs in a cron job or doing some background stuff . Database cleaning or monthly newsletter sending.

2 likes
bobbybouwmann's avatar

@seathor I disagree with you ;)

I would use events to let the system know something happend. So for example UserWasRegistered. Based on that you can fire a registration email and so on, so you can attach listeners.

Jobs however can be used for multiple thinks. Mostly to make sure you don't repeat stuff. Let's say that I have two ways for creating a user. One by the command line and one by a form. I then let them use the same job class so the functionality to create a user is on one place. You can then let a command from the command line call the same job. If something needs to change, you only have to change it on one place ;)

class CreateUser extends Job {
    public function handle($data = [])
    {
        return User::create($data);
    }
}

So the conclusion to me is that events are used to initiate other actions as well, so you let other actions know that they can be fired. A job is something that can happen multiple times and in multiple places.

4 likes
martinbean's avatar

Events and jobs can be thought of as exactly that.

An event describes something that’s happened. A user registering, an article being published, an order being placed, etc. You usually raise events and then have listeners do something in response to that event. So when a user registers you might want to send them a welcome email, or if a customer places an order to send an email receipt.

A job is a self-contained task. It can be executed synchronously or asynchronously. Jobs can be dispatched by a controller, a console command, or even an event listener. If you have a video streaming website, you might create a job for transcoding a video that could be dispatched by a controller when a user uploads a video, and you might also have an Artisan command where you can manually trigger a transcoding job for a particular video (i.e. if the transcoding job failed or you want to transcode to a different format).

Hopefully that helps!

9 likes
pauladams8's avatar

@martinbean you mention jobs can be dispatched by an event listener.

I have a job to provision a Let's Encrypt certificate. I need this job triggered once when a 'project' is created (an event is fired) and again every 60 days to renew the certificate. I thought that a job would best fit this use case rather than an event listener, but it's getting the job triggered the first time that's giving me headache.

When the ProjectCreated event is fired, I would like to dispatch the job in response (ie as an event listener). I decided the best way to do this would be to create an event listener, named ProvisionNewCertificate, which responds to the ProjectCreated event and in turn triggers the ProvisionCertificate job.

Unfortunately, I keep getting the error that the ProjectCreated event is being passed as the first parameter to the handle() method of my ProvisionCertificate job, rather than the dependencies I have type-hinted.

Why it this - it seems my job methods are conflicting with my event listener. Alternatively, could you suggest a better way of doing this?

On a sidenote, to run the ProvisionCertificate job every 60 days, I was going to create a task scheduler (which runs every 3 hours, to satisfy Let's Encrypt rate limiting) and execute the ProvisonCertificate job (which can create / renew a cert) on any certificates expiring in 30 days or less. Again, is this what you'd recommend or is there a better way I haven't thought of. Personally, I'm not a huge fan of the idea of using cron to manage potentially hundreds of certificates, especially if I'm going to deploy to a serverless platform.

pauladams8's avatar

Sorry, just noticed the errors I mentioned were because I had mixed up the ProvisionCertificate and ProvisonNewCertificate classes (they have similar names to be fair and I'm tired). Please ignore this.

Please or to participate in this conversation.