Sha256: 75e93aac82b4aaa5d32b462c453a2c868400c3c1316cb2662f3c91084cfe6ecf

Contents?: true

Size: 993 Bytes

Versions: 1

Compression:

Stored size: 993 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

1 entries across 1 versions & 1 rubygems

Version Path
worker_killer-1.0.1.83603 lib/worker_killer/delayed_job_plugin.rb