1


在下面的代码中,我必须发送一封关于进程状态的电子邮件,无论是完成、错误还是超时......

    def check_for_forecasts
   wait_until_time = Time.now + timeout.minutes
   loop do
       RAILS_DEFAULT_LOGGER.info "Checking if process has finished"
     if find_token != 0
       update_completion_status
       RAILS_DEFAULT_LOGGER.info "Process has finished"
       break
     elsif find_error != 0
       update_timed_out_field
       RAILS_DEFAULT_LOGGER.info "Process has errored"
       break
     elsif DateTime.now > wait_until_time
       update_timed_out_field
       RAILS_DEFAULT_LOGGER.info "Process has timed out"
       break
     else
       RAILS_DEFAULT_LOGGER.info "Waiting for Process to finish"
       sleep(60) # if it hasn't completed then wait 1 min and try again.
     end
    end
  end

一般来说,我们只在 .sh 文件而不是 .rb 文件中使用 linux 'mail' 命令。下面是我们如何编写“邮件”命令以在 .sh 文件中发送邮件。

 mail -s "the process has been finished" abc@xyz.com<<EOM
      The process has finished successfully.    
EOM

有没有办法在 .rb 文件中使用简单的邮件命令?还是我必须为此安装任何宝石?

请帮忙。谢谢你。

4

2 回答 2

2

从 ruby​​ 做同样的事情的一个非常丑陋但有用的方法:

to = "abc@xyz.com"
subject = "the process has been finished"
content = "The process has finished successfully."
`mail -s "#{subject}" #{to}<<EOM
  #{content}
EOM`
于 2013-04-04T09:21:38.187 回答
2

您可以在 Ruby 中使用反引号来运行命令行指令。所以你想要的是这样的:

`mail -s "the process has been finished" abc@xyz.com<<EOM
  The process has finished successfully.    
EOM`
于 2013-04-04T09:24:50.403 回答