Laravel Queue Pakai Pm2

29 Januari 2020 • 2 menit untuk membaca artikel ini

Laravel queues provide a unified API across a variety of different queue backends, such as Beanstalk, Amazon SQS, Redis, or even a relational database. Queues allow you to defer the processing of a time consuming task, such as sending an email, until a later time. Deferring these time consuming tasks drastically speeds up web requests to your application. – laravel queue docs

Oke jadi laravel itu punya fitur queue. Tapi daemonnya harus dijalankan manual pakai

php artisan queue:work

Ya kalau di dev mah gak masalah, kalau di production kan repot. Dari laravel ngasih contoh manage daemon pakai supervisor[d]. Tapi engga semua orang nyaman pakai supervisor[d]. Ada alternatif lain untuk menjalakan laravel queue, pakai PM2.

PM2 is a daemon process manager that will help you manage and keep your application online 24/7 – pm2.keymetrics.io

Setup daemon laravel queue pakai pm2 mudah banget, pertama install dulu pm2

npm install pm2 -g

Kemudian bikin definisi untuk proses pm2, aku beri nama laravel-queue-worker.yaml

apps:
  - name: laravel-queue-worker
    script: artisan
    exec_mode: fork
    interpreter: php
    instances: 1
    args:
      - queue:work
      - --tries=5
      - --sleep=1

Selanjutnya kita start definisi tsb

pm2 start laravel-queue-worker.yaml

Hasilnya bisa di cek pakai

pm2 ps

# outputnya
┌─────┬─────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name                    │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ laravel-queue-worker    │ default     │ N/A     │ fork    │ 21657    │ 18h    │ 0    │ online    │ 0%       │ 32.1mb   │ suneo    │ disabled │
└─────┴─────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

Langkah terakhir, enable auto start ketika mesin restart

pm2 startup
pm2 save

Selesai

Techlinuxlaravelqueuepm2

Sumarsono

System Administrator
Kembali ke atas

Manjaro Fix Touchpad X240 Setelah Sleep>>

<<Membuat Vanity Nameserver dns.he.net