lib/rocket_job/job.rb in rocketjob-1.1.3 vs lib/rocket_job/job.rb in rocketjob-1.2.0

- old
+ new

@@ -221,10 +221,16 @@ # Returns the number of required arguments for this job def self.argument_count(method = :perform) instance_method(method).arity end + # Override parent defaults + def self.rocket_job(&block) + @rocket_job_defaults = block + self + end + # Returns [true|false] whether to collect the results from running this batch def collect_output? collect_output == true end @@ -311,18 +317,10 @@ raise(MongoMapper::DocumentNotFound, "Document match #{_id.inspect} does not exist in #{collection.name} collection") end end end - # After this model is read, convert any hashes in the arguments list to HashWithIndifferentAccess - def load_from_database(*args) - super - if arguments.present? - self.arguments = arguments.collect { |i| i.is_a?(BSON::OrderedHash) ? i.with_indifferent_access : i } - end - end - # Set exception information for this job and fail it def fail(worker_name='user', exc_or_message='Job failed through user action') if exc_or_message.is_a?(Exception) self.exception = JobException.from_exception(exc_or_message) exception.worker_name = worker_name @@ -358,11 +356,10 @@ # not available as #super aasm.current_event = :requeue aasm_fire_event(:requeue, persist: false) end - ############################################################################ protected # Before events that can be overridden by child classes def before_start self.started_at = Time.now @@ -380,11 +377,11 @@ self.failure_count += 1 end def before_retry self.completed_at = nil - self.exception = nil + self.exception = nil end def before_pause self.completed_at = Time.now self.worker_name = nil @@ -400,9 +397,30 @@ end def before_requeue self.started_at = nil self.worker_name = nil + end + + private + + # After this model is loaded, convert any hashes in the arguments list to HashWithIndifferentAccess + def load_from_database(*args) + super + if arguments.present? + self.arguments = arguments.collect { |i| i.is_a?(BSON::OrderedHash) ? i.with_indifferent_access : i } + end + end + + def self.apply_defaults(job) + @rocket_job_defaults.call(job) if @rocket_job_defaults + end + + # Apply RocketJob defaults after initializing default values + # but before setting attributes + def initialize_default_values(except = {}) + super + self.class.apply_defaults(self) end end end