Laravel – Horizon & Totem

Lâu rồi không update bài gì cho SoulEvil nên nhân tiện post linh tinh 1 chút về 2 món này.

Horizon

Okay define 1 chút về Horizon. Giả sử có file conf Supervisor sau

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/laravel/install/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=your_local_user
numprocs=8
redirect_stderr=true
stdout_logfile=/path/to/your/laravel/install/queue.log
stderr_logfile=/path/to/your/laravel/install/queue.err.log

Oki em Supervisor này có nhiệm xử lý default queue với số lượng là 8 workers ! Cơ bản vậy thôi. Cũng không có gì phức tạp nhỉ.

Vậy nếu ta có hơn 1 channel default thì sao ? Như với XGallery mình có

    const QUEUE_JAV           = 'jav';
    const QUEUE_TRUYENTRANH   = 'truyentranh';
    const QUEUE_BATDONGSAN    = 'batdongsan';
    const QUEUE_DOWNLOADS     = 'downloads';
    const QUEUE_FLICKR        = 'flickr';
    const QUEUE_GOOGLE        = 'google';

Tổng cộng 6 channels. Và như cách cổ điển thì ta sẽ config 8 x 6 : 48 workers ! Mỗi channel có 8 workers.

Cũng không có gì. Tuy nhiên trên con VPS yếu đuối của mình chỉ có 2GB RAM thì ngần ấy workers khá mệt mỏi. Song song đó 1 số channels có những lúc rất bận và có những lúc rất rảnh. Vì vậy chia đều 8 x 6 không phải ý hay,

Song song đó mình cũng khó handle được ( trực quan ) jobs & workers chạy thế nào. Em nào chạy ngốn nhiều time nhất ! Để từ đó optimize ! Horizon giải quyết bài toán này

  • Giao diện trực quan để biết channels / workers nào chạy thế nào
  • Đặc biệt: Tự động gia giảm workers của từng channels tuỳ theo số jobs nó đang có ( dựa trên tổng số workers cho phép xuyên suốt channels )

Horizon provides a beautiful dashboard and code-driven configuration for your Laravel powered Redis queues. Horizon allows you to easily monitor key metrics of your queue system such as job throughput, runtime, and job failures.

All of your worker configuration is stored in a single, simple configuration file, allowing your configuration to stay in source control where your entire team can collaborate.

Install khá simple. Redis là required. Install qua composer như bình thường

composer require laravel/horizon

Và publish qua artisan

php artisan horizon:install

Edit config/horizon.php

'environments' => [
    'production' => [
        'supervisor-1' => [
            'connection' => 'redis',
            'queue' => ['default'],
            'balance' => 'auto',
            'minProcesses' => 1, // Process tối thiểu
            'maxProcesses' => 10, // Process tối đa có thể xài trên 1 channel
            'tries' => 3,
        ],
    ],
],

Và giờ thì update Supervisor để trỏ về Horizon ( thay vì Queue )

[program:horizon]
process_name=%(program_name)s
command=php /home/forge/app.com/artisan horizon
autostart=true
autorestart=true
user=forge
redirect_stderr=true
stdout_logfile=/home/forge/app.com/horizon.log
stopwaitsecs=3600

user: Horizon sẽ được execute dưới user này

Update Supervisor xong thì update lại nó thôi

sudo supervisorctl reread

sudo supervisorctl update

Vậy là xong.

Vậy còn Totem còn lại là gì.

https://github.com/codestudiohq/laravel-totem

Totem phục vụ cho viêc chạy schedule tasks “động” thay vì cố định trong Kernel.php

Bình thường thì ta define schedule trong Kernel và fixed frequency ( https://laravel.com/docs/7.x/scheduling#schedule-frequency-options ) . Cũng chả sao. ! Nhưng với XGallery mình cần động ! Nghĩa là lúc này mình sẽ setup là everyMinute nhưng lúc sau thì là everyFiveMinutes . Không lẽ cứ mỗi lần change là đục code ??

Có 1 cách ! Mình tạo 1 table schedules trong db và cho commands vô đây kèm với frequency ! Kernel sẽ load Commands thông qua db ! Fine ! Nhưng … mỗi lần change lại phải vô phpmyadmin hoặc qua Tinker ! Mà chưa kể muốn check output của commands cũng mệt vl !

Vậy thôi Totem !

Cũng install qua composer & install qua artisan thôi

composer require studio/laravel-totem
php artisan totem:assets

Of course cronjob vẫn trigger schedule:run như bình thường

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

Xong ! Nếu quởn thì secure lại 1 chút require isAdmin cho /horizon && /totem

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: