Ruby-on-rails 如何使用 monit 监控 delay_job

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1226302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:35:50  来源:igfitidea点击:

How to monitor delayed_job with monit

ruby-on-railsrubydelayed-jobmonitgod

提问by Luke Francl

Are there any examples on the web of how to monitor delayed_job with Monit?

网络上是否有关于如何使用Monit监控 delay_job 的示例?

Everything I can find uses God, but I refuse to use God since long running processes in Ruby generally suck. (The most current post in the God mailing list? God Memory Usage Grows Steadily.)

我能找到的一切都使用God,但我拒绝使用 God ,因为在 Ruby 中长时间运行的进程通常很糟糕。(大神邮件列表最新帖子?大神内存使用量稳步增长。)

Update:delayed_job now comes with a sample monit configbased on this question.

更新:delayed_job 现在带有一个基于这个问题的示例 monit 配置

回答by Luke Francl

Here is how I got this working.

这是我如何工作的。

  1. Use the collectiveidea fork of delayed_jobbesides being actively maintained, this version has a nice script/delayed_jobdaemon you can use with monit. Railscasts has a good episodeabout this version of delayed_job(ASCIICasts version). This script also has some other nice features, like the ability to run multiple workers. I don't cover that here.
  2. Install monit. I installed from source because Ubuntu's version is so ridiculously out of date. I followed these instructionsto get the standard init.d scripts that come with the Ubuntu packages. I also needed to configure with ./configure --sysconfdir=/etc/monitso the standard Ubuntu configuration dir was picked up.
  3. Write a monit script. Here's what I came up with:

    check process delayed_job with pidfile /var/www/app/shared/pids/delayed_job.pid
    start program = "/var/www/app/current/script/delayed_job -e production start"
    stop program = "/var/www/app/current/script/delayed_job -e production stop"

    I store this in my soucre control system and point monit at it with include /var/www/app/current/config/monitin the /etc/monit/monitrcfile.

  4. Configure monit. These instructionsare laden with ads but otherwise OK.
  5. Write a task for capistrano to stop and start. monit start delayed_joband monit stop delayed_jobis what you want to run. I also reload monit when deploying to pick up any config file changes.
  1. 除了积极维护之外,使用delayed_jobcollectiveidea 分支,这个版本有一个很好的script/delayed_job守护进程,你可以与monit 一起使用。Railscasts 有一个关于这个版本delayed_jobASCIICasts version的好剧集。这个脚本还有一些其他不错的功能,比如运行多个工作程序的能力。我不在这里介绍。
  2. 安装监视器。我是从源代码安装的,因为 Ubuntu 的版本已经过时了。我按照这些说明获取了 Ubuntu 软件包附带的标准 init.d 脚本。我还需要配置,./configure --sysconfdir=/etc/monit以便选择标准的 Ubuntu 配置目录。
  3. 编写监控脚本。这是我想出的:

    check process delayed_job with pidfile /var/www/app/shared/pids/delayed_job.pid
    start program = "/var/www/app/current/script/delayed_job -e production start"
    stop program = "/var/www/app/current/script/delayed_job -e production stop"

    我将它存储在我的源控制系统中,并include /var/www/app/current/config/monit/etc/monit/monitrc文件中指向它。

  4. 配置监控。这些说明充满了广告,但其他方面还可以。
  5. 为 capistrano 编写一个任务来停止和启动。monit start delayed_job并且monit stop delayed_job是您想要运行的。我还在部署时重新加载 monit 以获取任何配置文件更改。

Problems I ran into:

我遇到的问题:

  1. daemonsgem must be installed for script/delayed_jobto run.
  2. You must pass the Rails environment to script/delayed_jobwith -e production(for example). This is documented in the README file but not in the script's help output.
  3. I use Ruby Enterprise Edition, so I needed to get monit to start with that copy of Ruby. Because of the way sudo handles the PATHin Ubuntu, I ended up symlinking /usr/bin/rubyand /usr/bin/gemto the REE versions.
  1. daemons必须安装 gem 才能script/delayed_job运行。
  2. 您必须将 Rails 环境传递给script/delayed_jobwith -e production(例如)。这记录在 README 文件中,但没有记录在脚本的帮助输出中。
  3. 我使用 Ruby 企业版,所以我需要让 monit 开始使用 Ruby 的副本。由于sudo在 Ubuntu 中处理 PATH的方式,我最终进行了符号链接 /usr/bin/ruby/usr/bin/gemREE 版本。

