Laravel's command scheduler offers a fresh approach to managing scheduled tasks on your server. The scheduler allows you to fluently and expressively define your command schedule within your Laravel application itself.
To create Task Scheduler, please open your terminal and type command :
php artisan make:command ProcessOrder --command=process:order
The above command will create a new file ProcessOrder.php, in the app/Console/Commands directory. Navigate to the file and edit
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Order;
class processOrder extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'process:order';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Proses Order';
/**
* Execute the console command.
*/
public function handle()
{
//
$order_inqueue = Order::select('*')
->where('status','inqueue')
->get();
foreach ($order_inqueue as $q) {
$username = '081xxxxxxx';
$api_key = 'xxx7064b2d6dbcxxxxx'; //development
$sign = $username.''.$api_key.''.$q->id;
$sign = md5($sign);
$data_payload = [
"customer_id" => $q->acc,
"product_code" => $q->product_code,
"ref_id" => $q->id,
"username" => $username,
"sign" => $sign
];
$payload = json_encode($data_payload);
if ($q->command == 'top-up'){
$ch = curl_init("https://prepaid.iak.dev/api/top-up");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$curl_result = curl_exec($ch);
$curl_result_json = json_decode($curl_result);
switch ($curl_result_json->data->rc) {
case '00':
$status = 'success';
$status_desc = 'success';
break;
case '06':
$status = 'failed';
$status_desc = 'transaction not found';
break;
case '07':
$status = 'failed';
$status_desc = 'failed';
break;
case '10':
$status = 'failed';
$status_desc = 'reach top up limit using same destination number in 1 day';
break;
case '12':
$status = 'failed';
$status_desc = 'balance maximum limit exceed';
break;
case '16':
$status = 'failed';
$status_desc = 'wrong product code';
break;
case '17':
$status = 'failed';
$status_desc = 'isuficient deposit';
break;
default:
$status = 'unknown';
$status_desc = 'unknown';
break;
}
curl_close($ch);
if (isset($curl_result_json->data->tr_id)){
$u = Order::findOrFail($q->id);
$u->update([
'status' => $status,
'status_desc' => $status_desc,
'provider_trx_id' => $curl_result_json->data->tr_id,
]);
} else {
$u = Order::findOrFail($q->id);
$u->update([
'status' => $status,
'status_desc' => $status_desc,
]);
}
}
}
}
}
Task Scheduler in Laravel executes the artisan command, shell, or a callback periodically on the defined time. To do this, we use the schedule
method in app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands =[
Commands\processOrder::class,
];
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('process:order')->everyMinute();
// $schedule->command('inspire')->hourly();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
There are some more time intervals that we can define. You can replace ->daily(); with another time interval option from the following list. You can find more about Task Scheduling in Laravel Documentation.
To start the Laravel Scheduler itself, we only need to add one Cron job which executes every minute. Go to your terminal, ssh into your server, cd into your project and run this command.
crontab -e
The Command will open the server Crontab file, paste the code below into the file, save and then exit.
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Do not forget to replace /path/to/artisan with the full path to the Artisan command of your Laravel Application.