Laravel artisan: 每秒监听命令
Laravel artisan: listen command every second
我有两个使用 AMQP 进行通信的微服务。
我用bschmitt/laravel-amqphttps://github.com/bschmitt/laravel-amqp.
我创建了一个将消息发布到队列的操作。
然后我创建了 artisan 命令,它使用队列中的消息。
出版商示例:
Amqp::publish('', $user->toJson() , ['queue' => 'MS:USER:CREATED']);
消费者示例:
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
$result = json_decode($message->body);
Log::info($result);
$resolver->acknowledge($message);
$resolver->stopWhenProcessed();
});
我需要每秒听这个命令。为此,我使用主管。
主管配置示例:
[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
但主管启动并在几秒钟后停止。
这是我重启主管时的错误输出:
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)
然后为了获取消息,我需要每次都重新启动 supervisor。
我还知道另一种方法:
包装 Amqp::consume 并为此使用 crontab。
示例:
while (true) {
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
//some part of code
});
sleep(1);
}
这种方法可行,但我认为这不是个好主意。
也许在与主管的接触中,我对这种情况的配置有误。
如有任何帮助,我将不胜感激
您可以通过在主管配置命令Bash中使用 while 循环来做到这一点
[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
我有两个使用 AMQP 进行通信的微服务。
我用bschmitt/laravel-amqphttps://github.com/bschmitt/laravel-amqp.
我创建了一个将消息发布到队列的操作。
然后我创建了 artisan 命令,它使用队列中的消息。 出版商示例:
Amqp::publish('', $user->toJson() , ['queue' => 'MS:USER:CREATED']);
消费者示例:
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
$result = json_decode($message->body);
Log::info($result);
$resolver->acknowledge($message);
$resolver->stopWhenProcessed();
});
我需要每秒听这个命令。为此,我使用主管。
主管配置示例:
[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
但主管启动并在几秒钟后停止。 这是我重启主管时的错误输出:
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)
然后为了获取消息,我需要每次都重新启动 supervisor。
我还知道另一种方法:
包装 Amqp::consume 并为此使用 crontab。
示例:
while (true) {
Amqp::consume('MS:USER:CREATED', function ($message, $resolver) {
//some part of code
});
sleep(1);
}
这种方法可行,但我认为这不是个好主意。
也许在与主管的接触中,我对这种情况的配置有误。
如有任何帮助,我将不胜感激
您可以通过在主管配置命令Bash中使用 while 循环来做到这一点
[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