0

I need to run a job in queue, which takes a long time (2 hours around). It checks availability of some certain service. So instead of running one job for two hours, which constantly (every fice mins) makes an API request, I thought to use scheduling of laravel for queued jobs. I could call scheduler from anywhere by Artisan helper:

Artisan::call('schedule:run', [
   'args' => $args
]);

Which would dispatch a job. But can't figure out, how I can pass arguments ($arg1, $arg2, ..) in kernel.php, which my job file requires.

// Dispatch the job to the "heartbeats" queue...
$schedule->job(new Heartbeat($arg1, $arg2, ..), 'heartbeats')->everyFiveMinutes();

I tried to pass args in schedule method, but I suppose that's not the right way to do it.

Gio
  • 73
  • 2
  • 13
  • 1
    `php artisan schedule:run` is a command you should only use in a `crontab` If you want to create a job, I would advise making your own command which dispatches the job. Your own command can then get your args like so : https://laravel.com/docs/5.8/artisan#arguments and https://laravel.com/docs/5.8/artisan#programmatically-executing-commands – Techno Oct 30 '19 at 08:20
  • Thanks Rob. I was expecting this answer, but still wasn't sure since there is a way to dispatch job from scheduler without args. I just achieved my goal with another way: Inside my job checking.. if service isn't available, then emit _ServiceNotAvailable_ event, whose Listener waits for 5 mins and calls the same job. If available, then emit _ServiceAvailableEvent_ . – Gio Oct 30 '19 at 08:27

1 Answers1

0
  1. Make an artisan command. For example:

    php artisan make:command DispatchJobFromDynamicCalculatedArg

  2. Calculate $args whatever you want inside this artisan command.

  3. Dispatch target job with arguments which actually contain processes you need there.
  4. Schedule above artisan command in Kernel.php file.

That's it.

Alex
  • 1,148
  • 8
  • 30
  • Thanks. I guess I could use artisan command separately, but not with scheduler. Cuz I need to run schedule from a Job file many times until my service is available, so still need to pass arguments while calling schedule:run. – Gio Oct 30 '19 at 08:41