仅在瑞克任务完成后才运行邮件程序

时间:2019-03-13 14:34:56

标签: ruby-on-rails ruby rake actionmailer rake-task

如果我的rake任务未运行,我只想运行mailer。如果我同时运行这两个api,则会受到限制。

因此,我试图在我的OrderMailer的方法中定义一个变量,只注意该变量的true值,但我似乎无法使其正常工作。

到目前为止,这是我的代码:

class OrderMailer < ActionMailer::Base
  default from: 'todd@gmail.com'
  attr_reader :sheet
  attr_accessor :amazon_api_is_free

def order_report
    Rails.logger.info"Is the api free? #{amazon_api_is_free}"
    sleep(30) until amazon_api_is_free
    shops = Shop.where(subscribed: true, setup: true)
    shops.each do |shop|
      @sheet = OrderReport.new(shop)
      @sheet.create_order_workbook
      # get_data_for_inside_email
      time = Time.now
      if File.exist?("problem_orders_#{shop.id}.xlsx")
        mail(to: "todd@gmail.com", 
        subject: "FBA Shipping Report for #{time.strftime("%B %d")}").attachments["problem_orders_#{shop.id}.xlsx"] = File.read("problem_orders_#{shop.id}.xlsx")
        # mail(to: "todd.trimakas@gmail.com", subject: "FBA Shipping Report for #{time.strftime("%B %d")}")
      end
    end
  end

  def self.is_amazon_api_available
    @amazon_api_is_free
  end

然后是我的示例耙任务:

desc 'Passing the buck'
task :just_a_test => :environment do
  puts "testing"
  start
end

  def start
    not_available
    pp "From the rake is the api free? #{OrderMailer.is_amazon_api_available}"
    pp "making sure its running"
    sleep(20)
    available
  end

  def not_available
    is_amazon_api_available = false
  end

  def available
    is_amazon_api_available = true
  end

1 个答案:

答案 0 :(得分:2)

除非您在与邮件程序相同的Ruby机器内执行rake(我怀疑,),即使您试图使用全局变量(设置局部变量的效果为零),它们也不会共享环境。无论如何,即使在rake中的最后两个方法中,它们都是不同的变量。)

您需要编写.pid文件并检查其在文件系统中是否存在,或者使用其他外部真实数据源,例如redis甚至是DB