I'm trying to do something that seems to me quite basic but I can't figure out a way to get it DRY.
This is the code I had initially:
my first worker
class WorkerOne < BaseWorker
def perform
# do stuff
rescue *exceptions_to_rescue_with_error => error
job_error(error, try_later: false)
rescue *exceptions_to_rescue_with_error_and_try_later => error
job_error(error, try_later: true)
rescue *exceptions_to_rescue_with_warning => message
job_warning(message, try_later: false)
end
end
my second worker
class WorkerTwo < BaseWorker
def perform
# do stuff
rescue *exceptions_to_rescue_with_error => error
job_error(error, try_later: false)
rescue *exceptions_to_rescue_with_error_and_try_later => error
job_error(error, try_later: true)
rescue *exceptions_to_rescue_with_warning => message
job_warning(message, try_later: false)
end
end
the parent class
class BaseWorker
def exceptions_to_rescue_with_error
[
Exceptions::SomeOtherError,
ActiveRecord::RecordNotFound
]
end
def exceptions_to_rescue_with_error_and_try_later
[
Exceptions::SomeError1,
Exceptions::SomeError2,
]
end
def exceptions_to_rescue_with_warning
[
Exceptions::SomeWarning
]
end
end
This is the method I'm adding to BaseWorker to make it work:
def self.with_job_rescue_for_perfom
define_method(:perform) do |*args, &block|
self.perform(*args, &block)
rescue *exceptions_to_rescue_with_error => error
job_error(error, try_later: false)
rescue *exceptions_to_rescue_with_error_to_try_later => error
job_error(error, try_later: true)
rescue *exceptions_to_rescue_with_warning => message
job_warning(message, try_later: false)
end
end
end
I'm stuck there and I don't see where I should look into next. It seems the method used here does not work for me as I'm not using a module.
Any idea?