Scheduling and job in the Laravel

One of the things that can be implemented in programming projects is planning to run a process that will be executed at a certain time and it can also be repeatable. To do this, we can easily set a Cron.

Scheduling

In Laravel, scheduling allows us to easily define desired commands and only one Cron command on the server is needed to run the schedule.

In the past, you may have written a cron configuration entry for each task you needed to schedule on your server. However, this can quickly become a pain because your task schedule is no longer in source control and you must SSH into your server to view your existing cron entries or add additional entries.

Laravel’s command scheduler offers a fresh approach to managing scheduled tasks on your server.

https://laravel.com/docs/10.x/scheduling

Now we have learned the definition of scheduling, there is another topic called: job, jobs are processes that we can queue and execute in specific queues.

Job

In programming, a job typically refers to a unit of work that a computer program or system performs. This work could involve processing data, executing a specific task, or carrying out a series of operations. Jobs are often used in job scheduling, where tasks are queued and executed based on certain criteria such as time, dependencies, or available resources.

While building your web application, you may have some tasks, such as parsing and storing an uploaded CSV file, that take too long to perform during a typical web request. Thankfully, Laravel allows you to easily create queued jobs that may be processed in the background. By moving time intensive tasks to a queue, your application can respond to web requests with blazing speed and provide a better user experience to your customers.

https://laravel.com/docs/10.x/queues

Supervisor

Now, to run a job, we need a supervisor, the supervisor is responsible for running the tasks in the queue.

A supervisor is a process control system for operating systems. It provides a way to manage and monitor processes, ensuring that they restart if they crash or fail for any reason. Supervisor is commonly used to manage and monitor processes such as web servers, database servers, and other long-running services. It can be particularly useful for managing processes related to web applications, ensuring that they remain running and responsive. The supervisor also provides a web-based interface for monitoring and managing the processes it oversees.

Job Scheduling

In Laravel, you can schedule a job using the built-in task scheduler. Here’s a basic overview of how to do it:

  1. Define your job by creating a new class that implements the Illuminate\Contracts\Queue\ShouldQueue interface. This class will contain the logic of the job you want to schedule.
  2. Use the php artisan make:job Artisan command to generate a new job class, or create the class manually in the app/Jobs directory.
  3. Now, you can use Laravel’s task scheduler to define when and how often the job should run. You can do this by adding entries to the schedule method of your App\Console\Kernel class.

For example, you can schedule a job to run every day at midnight by adding the following code to the schedule method:

$schedule->job(new YourJobClass)->daily();

After defining your scheduled jobs, you need to add a single Cron entry to your server. This Cron entry should call the schedule:run Artisan command, which will dispatch your scheduled jobs.

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

By following these steps, you can easily schedule and run jobs in Laravel using the built-in task scheduler.

Leave a Reply

Your email address will not be published. Required fields are marked *