Laravel

command + forge schedule(스케줄)

짱구를왜말려? 2022. 4. 11. 22:21
반응형
SMALL

# What?

- 서버에서 일정 시간마다 어떤 작업을 예약하고 싶다면 사용

 

# How?

1. 스케줄 생성

php artisan make:command RefundFailLetter

 

2. 실행할 내용 작성

@ RefundFailLetter

   
   protected $signature = 'refund:fail-letter';

   protected $description = '발송실패건 환불';
    
   public function __construct()
   {
       parent::__construct();
   }
   
   public function handle()
    {
        $letters = Letter::where("finished", 1)->where("refunded", 0)->where("count_fail", ">", 0)->cursor();

        foreach($letters as $letter){
            $price = $letter->price * $letter->count_fail;

            $letter->update([
                "refunded" => 1,
            ]);

            $letter->user->update([
                "point" => $letter->user->point + $price
            ]);
        }
    }

@ Kernel.php

    protected $commands = [
        FormatCars::class,
    ];

3. Forge 설정

- 서버 선택 > Scheduler > Command 입력 > Frequency는 원하는 주기로 설정하고 재시작하기

php8.0 /home/forge/자기사이트명/artisan 명령어

 ex. php8.0 /home/forge/admin.lettrers.com/artisan check:letter-log

 

* 5분마다 실행하게 하고싶을땐?

Custom check -> */5 * * * *

LIST