Mike Cochran
@vongrippen
http://vongrippen.com/memrb-2013-04-resque
def create
client_case.input = filtered_params
client_case.calculate
# 3 minutes later...
client.save
# Render HTML
end
def calculate
periods = 0..(periods_to_run-1)
summaries = [ ]
periods.each do |period|
# Do a bunch of fun math
years_in_period = calculate_period(input, period)
self.save_period(period, years_in_period)
summaries << years_in_period.last
end
self.summary = summarize(summaries)
end
def create
client_case.input = filtered_params
client_case.save
Resque.enqueue(CaseWorker, client_case.id)
# Render HTML
end
class CaseWorker
@queue = :cases
def self.perform(case_id)
client_case = Case.find(case_id)
client_case.calculate
client_case.save
end
end
def calculate
periods = 0..(periods_to_run-1)
summaries = [ ]
periods.each do |period|
# Do a bunch of fun math
years_in_period = calculate_period(input, period)
self.save_period(period, years_in_period)
summaries << years_in_period.last
end
self.summary = summarize(summaries)
end
def create
client_case.input = filtered_params
client_case.save
(0..client_case.periods_to_run-1).each do |period|
Resque.enqueue(PeriodWorker, case_id, period)
end
# Render HTML
end
class CaseWorker
@queue = :cases
def self.perform(case_id)
client_case = Case.find(case_id)
client_case.summary = summarize(client_case.summaries)
client_case.save
end
end
class PeriodWorker
@queue = :periods
def self.perform(case_id, period)
client_case = Case.find(case_id)
years_in_period = calculate_period(input, period)
client_case.save_period(period, years_in_period)
client_case.summaries << years_in_period.last
client_case.save
if client_case.periods == client_case.periods_in_case
Resque.enqueue(CaseWorker, case_id)
end
end
end