When debugging monit, I found it helps to stop the init.d version and run it from the th command line, so you can get error messages. Otherwise it is very difficult to figure out why things are going wrong.

在调试 monit 时,我发现它有助于停止 init.d 版本并从 th 命令行运行它,因此您可以获得错误消息。否则很难弄清楚为什么会出错。

sudo /etc/init.d/monit stop
sudo monit start delayed_job

Hopefully this helps the next person who wants to monitor delayed_jobwith monit.

希望这可以帮助下一个想要delayed_job使用 monit进行监控的人。

回答by mrchucho

For what it's worth, you can always use /usr/bin/env with monit to setup the environment. This is especially important in the current version of delayed_job, 1.8.4, where the environment (-e) option is deprecated.

无论如何,您始终可以将 /usr/bin/env 与 monit 一起使用来设置环境。这在当前版本的delayed_job 1.8.4 中尤其重要,其中环境(-e) 选项已弃用。

check process delayed_job with pidfile /var/app/shared/pids/delayed_job.pid
start program = "/usr/bin/env RAILS_ENV=production /var/app/current/script/delayed_job start"
stop  program = "/usr/bin/env RAILS_ENV=production /var/app/current/script/delayed_job stop"

In some cases, you may also need to set the PATH with env, too.

在某些情况下,您可能还需要使用 env 设置 PATH。

回答by Jason Green

I found it was easier to create an init script for delayed job. It is available here: http://gist.github.com/408929or below:

我发现为延迟作业创建初始化脚本更容易。可在此处获得:http: //gist.github.com/408929或以下:

#! /bin/sh
set_path="cd /home/rails/evatool_staging/current"

case "" in
  start)
        echo -n "Starting delayed_job: "
                su - rails -c "$set_path; RAILS_ENV=staging script/delayed_job start" >> /var/log/delayed_job.log 2>&1
        echo "done."
        ;;
  stop)
        echo -n "Stopping sphinx: "
                su - rails -c "$set_path; RAILS_ENV=staging script/delayed_job stop" >> /var/log/delayed_job.log 2>&1
        echo "done."
        ;;
      *)
            N=/etc/init.d/delayed_job_staging
            echo "Usage: $N {start|stop}" >&2
            exit 1
            ;;
    esac

    exit 0

Then make sure that monit is set to start / restart the app so in your monitrc file:

然后确保在您的 monitrc 文件中将 monit 设置为启动/重新启动应用程序:

check process delayed_job with pidfile "/path_to_my_rails_app/shared/pids/delayed_job.pid"
start program = "/etc/init.d/delayed_job start"
stop program = "/etc/init.d/delayed_job stop"

and that works great!

效果很好!

回答by Laurynas

I found a nice way to start delayed_job with cron on boot. I'm using wheneverto control cron.

我找到了一种在启动时使用 cron 启动 delay_job 的好方法。我正在使用when来控制cron。

My schedule.rb:

我的日程安排.rb:

# custom job type to control delayed_job
job_type :delayed_job, 'cd :path;RAILS_ENV=:environment script/delayed_job ":task"'

# delayed job start on boot
every :reboot do
  delayed_job "start"
end

Note: I upgraded whenever gem to 0.5.0 version to be able to use job_type

注意:我将 gem 升级到 0.5.0 版本以便能够使用 job_type

回答by Lev Lukomsky

If your monit is running as rootand you want to run delayed_job as my_userthen do this:

如果您的 monit 以root身份运行,并且您想以my_user身份运行delay_job,请执行以下操作:

/etc/init.d/delayed_job:

/etc/init.d/delayed_job

#!/bin/sh
#   chmod 755 /etc/init.d/delayed_job
#   chown root:root /etc/init.d/delayed_job

case "" in
  start|stop|restart)
    DJ_CMD=
    ;;
  *)
    echo "Usage: 
