lib/rocket_job/concerns/worker.rb in rocketjob-1.1.3 vs lib/rocket_job/concerns/worker.rb in rocketjob-1.2.0

- old
+ new

@@ -4,13 +4,10 @@ module RocketJob module Concerns module Worker def self.included(base) base.extend ClassMethods - base.class_eval do - @rocket_job_defaults = nil - end end module ClassMethods # Returns [Job] after queue-ing it for processing def later(method, *args, &block) @@ -23,11 +20,17 @@ end end # Create a job and process it immediately in-line by this thread def now(method, *args, &block) - job = build(method, *args, &block) + job = build(method, *args, &block) + # Call validations + if job.respond_to?(:validate!) + job.validate! + elsif job.invalid? + raise(MongoMapper::DocumentNotValid, "Validation failed: #{job.errors.messages.join(', ')}") + end worker = RocketJob::Worker.new(name: 'inline') worker.started job.start while job.running? && !job.work(worker) end @@ -41,11 +44,10 @@ # queued for processing. # - If data is uploaded into the job instance before saving, and is then # discarded, call #cleanup! to clear out any partially uploaded data def build(method, *args, &block) job = new(arguments: args, perform_method: method.to_sym) - @rocket_job_defaults.call(job) if @rocket_job_defaults block.call(job) if block job end # Method to be performed later @@ -59,15 +61,9 @@ end # Method to be performed now def perform_now(*args, &block) now(:perform, *args, &block) - end - - # Define job defaults - def rocket_job(&block) - @rocket_job_defaults = block - self end # Returns the next job to work on in priority based order # Returns nil if there are currently no queued jobs, or processing batch jobs # with records that require processing