Rails Daemon 保持开发模式

发布于 2024-07-15 11:06:22 字数 1005 浏览 8 评论 0原文

我有一个带有守护进程的 Rails 应用程序,用于检查邮箱中是否有新电子邮件。 我正在使用 Fetcher 插件来完成此任务。 守护程序文件如下所示:

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

所以我让它抓取新电子邮件,解析它们并从解析的数据创建资源。 但是当它尝试保存资源时,会引发异常。 这是因为脚本是自动分配开发环境的。 所以它使用我的开发数据库配置而不是生产环境(这是我想要的配置)。

我尝试用以下命令启动脚本:

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

但无济于事。 似乎当我加载environment.rb文件时,它只是默认为开发环境并从database.yml加载development.rb和开发数据库配置。

想法? 建议?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烏雲後面有陽光 2024-07-22 11:06:22

这在我的应用程序中有效,我看到的唯一区别是没有分号

RAILS_ENV=production script/mail_fetcher start

This is working in my app, the only difference I see is no semi-colon

RAILS_ENV=production script/mail_fetcher start
你没皮卡萌 2024-07-22 11:06:22

所以当你说

RAILS_ENV=production; script/mail_fetcher start

你的意思是

#!/bin/bash
export RAILS_ENV=production
cd /path/to/rails_root
./script/mail_fetcher start

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
娇女薄笑 2024-07-22 11:06:22

您可以尝试将其添加到脚本中:

ENV['RAILS_ENV'] = "production"

或者,将其添加到命令行也可能有效。

#!/bin/bash
cd /path/to/rails_root
./script/mail_fetcher start RAILS_ENV=production

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文