Sha256: 14044ff927825e73d7df7d8fc475df20a7105540b1aebc8217050a3e2bc24081

Contents?: true

Size: 1.71 KB

Versions: 7

Compression:

Stored size: 1.71 KB

Contents

module RailsPerformance
  module Gems
    class DelayedJobExt
      class Plugin < ::Delayed::Plugin
        callbacks do |lifecycle|
          lifecycle.around(:invoke_job) do |job, *args, &block|
            now = Time.current
            block.call(job, *args)
            status = "success"
          rescue Exception => error # rubocop:disable Lint/RescueException
            status = "error"
            raise error
          ensure
            meta_data = RailsPerformance::Gems::DelayedJobExt::Plugin.meta(job.payload_object)
            record = RailsPerformance::Models::DelayedJobRecord.new(
              jid: job.id,
              duration: (Time.current - now) * 1000,
              datetime: now.strftime(RailsPerformance::FORMAT),
              datetimei: now.to_i,
              source_type: meta_data[0],
              class_name: meta_data[1],
              method_name: meta_data[2],
              status: status
            )
            record.save
          end
        end

        # [source_type, class_name, method_name, duration]
        def self.meta(payload_object)
          if payload_object.is_a?(::Delayed::PerformableMethod)
            if payload_object.object.is_a?(Module)
              [:class_method, payload_object.object.name, payload_object.method_name.to_s]
            else
              [:instance_method, payload_object.object.class.name, payload_object.method_name.to_s]
            end
          else
            [:instance_method, payload_object.class.name, "perform"]
          end
        rescue
          [:unknown, :unknown, :unknown]
        end
      end

      def self.init
        ::Delayed::Worker.plugins += [::RailsPerformance::Gems::DelayedJobExt::Plugin]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_performance-1.4.0.alpha3 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.4.0.alpha2 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.4.0.alpha1 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.3.3 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.3.2 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.3.1 lib/rails_performance/gems/delayed_job_ext.rb
rails_performance-1.3.0 lib/rails_performance/gems/delayed_job_ext.rb