Cron Job in Laravel

တစ်မိနစ်တိုင်းမှာ Run စရာလိုတဲ့ Task တစ်ခု ရှိလာတယ်။

Task က our-domain.com/api/v1/craw ကို တစ်မိနစ်တစ်ခါ Run ရမှာ။

အရင်က https://cron-job.org သုံးတာ။ ပြဿနာက timeout ကို စက္ကန့်(၃၀)​ ပဲ​ ပေးတာ။ Craw လုပ်တဲ့ Process က နည်းနည်းကြာတယ်။​ ခန့်မှန်းခြေ စက္ကန့် (၄၀)​ ကနေ တစ်မိနစ်လောက်အထိ ဖြစ်နိုင်တယ်။

ဒီတော့ Thirdparty Service သုံးလို့မရ၊​ ကိုယ့်ဘာသာကိုယ် Cron Job ရေးဖို့ဖြစ်လာတယ်။

ဒီ route ကို GET Request ပိုတဲ့ artisan command တစ်ခု ရေးတယ်။ နောက် ဒီ command ကို တစ်မိနစ်တိုင်းမှာ run ပါမယ်ဆိုပြီး schedule ထဲမှာ သွားထည့်။

ဒါက Laravel ဘက်မှာ လုပ်ရမယ့် အပိုင်းပြီးပြီ။

နောက် သက်ဆိုင်ရာ OS က Cron Tab နဲ့ integration လုပ်ကြမယ်။

crontab -e

ဒီဖိုင်မှာ တစ်ကြောင်း ထည့်မယ်။

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

/path-to-your-project နေရာမှာ ကိုယ့် Laravel Project Path ကို ထည့်ပေးရမှာ။ ဉပမာ ဒီလို

* * * * * cd /var/www/html/our-laravel-project && php artisan schedule:run >> /dev/null 2>&1

ဒါကို Breakdown လုပ်ရမယ်ဆိုရင် php artisan schedule:run ကို OS ရဲ့ CRON JOB က မိနစ်တိုင်းမှာ run မယ်ပေ့ါ။

php artisan schedule:run ကတော့ Laravel Schedule မှာ ရေးထားတဲ့ Task တွေကို run ပေးတယ်ပြောရမလား။

ကောင်းတာက CRON JOB တွေကို တာမီနယ်မှာ ရှုပ်ရှုပ်ရှက်ရှက်တွေလုပ်စရာ လုပ်စရာမလိုဘူး။ Laravel က ပေးတဲ့ Expressive Syntax လွယ်လွယ်လေးတွေသုံးပြီး ကိုယ့် Task တွေကို အလွယ်တကူ Manage လုပ်လို့ရလာတယ်။

Let’s breakdown with ChatGPT


The expression * * * * * is a cron syntax that represents the schedule for executing a command. Each asterisk represents a specific time unit, as follows:

Minute (0-59)
Hour (0-23)
Day of the month (1-31)
Month (1-12)
Day of the week (0-7, where both 0 and 7 represent Sunday)
In the given expression * * * * *, all the asterisks are used, which means the command will run every minute.

The rest of the command cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 is the actual command being executed by the cron job.

Breaking it down:

cd /path-to-your-project: This changes the current directory to the specified path where your Laravel project resides. You should replace /path-to-your-project with the actual path to your project directory.

&&: This is a shell operator that allows you to execute multiple commands sequentially. In this case, it ensures that the following command is executed only if the directory change (cd) is successful.

php artisan schedule:run: This is the Laravel command that triggers the Laravel task scheduler. It checks if any scheduled tasks are due to be executed and runs them if necessary.

>> /dev/null 2>&1: These redirection operators redirect the output of the command. In this case, >> /dev/null redirects the standard output to the null device, discarding it. 2>&1 redirects the standard error output to the same place as the standard output (null device). Essentially, this part of the command suppresses any output or error messages generated by the scheduled tasks.

Putting it all together, the cron job executes the php artisan schedule:run command every minute, within the context of your Laravel project, discarding any output or error messages. This ensures that the Laravel task scheduler runs your scheduled tasks without generating unnecessary output.

Ref

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

Music Heal Me

Written on July 11, 2023