Laravel – Setup Scheduling & Queues on MacOS

There is one main advantage of Supervisor that the task you set there is working constantly. This mean that when the proces will finish the new one will starts immediately.

Crontab runs every process for a minute minimum! So if you have a task like queue:work is much better to use Supervisor over Crontab.

Supervisor VS CronJobs

Scheduling

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

Trước hết định nghĩa scheduling đã. Nhiệm vụ của em nó tương đương cronjob. Trong trường hợp này sẽ execute schedule:run theo chu kì minutes.

Tạo 1 file /Library/LaunchDaemons/net.xgallery.schedule.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyLi$
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>net.xcrawler.schedule</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/Cellar/php/7.4.4/bin/php</string>
        <string>/Users/vietvu/Sites/www/xgallery-laravel/artisan</string>
        <string>schedule:run</string>
    </array>

    <key>UserName</key>
    <string>vietvu</string>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

    <key>StartInterval</key>
    <integer>1000</integer>
</dict>
</plist>
  • UserName: Execute command dưới user vietvu
  • RunAtLoad: Execute command ngay sau khi được loaded
  • StartInterval: Tính theo seconds. Chu kì chạy

https://www.launchd.info/

sudo launchctl load -w net.xgallery.schedule.plist

Xem như schedule:run đã được setup để check every minutes

Và tiếp theo là supervisor

Scheduling ở trên sau khi executed và trigger jobs. Thì supervisor sẽ đảm nhiệm việc execute jobs.

Install supervisor qua brew

Edit /usr/local/etc/supervisord.ini

[include]
files = /usr/local/etc/supervisor.d/*.conf

Sau đó tạo file xgallery.conf theo path trên

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/Cellar/php/7.4.4/bin/php /Users/vietvu/Sites/www/xgallery-laravel/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=vietvu
numprocs=8
redirect_stderr=true
stdout_logfile=/Users/vietvu/Sites/www/xgallery-laravel/storage/logs/worker.log
stopwaitsecs=3600

Note:// Các path execute nên là đường dẫn tuyệt đối. Ví dụ php phải tuyệt đối đến binary file.

Xong. !

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 )

Google photo

You are commenting using your Google 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.

Blog at WordPress.com.

Up ↑

%d bloggers like this: