Sha256: af29433c87f0808bcb3ddeb8029968348c3b17dd38f7c2d737bb8129f02cc75c

Contents?: true

Size: 991 Bytes

Versions: 4

Compression:

Stored size: 991 Bytes

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)
    end

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

    def configure_lifecycle(lifecycle)
      lifecycle.after(:perform) do |worker, *_args|
        reaction.call(limiter, killer, worker) if limiter.check
      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

4 entries across 4 versions & 1 rubygems

Version Path
worker_killer-1.0.1.75538 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.1.39842 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.0.39841 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.0.39839 lib/worker_killer/delayed_job_plugin.rb