我有一个应用程序,用户可以在其中相互下注,当结果加载到应用程序中时,我使用Rake任务来结算赌注。我在Heroku上运行,所以我每半小时就会使用他们的计划服务来完成这个Rake任务。
这可以很好地工作,但我更希望在数据库中保存/更新结果时运行Rake作业。
我如何转换Rake任务,以便可以在我的模型中运行它,可能如下所示。如果我可以从控制器运行它也会很好,因为我可能会有几种需要结算过程的情况。
class Spotprice < ActiveRecord::Base
belongs_to :spotarea
belongs_to :product
after_save :settlement
end我的Rake任务现在看起来像这样:
task :settlement => :environment do
puts "Settlement in progress..."
puts "-------------------------"
puts " "
puts " "
puts "BETS:"
puts "-------------------------"
# Bet settlement
@bets = Bet.where(:settled => false)
@bets.find_each do |bet|
if not bet.choice.spotprice.nil?
case
when bet.choice.spotprice.value > bet.choice.value && bet.buy == true
profitloss = 10
puts "#{bet.id}: Win (1)"
when bet.choice.spotprice.value < bet.choice.value && bet.buy == false
profitloss = 10
puts "#{bet.id}: Win (2)"
when bet.choice.spotprice.value > bet.choice.value && bet.buy == false
profitloss = -10
puts "#{bet.id}: Loose (3)"
when bet.choice.spotprice.value < bet.choice.value && bet.buy == true
profitloss = -10
puts "#{bet.id}: Loose (4)"
when bet.choice.spotprice.value == bet.choice.value
profitloss = -10
puts "#{bet.id}: Loose (5)"
end
if profitloss
bet.settled = true
bet.profitloss = profitloss
bet.save
end
end
if bet.choice.settled == true
bet.choice.settled = false
bet.choice.save
end
end
# Pusher update
Pusher["actives"].trigger("updated", {:message => "Settlement completed"}.to_json)
end发布于 2012-11-21 03:40:21
只是把我对原始问题的评论放到一个答案中。
我有一个类似的情况,我有一个rake任务,我想从rake外部调用它。您想要做的是将代码从rake任务移到ruby类中,最好的位置是在lib中。您的类将如下所示:
# lib/settlement.rb
class Settlement
def self.settle_bets
# all the code that used to be in the rake task
end
end然后在代码中,你可以这样做(随机例子):
# app/controllers/bets_controller.rb
#...
def settle_bets
require "settlement"
Settlement.settle_bets
end
# ...或者(另一个随机的例子):
# app/models/bet.rb
class Bet ...
after_update: update_some_bets
# ... more code here
private
def update_some_bets
require "settlement"
Settlement.settle_bets
end如果你愿意,你仍然可以使用rake任务:
# lib/tasks/settlement.task
require "settlement"
# require "#{Rails.root}/lib/settlement.rb" # use this if the above won't work
task :settlement => :environment do
Settlement.settle_bets
end发布于 2012-11-21 02:12:29
如果想要在更新(或保存)时计算结果,为什么不将大部分代码从rake任务移到Bet模型的方法中,然后调用after_update回调
在bet.rb中
# class method that settles the given bet
def self.settle(bet)
message = nil
if not bet.choice.spotprice.nil?
case
when bet.choice.spotprice.value > bet.choice.value && bet.buy == true
profitloss = 10
message = "#{bet.id}: Win (1)"
when bet.choice.spotprice.value < bet.choice.value && bet.buy == false
profitloss = 10
message = "#{bet.id}: Win (2)"
when bet.choice.spotprice.value > bet.choice.value && bet.buy == false
profitloss = -10
message = "#{bet.id}: Lose (3)"
when bet.choice.spotprice.value < bet.choice.value && bet.buy == true
profitloss = -10
message = "#{bet.id}: Lose (4)"
when bet.choice.spotprice.value == bet.choice.value
profitloss = -10
message = "#{bet.id}: Lose (5)"
end
if profitloss
bet.settled = true
bet.profitloss = profitloss
bet.save
end
end
if bet.choice.settled == true
bet.choice.settled = false
bet.choice.save
end
# return message
message
end在spot_price.rb中
class SpotPrice
after_update Bet.settle(self)
after_save Bet.settle(self)
....
end这不会处理其他输出中的Pusher内容(保存在message变量中并返回)。此外,我无法控制自己,但我认为你的意思是“失败”,而不是“松散”。
这就是你要找的吗?
https://stackoverflow.com/questions/13479088
复制相似问题