Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created June 10, 2012 22:29
Show Gist options
  • Save krisleech/2907540 to your computer and use it in GitHub Desktop.
Save krisleech/2907540 to your computer and use it in GitHub Desktop.
class SignificantDates < Job
def model_name
'Study'
end
def options
[
{ :name => :valid_submission, :days => 60.days },
{ :name => :kpi_end_date, :days => 1.month }
]
end
def condition(study, option)
date = study.significant_date(option[:name])
date.present? && date < Time.now - option[:days]
end
def action(failures)
send_email(:to => study.person_with_capacity(:officer), :template => 'alfresco://significant_dates', :failures => failures)
end
end
task :run => [:environment] do
SignificantDates.run!
MissingData.run!
end
class Job < ActiveRecord::Base
def run!
failures = []
model_class.all.each do | record |
options.each do |option|
failures << { :record => record, :option => option } unless condition(record, option)
end
action(failures) unless failures.empty?
end
end
private
def options
[]
end
def conditions(study, option)
raise NotImplementedError
end
def action(failures)
raise NotImplementedError
end
def model_name
raise NotImplementedError
end
def model_class
model_name.constantize
end
end
# We use active record to store when a job is run so we dont over send emails
# and to log any errors. We could even use it to schedule when the job should
# be next run, which can be a different time depending on if an action was
# performed or not.
#
# Omitted: The base class would also rescue any errors and log them
# jobs_table
# type (class of job)
# last_run_at
# error_log
# next_run_at
class MissingData < Job
def model_name
'Study'
end
def options
[:acrnoymn, :short_title, :ref_code]
end
def condition(study, option)
study.send(option[:field]).blank?
end
def action(failures)
send_email(:to => '[email protected]', :template => 'alfresco://missing_data', :failures => failures)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment