简体   繁体   中英

Laravel: Schedule a job or an artisan command?

I have a Product model with id,name,price .

The price value is stored in an external API and i need to fetch it every minute in order to update it in the database.

Looking through the Laravel documentation I found two ways to implement:

  1. Create an artisan command ( https://laravel.com/docs/8.x/artisan ) and add it to task scheduling ( https://laravel.com/docs/8.x/scheduling#scheduling-artisan-commands )
  2. Create a job ( https://laravel.com/docs/8.x/queues ) and add it to task scheduling ( https://laravel.com/docs/8.x/scheduling#scheduling-artisan-commands )

First of all, is there any other approach i should take in consideration?

If not, which one of the above would be the best approach and why is it correct for my use case?

As per my comments on one of your previous questions on this topic, whether you use a queue or not depends on your use case.

An Artisan command is a process that executes once and performs a task or tasks and then exits when that task is complete. It is generally run from the command line rather than through a user action. You can then use the task scheduling of your command's host operating system (eg a CRON job) to execute that command periodically. It will faithfully execute it when you schedule it to be done.

A Queued job will execute when the Job turns up next in the queue, in priority order. Let's say you send your API call (from your other post) to the queue to be processed. Another system then decides it needs to send out emails urgently (with a higher priority). Suddenly, your Job, which was next, is now waiting for 2000 other Jobs to finish (which might take a half hour). Then, you're no longer receiving new data until your Job executes.

With a scheduled job, you have a time critical system in place. With queues, you have a "when I get to it" approach.

Hope this makes the difference clearer.

With laravel it is a lot easy to use the built in scheduler. You have to add only one entry to the crontab and that is to run the command php artisan schedule:run EVERY MINUTE on your project. After that you dont have to thing about configuring the crontab on the server, you just add commands to the laravel scheduler and they will work as expected.

You should probably use Cron Job Task Scheduling which would be the first approach you mentioned.

Commonly for this type of use-cases commands are the easiest and cleanest approach.

There are a few things to do in order to make it work as expected:

  • Create a new command that will need to take care of hitting the endpoint and storing the retrieved data to the database
  • In Kernel.php file register your command and the frequency of running (each minute)
  • Run php artisan schedule:run

You can read more about how to create it here :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM