I have two microservices which communicate with help AMQP.
I use bschmitt/laravel-amqp https://github.com/bschmitt/laravel-amqp.
I created one action for publish message to queue.
Then I created artisan command which consume message from queue. Example of publisher:
Amqp::publish('', $user->toJson() , ['queue' => 'MS:USER:CREATED']);
Example of consumer:
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
$result = json_decode($message->body);
Log::info($result);
$resolver->acknowledge($message);
$resolver->stopWhenProcessed();
});
I need to listen this command every second. For this I use supervisor.
Example of supervisor configuration:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/user/Projects/other/user/artisan ms:user:created
autostart=true
autorestart=true
user=user
numprocs=8
redirect_stderr=true
stdout_logfile=/home/user/Projects/other/user/worker.log
But supervisor start and after few second down. This is output of error when I restart supervisor:
laravel-worker:laravel-worker_02: ERROR (spawn error)
laravel-worker:laravel-worker_06: ERROR (spawn error)
laravel-worker:laravel-worker_00: ERROR (spawn error)
laravel-worker:laravel-worker_01: ERROR (spawn error)
laravel-worker:laravel-worker_04: ERROR (spawn error)
laravel-worker:laravel-worker_07: ERROR (spawn error)
laravel-worker:laravel-worker_03: ERROR (spawn error)
laravel-worker:laravel-worker_05: ERROR (spawn error)
Then in order to get the messages I need to restart the supervisor every time.
Also I know another approach:
Wrap Amqp::consume and use crontab for this.
Example:
while (true) {
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
//some part of code
});
sleep(1);
}
This approach work, but I don't think that this is good idea.
Maybe in approach with supervisor I have wrong configuration for this case.
I will be grateful for any help
You can do that by using a while loop in Bash in the supervisor configuration command
[program:ms-user-created]
process_name=%(program_name)s_%(process_num)02d
command=/bin/sh -c "while [ true ]; do (php /home/user/Projects/other/user/artisan ms:user:created &); sleep 1; done"
autostart=true
autorestart=true
numprocs=2
user=www-data
redirect_stderr=true