在 rails 5 中监视 rake 任务

Monitor a rake task in rails 5

我有一个要求,我必须不断地从 AWS-SQS(简单队列服务)获取消息并更新模型的相关记录。该消息包含需要在获取相关用户后立即作为通知显示给相关用户的数据。这已通过使用 Action Cable 成功管理。我创建了一个 rake 任务,它从队列中获取消息并进行所需的处理。该任务应该 运行 无限循环。我有 2 个问题吗?

namespace :sqs_consumer do
  desc 'Get data from the AWS-SQS and process it.'
  task start: :environment do
    # initialize the sqs client
    loop do
      #read the queue for messages and process them in batches(if any)
    end
  end
end

1)针对上述需求创建rake任务是否正确? 运行s 无穷大的 rake 任务才是正道?如果不是,什么是正确的方法。我不能定期 运行 任务,因为我需要实时数据。

2) 我要监控任务。我正在使用 monit。不幸的是,同样的 Monit conf 似乎不起作用。我做错了什么或遗漏了什么?

check process aws_sqs_consumer with pidfile /var/www/myproject/shared/pids/sqs_consumer.pid
  start program = "/bin/sh -c 'cd /var/www/myproject/current; nohup bundle exec rake sqs_consumer:start RAILS_ENV=staging -i 0 -P /var/www/myproject/shared/pids/sqs_consumer.pid >> log/sqs_consumer.log 2>&1 &'" as uid ubuntu and gid ubuntu
  stop program = "/bin/sh -c 'kill $(cat /var/www/myproject/shared/pids/sqs_consumer.pid)'" as uid ubuntu and gid ubuntu

这个监控配置对我有用

check process aws_sqs_consumer with pidfile /var/www/myproject/shared/tmp/pids/sqs_consumer.pid
  start program = "/bin/sh -c 'cd /var/www/myproject/current && BACKGROUND=y PIDFILE=/var/www/myproject/shared/tmp/pids/sqs_consumer.pid LOG_LEVEL=info bundle exec rake sqs_consumer:start RAILS_ENV=staging'"
  stop program = "/bin/sh -c 'kill $(cat /var/www/myproject/shared/tmp/pids/sqs_consumer.pid)'" as uid ubuntu and gid ubuntu with timeout 90 seconds