Sha256: 32815ec0da6abe2e1d73d9fe48516f48d462caf01aecd2ba41bac269f1c24e8c

Contents?: true

Size: 1.25 KB

Versions: 4

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

4 entries across 4 versions & 1 rubygems

Version Path
worker_killer-1.0.5.213977 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.5.213889 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.4.189871 lib/worker_killer/delayed_job_plugin.rb
worker_killer-1.0.3.189564 lib/worker_killer/delayed_job_plugin.rb