[英]Run Rake Task without using Rake
我有一個應用程序,用戶可以互相下注,當結果加載到應用程序中時,我使用Rake任務來結算賭注。 我在Heroku上運行,因此每半小時使用他們的Schedule服務進行此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
只需將我對原始問題的評論放入答案中即可。
我遇到過類似的情況,我有一個耙任務,我想從耙外部調用。 您要做的是將代碼從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
如果要在更新(或保存)時計算結果,為什么不將大多數代碼從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
變量中返回)。 另外,我也無濟於事,但我想你是說“輸”而不是“輸”。
這是您要找的東西嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.