module Qujo module Concerns module Common extend ActiveSupport::Concern included do extend ClassMethods attr_accessor :model, :parent def run #self.load_model #self.load_parent self.status = :working self.save! self.work self.status = :complete self.save! self.accept rescue => e error "JOB#RUN: error running job, attempting to save" error " #{e.message} at #{e.backtrace.first}" self.status = :error self.message = e.message self.trace = e.backtrace self.save! end # wait while return value from block is true def wait(options = {}, &block) return unless block_given? o = {interval: 3, maximum: 600}.merge(options) interval = o[:interval] maximum = o[:maximum] count = 0 while ((count * interval) < maximum) && yield do sleep interval count += 1 end raise "wait timeout count=#{count} interval=#{interval} maximum=#{maximum}" if ((count * interval) >= maximum) end #def model # @model ||= begin # if data && data["id"] && data["class"] # begin # c = data["class"].constantize # c.find(data["id"]) # rescue nil #TODO: make this smarter # #rescue Mongoid::Errors::DocumentNotFound => e # # logger.error "document not found" # # nil # end # end # end #end # #def parent # @parent ||= begin # if data["parent"] && data["parent"]["type"] && data["parent"]["id"] # begin # c = data["parent"]["type"].constantize # c.find(data["parent"]["id"]) # #rescue Mongoid::Errors::DocumentNotFound => e # # logger.error "document not found" # # nil # end # end # end #end def model @model ||= begin if data && data["model"] && data["model"]["id"] && data["model"]["class"] i = data["model"]["id"] c = data["model"]["class"].constantize c.find(i) end rescue => e logger.error "could not load model: #{e.message} at #{e.backtrace.first}" nil end end def parent @parent ||= begin if data && data["parent"] && data["parent"]["id"] && data["parent"]["class"] i = data["parent"]["id"] c = data["parent"]["class"].constantize c.find(i) end rescue => e logger.error "could not load parent: #{e.message} at #{e.backtrace.first}" nil end end end module ClassMethods def errors? where(status: :error).count > 0 end def inherited(child) child.instance_eval do def model_name ::Job.model_name end end super end end end end end