check process delayed_job with pidfile /var/www/my_app/shared/tmp/pids/delayed_job.pid
start program = "/etc/init.d/delayed_job start"
stop  program = "/etc/init.d/delayed_job stop"
if 5 restarts within 5 cycles then timeout
{start|stop|restart}" exit esac su -c "cd /var/www/my_app/current && /usr/bin/env bin/delayed_job $DJ_CMD" - my_user

/var/www/my_app/shared/monit/delayed_job.monitrc:

/var/www/my_app/shared/monit/delayed_job.monitrc

# add at bottom
include /var/www/my_app/shared/monit/*

/etc/monit/monitrc:

/etc/monit/monitrc

/bin:/usr/bin:/sbin:/usr/sbin

回答by Julian H

Thanks for the script.

谢谢你的剧本。

One gotcha -- since monit by definition has a 'spartan path'of

有一个问题-因为monit的定义有一个“斯巴达路径”

check process delayed_job with pidfile /var/www/app/shared/pids/delayed_job.pid
start program = "/usr/bin/env PATH=$PATH:/usr/local/bin /var/www/app/current/script/delayed_job -e production start"
stop program = "/usr/bin/env PATH=$PATH:/usr/local/bin /var/www/app/current/script/delayed_job -e production stop"

... and for me ruby was installed / linked in /usr/local/bin, I had to thrash around for hours trying to figure out why monit was silently failing when trying to restart delayed_job (even with -v for monit verbose mode).

...对我来说,ruby 安装/链接在 /usr/local/bin 中,我不得不折腾了几个小时试图找出为什么 monit 在尝试重新启动 delay_job 时默默失败(即使使用 -v 进行 monit 详细模式) .

In the end I had to do this:

最后我不得不这样做:

check process delayed_job
  with pidfile /var/app/shared/pids/delayed_job.pid
  start program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /var/app/current/script/delayed_job start' - rails"
  stop program = "/bin/su -c '/usr/bin/env RAILS_ENV=production /var/app/current/script/delayed_job stop' - rails"

回答by xiplias

I had to combine the solutions on this page with another scriptmade by toby to make it work with monit and starting with the right user.

我必须将这个页面上的解决方案与另一个由 toby 制作的脚本结合起来,使其与 monit 一起工作并从正确的用户开始。

So my delayed_job.monitrc looks like this:

所以我的 delay_job.monitrc 看起来像这样:

start() {
    echo "Starting $PROGNAME"
    sudo -u $USER /usr/bin/env HOME=$HOME RAILS_ENV=$RAILS_ENV $PROGNAME start
}

stop() {
    echo "Stopping $PROGNAME"
    sudo -u $USER /usr/bin/env HOME=$HOME RAILS_ENV=$RAILS_ENV $PROGNAME stop
}

回答by Helder S Ribeiro

I don't know with Monit, but I've written a couple Munin pluginsto monitor Queue Size and Average Job Run Time. The changes I made to delayed_job in that patch might also make it easier for you to write Monit plugins in case you stick with that.

我不知道 Monit,但我写了几个 Munin 插件来监控队列大小和平均作业运行时间。我在该补丁中对 delay_job 所做的更改也可能使您更容易编写 Monit 插件,以防您坚持使用。

回答by Ben Marini

Since i didn't want to run as root, I ended up creating a bash init script that monit used for starting and stopping (PROGNAME would be the absolute path to script/delayed_job):

由于我不想以 root 身份运行,因此我最终创建了一个 bash init 脚本,该脚本用于启动和停止(PROGNAME 将是脚本/delayed_job 的绝对路径):

##代码##

回答by Kenny Johnston

I have spent quite a bit of time on this topic. I was fed up with not having a good solution for it so I wrote the delayed_job_tracer plugin that specifically addresses monitoring of delayed_job and its jobs.

我在这个话题上花了很多时间。我厌倦了没有一个好的解决方案,所以我编写了delayed_job_tracer插件,专门解决delayed_job及其作业的监控问题。

Here's is an article I've written about it: http://modernagility.com/articles/5-monitoring-delayed_job-and-its-jobs

这是我写的一篇文章:http: //modernagility.com/articles/5-monitoring-delayed_job-and-its-jobs

This plugin will monitor your delayed job process and send you an e-mail if delayed_job crashes or if one of its jobs fail.

此插件将监控您的延迟作业过程,并在 delay_job 崩溃或其中一项作业失败时向您发送电子邮件。