Sha256: 6cd949e06068385eb0402ff348b590e31c059e52c5be437729ffdbe16f21b6d5

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "sidekiq"
require_relative "job_signal/version"
require_relative "job_signal/server_middleware"
require_relative "job_signal/receiver"
require_relative "job_signal/web"

module Sidekiq
  module JobSignal
    class << self
      def handlers
        @handlers ||= []
      end

      def on_quit(&block)
        handlers << block
      end

      def quit(worker_class: "", jid: "")
        ::Sidekiq.redis do |r|
          r.pipelined do |pipeline|
            pipeline.set "jobsignal-#{jid}", "quit", ex: 86_400 if jid && !jid.empty?
            pipeline.set "jobsignal-#{worker_class}", "quit", ex: 86_400 if worker_class && !worker_class.empty?
          end
        end
      end

      def delete_signal(worker_class: "", jid: "")
        ::Sidekiq.redis do |r|
          r.pipelined do |pipeline|
            pipeline.del("jobsignal-#{jid}") if jid && !jid.empty?
            pipeline.del("jobsignal-#{worker_class}") if worker_class && !worker_class.empty?
          end
        end
      end

      def quitting?(worker_class: "", jid: "")
        results = ::Sidekiq.redis do |r|
          r.pipelined do |pipeline|
            pipeline.get("jobsignal-#{jid}") if jid && !jid.empty?
            pipeline.get("jobsignal-#{worker_class}") if worker_class && !worker_class.empty?
          end
        end
        results.include?("quit")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-job-signal-0.1.1 lib/sidekiq/job_signal.rb
sidekiq-job-signal-0.1.0 lib/sidekiq/job_signal.rb