Sha256: 0908d97d73a1bb59ba61c44756f80d0dee604850726d4658e02067915b62740a

Contents?: true

Size: 988 Bytes

Versions: 6

Compression:

Stored size: 988 Bytes

Contents

# frozen_string_literal: true

require 'sd_notify'

module Puma
  class Systemd
    def initialize(log_writer, events)
      @log_writer = log_writer
      @events = events
    end

    def hook_events
      @events.on_booted { SdNotify.ready }
      @events.on_stopped { SdNotify.stopping }
      @events.on_restart { SdNotify.reloading }
    end

    def start_watchdog
      return unless SdNotify.watchdog?

      ping_f = watchdog_sleep_time

      log "Pinging systemd watchdog every #{ping_f.round(1)} sec"
      Thread.new do
        loop do
          sleep ping_f
          SdNotify.watchdog
        end
      end
    end

    private

    def watchdog_sleep_time
      usec = Integer(ENV["WATCHDOG_USEC"])

      sec_f = usec / 1_000_000.0
      # "It is recommended that a daemon sends a keep-alive notification message
      # to the service manager every half of the time returned here."
      sec_f / 2
    end

    def log(str)
      @log_writer.log(str)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
puma-6.0.2-java lib/puma/systemd.rb
puma-6.0.2 lib/puma/systemd.rb
puma-6.0.1-java lib/puma/systemd.rb
puma-6.0.1 lib/puma/systemd.rb
puma-6.0.0-java lib/puma/systemd.rb
puma-6.0.0 lib/puma/systemd.rb