require 'delayed_job' require 'delayed_job_active_record' # # defining a hook on the enqueue module ActiveJob module QueueAdapters class DelayedJobAdapter class JobWrapper def max_attempts 3 end def enqueue args args.account_id = YAML::load( args.handler).job_data["arguments"][1]["account_id"] end # def before(job) # Rails.logger.info "-------------> before" # end # # def after(job) # Rails.logger.info "-------------> after" # end def success(job) jd = YAML::load( job.handler).job_data Rails.logger.info "OXEN (%s) executed successfully by DelayedJobAdapter on ActiveJob! Job was %s" % [jd['job_class'], jd['arguments'][0]["_aj_globalid"]] end # If delayed_job runs into errors or exceptions it will hook/callback to the error method. # cycle_error will be called and the state of the PrintJob will be changed. def error(job, exception) begin jd = YAML::load( job.handler).job_data Rails.logger.error "OXEN ERROR (%s) executed unsuccessfully by DelayedJobAdapter on ActiveJob! Message is: #{exception.message}" % jd['job_class'] parms=jd['arguments'][0]['_aj_globalid'].split("/") parms[-2].constantize.find(parms[-1]).update_column( :state, "job encounters an exception - #{exception.message}") rescue # logger.error("[OXEN] #{Time.now} [error] %s %s" % [I18n.oxt('print_job_error'),exception.message]) Rails.logger.error "OXEN ERROR (unknown) executed unsuccessfully by DelayedJobAdapter on ActiveJob! Message is: #{exception.message}" end end # If delayed_job has used up all its attempts and is unable to render and print the job, the PrintJob will fail and delayed_job will hook/callback to the method failure # Look also in delayed_job_config.rb to see the configuration for delayed_job. # Failed jobs will not be deleted, which is normally the default configuration. def failure(job) begin jd = YAML::load( job.handler).job_data Rails.logger.error "OXEN ERROR FAILURE (%s) executed unsuccessfully by DelayedJobAdapter on ActiveJob and gave up! Message is: #{exception.message}" % jd['job_class'] parms=jd['arguments'][0]['_aj_globalid'].split("/") parms[-2].constantize.find(parms[-1]).update_column( :state, "job encounters an exception - #{exception.message}") rescue # logger.error("[OXEN] #{Time.now} [error] %s %s" % [I18n.oxt('print_job_error'),exception.message]) Rails.logger.error "OXEN ERROR FAILURE (unknown) executed unsuccessfully by DelayedJobAdapter on ActiveJob and gave up! Message is: #{exception.message}" end #Airbrake.notify(exception) # cycle_error #logger.debug "Printeren svarer ikke!" end end end end end # # class OxJob < ActiveJob::Base queue_as :default # at least implement this method on your job classes inherited from OxJob # def perform raise "You need to implement perform on your job class!" end # hooks # before_enqueue do |job| job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.enqueueing") job.arguments << {account_id: job.arguments.first.account_id} end # # around_enqueue after_enqueue do |job| job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.enqueued") end # before_perform do |job| job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.prepare_to_perform") end # # around_perform do |job| # # end after_perform do |job| job.arguments.first.update_column :state, I18n.t( "#{job.arguments.first.class.to_s.underscore.pluralize}.done_performing") end # end