2

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?

Full Stack Alien
  • 11,244
  • 1
  • 24
  • 37
  • Great question, I've been confused about this as well :) – leo Oct 13 '22 at 14:12
  • 1
    I finally landed on something that I'm happy with! I wrote a quick post about it here: https://janestreetdigital.com/blog/commands-jobs-and-now-nova-actions/?ref=StackOverflow – Full Stack Alien Oct 13 '22 at 14:14

1 Answers1

5

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');
    }
Bruno Tomé
  • 528
  • 1
  • 6
  • 22
  • 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? – leo Oct 13 '22 at 14:15