Sha256: ee8e60a1da70cfc867933b44cce22aa2327a5c5428fdf0c6707df965290add71

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'worker_killer/memory_limiter'
require 'worker_killer/count_limiter'

module WorkerKiller
  class DelayedJobPlugin

    attr_reader :limiter, :killer, :reaction

    def initialize(klass:, killer:, reaction: nil, **opts)
      @killer = killer

      @reaction = reaction || proc do |l, k, dj|
        k.kill(l.started_at, dj: dj)
      end

      @limiter = klass.new(**opts)
      @time_to_burn = false
    end

    def new(lifecycle = Delayed::Worker.lifecycle, *_args)
      configure_lifecycle(lifecycle)
    end

    def configure_lifecycle(lifecycle)
      # Count condition after every job
      lifecycle.after(:perform) do |_worker, *_args|
        @time_to_burn ||= limiter.check
      end

      # Stop execution only after whole loop completed
      lifecycle.after(:loop) do |worker, *_args|
        @time_to_burn ||= limiter.check
        reaction.call(limiter, killer, worker) if @time_to_burn
      end
    end

    class JobsLimiter < ::WorkerKiller::DelayedJobPlugin

      def initialize(**opts)
        super(klass: ::WorkerKiller::CountLimiter, **opts)
      end

    end

    class OOMLimiter < ::WorkerKiller::DelayedJobPlugin

      def initialize(**opts)
        super(klass: ::WorkerKiller::MemoryLimiter, **opts)
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
worker_killer-1.1.0.223443 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.1.0.214159 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.1.0.214146 lib/worker_killer/delayed_job_plugin.rb