VANHIEP.NET - Làm web giá rẻ - Thiết Kế Website - Thiết Kế Ứng Dụng Mobile
Hẹn giờ trong laravel

Hẹn giờ trong laravel

Hẹn giờ trong laravel

Để thực hiện việc hẹn giờ thực hiện 1 công việc gì đó trong laravel ta làm như sau :

Bước 1 : Ta tạo 1 command bằng lệnh như sau 

php artisan make:command CommandName

1 class command được tạo tự động trong thư mục App \ Console \ Commands \ CommandName.php

Chúng ta mở file này lên và cấu hình như sau

protected $signature = 'send:message';

Trong dấu nháy kép là từ khóa command

protected $description = 'Command description';

Có thể nhập mô ta cho command ở đây.

public function handle()
    {
        $list = Notification::where('status', 0)->where('time', '!=', 0)->orderBy('id', 'desc')->get(); 
        foreach ($list as $key => $value) {
            if((int)$value->time / 1000 < time()){
                $list[$key]->update(['status'=>1]);
                $list[$key]->save();
                $receiver_id = json_decode($list[$key]->receiver_id);
                $list[$key]->receiver_id = $receiver_id;
                
                $arr = [
                    'title'         =>$value->title,
                    'message'       =>$value->message,
                    'time'          =>$value->time,
                    'status'        =>$value->status,
                    'channel'       =>$value->channel,
                    'receiver_id'   =>$receiver_id
                ];
                PushMess::dispatch($arr);
            }
        }
        return 0;
    }

Trong hàm handle ta thực hiện các công việc khi command được gọi. Như ví dụ trên chúng ta lấy dữ liệu từ database và so sánh với thời gian hiện tại. nếu thỏa mãn thì thực hiện 1 công việc gì đó.

Bước 2 : Cấu hình cho file kernel.php

Mở file Kernel ở thư mục App \ Console \ Kernel.php

protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')->hourly();
        $schedule->command('send:message')->everyMinute();
    }

Trong hàm schedule chúng ta khai báo như trên, chúng ta có thể khai báo tần suất lặp lại của command theo các hàm đã cung cấp sẵn trong laravel.

protected function commands()
    {
        $this->load(__DIR__.'/Commands');
        Commands\RedisCommand::class;
        Commands\SendMessage::class;

        require base_path('routes/console.php');
    }

Trong hàm commands ta thêm đường dẫn tới class command của mình vừa tạo như vi dụ trên.

Bước 3 : Viết server trên server để chạy lệnh schedule liên tục

Trên server tạo 1 thư mục services. Trong thư mục này tạo 1 file name.service có nội dung như sau

[Unit]
Description = Lee Dong Chul service send mess
After = network.target

[Service]
ExecStart = /patch/to/name.sh

[Install]
WantedBy = multi-user.target

Tiếp tục tạo 1 file name.sh có nội dung như sau

#!/bin/sh
echo "schedule running work ...."
cd /path/to/root_project_laravel
php artisan schedule:work

Tiếp theo chúng ta copy file name.service vào thư mục 

/etc/systemd/system

Sau đó chúng ta chạy lệnh sau để cập nhật dịch vụ mới cho server

sudo systemctl daemon-reload

Chạy lệnh sau để khởi động dịch vụ (Nhớ thay tên dịch vụ vào cho đúng nha)

sudo systemctl start name.service

Kiểm tra trạng thái của dịch vụ

sudo systemctl status name.service

Chạy dịch vụ khi khởi động lại server

sudo systemctl enable name.service

Tắt dịch vụ

sudo systemctl disable name.service

Lưu ý : Tùy thuộc vào server chạy hệ điều hành gì mà các câu lệnh và thư mục chứa dịch vụ như ví dụ trên sẽ khác nhau.

 

Chúc bạn thành công.