如果我的rake任务没有运行,我只想运行我的mailer
。如果我同时运行api会受到限制。
所以我试图在我的OrderMailer
上的方法中定义一个变量,只是在该变量上查看true
值,但我似乎无法让它工作。
到目前为止这是我的代码:
class OrderMailer < ActionMailer::Base
default from: '[email protected]'
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: "[email protected]",
subject: "FBA Shipping Report for #{time.strftime("%B %d")}").attachments["problem_orders_#{shop.id}.xlsx"] = File.read("problem_orders_#{shop.id}.xlsx")
# mail(to: "[email protected]", subject: "FBA Shipping Report for #{time.strftime("%B %d")}")
end
end
end
def self.is_amazon_api_available
@amazon_api_is_free
end
然后我的示例rake任务:
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
除非您从与邮件程序相同的Ruby机器内执行rake
(我怀疑),即使您尝试使用全局变量,它们也不共享环境(设置局部变量在任何情况下都没有任何影响,甚至在rake
的最后两个方法中,这些是不同的变量。)
你需要的是编写.pid
文件并检查它是否存在于文件系统中,或使用任何其他外部事实来源,如redis
,甚至DB
。