Search code examples
ruby-on-railsruby-on-rails-3daemonruby-on-rails-2

Change frequency of daemon in Rails


Every day, my Rails 2.3.16 web app checks data on many external websites to determine whether the links on these websites are still active or if they are 404. I receive many emails a day, and I'd like to change the frequency with which this process runs so that I don't get as many emails so frequently.

There is a file, domain_checker.rb, that lives in \web\current\lib\daemons. I can't see anything in the file that says when the daemon will run, however. It seems to run every morning at 06:55:00 +0000. Is there a way to change the frequency? Where should I start by looking? Sorry, I'm pretty amateurish at Rails and am fumbling my way through.

Here is the sanitized code from the domain_checker.rb file for reference. EDIT: I added code that I think suggests that ActiveMailer is sending these emails.

#!/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")

Solution

  • This code doesn't handle scheduling. Scheduling is typically delegated to a gem or something like cron.