4

I'm torn on whether to schedule jobs or commands in the scheduler. I can't really find any in depth data on why I would choose one over the other. Typically, I've considered how long a given scheduled task will run and if it's "long" then I'll create a job, but I've recently switched a few jobs over to commands more recently because I can run them manually.

Also, if I'm using commands in the scheduler and I'm using runInBackground() how does that differ from a job?

2

1 Answer 1

8

When you use runInBackground, you're just sending the command to the shell background, like calling a command in the shell with & after the command.

Jobs can be executed in queues, which can be retried, scaled, executed with middlewares, executed in batches and monitored with tools like Laravel Horizon.

Tip: you can dispatch your jobs as commands by registering commands in routes/console.php that just dispatch the job, example:

Artisan::command('my-job-command', fn () => dispatch(new MyJob()));

The commands in this file are registered automatically by this code in the Kernel:

    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');

        require base_path('routes/console.php');
    }
Sign up to request clarification or add additional context in comments.

1 Comment

The tip about how you can run a job as a command with artisan ..., that's gold! That also (indirectly) answers the question what the difference between the two is, and which one to use ... a small remark - dispatch(MyJob()), shouldn't that be dispatch(new MyJob()) instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.