Laravel / Lumen Task Scheduling or Set Cron Job

Hello friends, today I will show you how to set cron jobs or task scheduling in a Laravel and Lumen (a micro-framework of Laravel) application. Laravel offers a flexible approach to manage your scheduled tasks on the server. Only one single cron entry is required on your server. All your jobs or tasks will be defined in the schedule method under app/Console/Kernel.php file. Let’s take an example and understand the whole thing.

How to Create:

To create the scheduled task files or cron job files you need to have make:command artisan command available in your application. If you are using Laravel you will have this command by default. But if you are using Lumen this command will not be available by default. You can check availability by running the php artisan command on your project root directory in the terminal/cmd window. If not, please install the Lumen Generator package in your application.

Installation of Lumen Generator:

This is an extra step needed for Lumen users only. Run the following command in your command/terminal window.

composer require flipbox/Lumen-generator

Configuration:

To register this package in your application please add the following code in your bootstrap/app.php file.

$app->register(Flipbox\LumenGenerator\LumenGeneratorServiceProvider::class);

By doing the above steps correctly you will get some extra generator commands in your Lumen application. make:command will be one of them.

You may also like: Handle CORS error in Lumen

How to create a scheduler

Now the make:command is available for both Laravel and Lumen users. With the use of this command we will clean a table every hour in our example.

php artisan make:command CleanTable

By running the above command a file named CleanTable.php will be created under app/Console/Commands. Once you open the file it will look like below:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CleanTable extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
}
}

Here the value of $signature would be the name of your command for the task you are going to run and it could be anything. Replace the value of the $signature and $description property on your own as per the purpose of the task/job. And write your main script inside the handle method. As per my example, my script would look like below:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CleanTable extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'clean:table';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// Clean the log table
DB::table('log_table')->delete();
}
}

Once you have done this you need to register your command in the Kernel file which is located in app/Console/Kernel.php. Open your Kernel.php file and paste the below code under the $commands array.

Commands\CleanTable::class

Now schedule the task as per requirement. As my script will run every hour so in Kernel.php my schedule method will look like

$schedule->command('clean:table') ->hourly();

Change the schedule frequency as per your requirement. Check the available list of schedule frequency Options by running the below command

php artisan schedule:list

We have seen how to create and define a scheduled task. Now we will see how we can run the scheduler.

Run Scheduler Locally

To run the scheduler script we don’t need to set up any scheduler cron to our development machine. Just use the schedule:run artisan command instead.

php artisan schedule:run

Run Scheduler on the Server

To run the scheduler we need to configure it on our server that runs the schedule:run artisan command as per the schedule frequency defined in the file. In our example, the script will run every hour and clean the table.

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

And that’s all. To get more options on scheduling please go through the Laravel official website https://Laravel.com/docs/8.x/scheduling.

You may also like: Validate JWT Token In Lumen

Originally published at https://www.codinghub.net.

Coding Hub — Learn & Build Your Career