Writing · 2021

Laravel Short Schedule

Pyae Sone Aung From the archive

Artisan Command တွေကို စက္ကန့်တိုင်း run မယ်။

အရင်ကတော့ command တွေကို စက္ကန့်တိုင်း run ချင်ရင် infinite loop တစ်ခုပတ်ပြီး sleep(1) ဆိုပြီး ရေးချင်တာရေးပါတယ်။ (လိုက်မလုပ်ပါနဲ့ :3)

အခုတော့ laravel-short-schedule ကိုသုံးပြီး နည်းစနစ် မှန်မှန် လုပ်လို့ရပါပြီ။

အရင်ဆုံး package ကို install လုပ်ပါမယ်။

composer require spatie/laravel-short-schedule

ပြီးရင် app\Console\Kernel.php မှာ shortSchedule ဆိုတဲ့ function တစ်ခုလုပ်ပြီး ကိုယ် run ချင်တဲ့ command ထည့်ပါမယ်။

protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSchedule)
{
    $shortSchedule->command('inspire')->everySecond();
}

နောက်ဆုံး

php artisan short-schedule:run

လို့ run လိုက်ရင် shortSchedule function ထဲက command တွေကို သတ်မှတ်ထားတဲ့ အချိန်အလိုက် run နေမှာပါ။

အခုနမူနာအရဆို inspire ဆိုတဲ့ command ကို တစ်စက္ကန့်တိုင်း run နေမှာပါ။

5 စက္ကန့်တိုင်း run ချင်ရင်

$shortSchedule->command('inspire')->everySeconds(5);

Overlap မဖြစ်အောင် run ချင်ရင်

$shortSchedule->command('inspire')->withoutOverlapping()->everySecond();

သတ်မှတ်ထားတဲ့ အချိန်အတွင်း run ချင်ရင်

$shortSchedule->command('inspire')->between('09:00', '17:00')->everySecond();

တခြား option တွေလည်းရှိပါသေးတယ်။ ဒီမှာ ကြည့်လို့ရပါတယ်။

Production မှာဆိုရင်

php artisan short-schedule:run

ကို supervisor သုံးပြီး background process အနေနဲ့ run ဖို့လိုပါတယ်။ ဒါဆို ရပါပြီ။

ဆက်လက်ပြီး နောက်ကွယ်ကနေ ဘယ်လို အလုပ်လုပ်သွားတယ်ဆိုတာ ကျွန်တော်သိလောက် share ချင်ပါတယ်။

(နောက်ကွယ်က လုပ်တာတွေ မသိလည်းဘာမှ မဖြစ်ပါဘူး အလုပ်ဖြစ်ရင် အဆင်ပြေပါတယ်။ သိတော့ ကိုယ်အရေးတဲ့ code အပေါ်ကို ပိုပြီး confident ရှိတာပေါ့ 😁)

နောက်ကွယ်ကနေ အမြဲ run နေဖို့ အတွက်ကို React PHP ရဲ့ EventLoop ကို သုံးထားတာပါ။

ဒီ video မှာ Event Loop ဘယ်လို အလုပ်လုပ်တယ် ဆိုတာ ရှင်းပြထားပါတယ်။

IMAGE_ALT

Ref : https://freek.dev/1743-how-to-schedule-commands-to-run-every-few-seconds-in-a-laravel-app