在 Rails 中更改守护进程的频率

Change frequency of daemon in Rails

每天,我的 Rails 2.3.16 网络应用程序都会检查许多外部网站的数据,以确定这些网站上的链接是否仍然有效或是否为 404。我每天收到很多电子邮件,并且我想更改此过程的频率 运行,这样我就不会那么频繁地收到那么多电子邮件。

\web\current\lib\daemons 中有一个文件 domain_checker.rb。但是,我在文件中看不到任何说明守护进程何时 运行 的内容。似乎每天早上 06:55:00 +0000 运行。有没有办法改变频率?我应该从哪里开始看?抱歉,我在 Rails 方面还很业余,正在摸索。

这里是 domain_checker.rb 文件中经过清理的代码以供参考。编辑:我添加了我认为表明 ActiveMailer 正在发送这些电子邮件的代码。

#!/usr/bin/env ruby

#You might want to change this
ENV["RAILS_ENV"] ||= "production"

require File.dirname(__FILE__) + "/../../config/environment"
require 'net/http'
require 'uri'
# require "system_timer"


class Net::HTTP
  alias_method :old_initialize, :initialize
  def initialize(*args)
    old_initialize(*args)
    @ssl_context = OpenSSL::SSL::SSLContext.new
    @ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end

def get_url(uri_str,keywords, limit=10)
  raise ArgumentError, 'HTTP redirect too deep' if limit == 0

  url = URI.parse(uri_str)

  http.read_timeout = 10
  http.open_timeout = 10
  response = nil

  begin
  SANITIZED 
  end
  SANITIZED
  if keywords == nil or keywords == ""
    case response.code
    when /^2|3\d{2}/ then true
      #when Net::HTTPSuccess     then true
      #when Net::HTTPRedirection then true
      #when Net::HTTPFound       then true
      #when Net::HTTPRedirection then get_url(response['location'], limit - 1) 
    else
      response.error!
    end
  else
    if response.code =~ /^2|3\d{2}/ and response.body.include? keywords 
      true
      #when Net::HTTPSuccess     then true
      #when Net::HTTPRedirection then true
      #when Net::HTTPFound       then true
    elsif response.code == Net::HTTPRedirection then get_url(response['location'], limit - 1) 
    else
      @response_pass = nil
      response.error!
    end
  end
rescue Timeout::Error
  return false, "Timeout"
rescue TimeoutError
  return false, "Timeout"
rescue Exception
  if response != nil
    if not @response_pass =~ /^2|3\d{2}/  #check for errant 200/300s and get rid of them...
      return false, @response_pass, response['location'], response.body  #return response error code if it isn't 2xx-3xx
    else
      return false, nil
    end
  else
    return false, nil
  end
end


@lists = List.find(:all)
for list in @lists
  SANTIZED
if (@result == false) then
  # ActiveRecord::Base.logger.info("#{task.list.id}, #{task.list.name}, #{code}, #{url}.\n")  
  UserMailer.deliver_emaildown(task, "Business Website", code, url)
end
  end

  ActiveRecord::Base.logger.info("The site checker is still running at #{Time.now}.\n")

end

ActiveRecord::Base.logger.info("The site checker is done at #{Time.now}.\n")

此代码不处理日程安排。调度通常委托给 gem 或类似 cron 的人。