segunda-feira, 7 de setembro de 2020

Supervisor

 

Install & Configure Supervisor

1. Installation

To install on Ubuntu 18.04 just run:

  $ sudo apt install supervisor

Check if the installation was successful with $ service supervisor status which return something like this:


  Active: active (running) since Tue 2019-11-05 15:27:12 UTC; 19s ago

2. Adjust user permissions for Supervisor

For Supervisor being able to access the artisan command with sudo, we need to create a new user group and add the active user to it:

  $ sudo groupadd supervisor
  $ sudo usermod -a -G supervisor $USER

Then you need to adjust the main supervisor config file to have the necessary permission. For that open the main config file with an editor:

  $ sudo nano /etc/supervisor/supervisord.conf

The contents should be adjusted like this:


; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0770                       ; socket file mode (default 0700)
chown=root:supervisor

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

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

What has been changed is the socket file mode (chmod=0770) and the user group supervisor assigend to root (chown=root:supervisor).

After adjusting the main config you will need to restart the service:

  $ sudo service supervisor restart

3. Add your program configuration file

Now after Supervisor has been setup correctly, it is time to implement the actual Laravel Queue Worker. For that you will need to create a new program configuration file which should be located in /etc/supervisor/conf.d. Create it wit nano:

  $ sudo nano /etc/supervisor/conf.d/laravel-worker.conf

And give it this content:


[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=sudo php /var/www/laravel/artisan queue:work --tries=3 --daemon
user=root
autostart=true
autorestart=true
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/laravel/storage/logs/test.log

The command actually calls the Laravel queue worker. Please insert your correct path to your application diretory here. Also read about further configuration in the official Docs.

4. Reset supervisor to apply changes

Finally run the following commands to make the changes take effect:

  $ supervisorctl reread
  $ supervisorctl update

Check if it is running with:

  $ supervisorctl status

If everything is correct, then you should see something like:


laravel-worker:queue_00                   RUNNING   pid 2093, uptime 0:01:46

Credits

Thanks to whabash090 for writing the initial tutorial.

Nenhum comentário:

Postar um comentário