Created
October 28, 2009 19:09
-
-
Save raldred/220723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyJob < ActiveRecord::Base | |
def bulk_task | |
start() | |
task_work() | |
@log.info("Errors missing: #{errors.inspect}") | |
finish() | |
end | |
private | |
def start | |
@log = Logger.new('/Users/rob/project/logs/job.log') | |
@log.info "Starting #{Time.now}" | |
end | |
def finish | |
@log.info("Error missing in finish: #{errors.inspect}") | |
log_errors unless errors.empty? | |
@log.info "No Errors" if errors.empty? | |
@log.info "Finished #{Time.now}" | |
end | |
def task_work | |
#do some magic... | |
# the magic fails | |
# lets add an error to the activerecord errors | |
self.failed = true # set the failed field to true | |
errors.add_to_base("The magic wasn't so magic after all!") | |
@log.info("Error added: #{errors.inspect}") | |
save! | |
end | |
def log_errors | |
# we never get here... errors is always empty, even though we set one on line 30 | |
errors.full_messages.each do |error| | |
@log.error("#{error}") | |
end | |
end | |
end | |
class MyJobsController < ApplicationController | |
def create | |
job = MyJob.create(:name => "My Job") | |
job.send_later(:bulk_task) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment