1

我有一个用户可以互相下注的应用程序,当结果加载到应用程序中时,我使用 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
4

2 回答 2

1

只需将我对原始问题的评论放入答案即可。

我有类似的情况,我有一个 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-20T19:40:21.010 回答
0

如果您希望在更新(或保存)时计算结果,为什么不将大部分代码从您的 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变量中)。另外,我情不自禁,但我认为你的意思是“失去”,而不是“松散”。

这是你要找的吗?

于 2012-11-20T18:12:29.360 回答