Monday, April 25, 2011

Rails Daemon stays in development mode

Hello! I have a Rails application with a daemon that checks a mailbox for any new emails. I am using the Fetcher plugin for this task. The daemon file looks like this:

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment.rb'

class MailFetcherDaemon < Daemon::Base

  @config = YAML.load_file("#{RAILS_ROOT}/config/mail.yml")
  @config = @config['production'].to_options

  @sleep_time = @config.delete(:sleep_time) || 20
  def self.start
    puts "Starting MailFetcherDaemon"
    # Add your own receiver object below
    @fetcher = Fetcher.create({:receiver => MailProcessor}.merge(@config))
  ...

So I have it grab the new emails, parse them and create a resource from the parsed data. But when it tries to save the resource an exception is thrown. This is because the script is automatically assigned the development environment. So it is using my development database configuration instead of the production environment (which is the config that I want).

I have tried starting the script with:

 rails-root$ RAILS_ENV=production; script/mail_fetcher start

but to no avail. It seems like when I load the environment.rb file it just defaults to the development environment and loads development.rb and the development database configuration from database.yml.

Thoughts? Suggestions?

Thanks

From stackoverflow
  • So when you say

    RAILS_ENV=production; script/mail_fetcher start
    

    do you mean

    #!/bin/bash
    export RAILS_ENV=production
    cd /path/to/rails_root
    ./script/mail_fetcher start
    
    vrish88 : Yeah that's the command I was doing but I had it in shorthand. I updated the question to indicate where I was running the command from.
  • You might try adding this to your script:

    ENV['RAILS_ENV'] = "production"
    

    Alternatively, it might work to add it to the command line.

    #!/bin/bash
    cd /path/to/rails_root
    ./script/mail_fetcher start RAILS_ENV=production
    
    vrish88 : I tried that but I need the environment to be set to 'production' as the environment.rb file is loaded this way it will load the production configuration (the settings for the database, mailier, logger, etc)
  • This is working in my app, the only difference I see is no semi-colon

    RAILS_ENV=production script/mail_fetcher start
    

0 comments:

Post a Comment