require 'monitor'

module Rpush
  module Daemon
    class InterruptibleSleep
      def sleep(duration)
        @thread = Thread.new { Kernel.sleep duration }
        Thread.pass
        @thread.join
      end

      def stop
        @thread.kill if @thread
      rescue StandardError # rubocop:disable Lint/HandleExceptions
      end
    end
  end
end

module Rpush
  module Daemon
    class InterruptibleSleep
      def sleep(duration)
        @thread = Thread.new { Kernel.sleep duration }
        Thread.pass

        begin
          @thread.join
        rescue StandardError # rubocop:disable Lint/HandleExceptions
          @thread = nil
        end
      end

      def stop
        @thread.kill if @thread
      rescue StandardError # rubocop:disable Lint/HandleExceptions
      ensure
        @thread = nil
      end
    end
  end
end

Handle exceptions when joining sleep thread in InterruptibleSleep.
Handle exceptions when joining sleep thread in InterruptibleSleep.
