Module: Puma::SdNotify
- Defined in:
- lib/puma/sd_notify.rb
Overview
The MIT License
Copyright (c) 2017-2022 Agis Anastasopoulos
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This is a copy of https://github.com/agis/ruby-sdnotify as of commit cca575c The only changes made was "rehoming" it within the Puma module to avoid namespace collisions and applying standard's code formatting style.
SdNotify is a pure-Ruby implementation of sd_notify(3). It can be used to notify systemd about state changes. Methods of this package are no-op on non-systemd systems (eg. Darwin).
The API maps closely to the original implementation of sd_notify(3), therefore be sure to check the official man pages prior to using SdNotify.
Defined Under Namespace
Classes: NotifyError
Constant Summary collapse
- READY =
"READY=1"- RELOADING =
"RELOADING=1"- STOPPING =
"STOPPING=1"- STATUS =
"STATUS="- ERRNO =
"ERRNO="- MAINPID =
"MAINPID="- WATCHDOG =
"WATCHDOG=1"- FDSTORE =
"FDSTORE=1"
Class Method Summary collapse
- .errno(errno, unset_env = false) ⇒ Object
- .fdstore(unset_env = false) ⇒ Object
- .mainpid(pid, unset_env = false) ⇒ Object
-
.notify(state, unset_env = false) ⇒ Fixnum?
Notify systemd with the provided state, via the notification socket, if any.
- .ready(unset_env = false) ⇒ Object
- .reloading(unset_env = false) ⇒ Object
- .status(status, unset_env = false) ⇒ Object
- .stopping(unset_env = false) ⇒ Object
- .watchdog(unset_env = false) ⇒ Object
-
.watchdog? ⇒ Boolean
If the $WATCHDOG_USEC environment variable is set, and the $WATCHDOG_PID variable is unset or set to the PID of the current process.
Class Method Details
.errno(errno, unset_env = false) ⇒ Object
74 75 76 |
# File 'lib/puma/sd_notify.rb', line 74 def self.errno(errno, unset_env=false) notify("#{ERRNO}#{errno}", unset_env) end |
.fdstore(unset_env = false) ⇒ Object
87 88 89 |
# File 'lib/puma/sd_notify.rb', line 87 def self.fdstore(unset_env=false) notify(FDSTORE, unset_env) end |
.mainpid(pid, unset_env = false) ⇒ Object
79 80 81 |
# File 'lib/puma/sd_notify.rb', line 79 def self.mainpid(pid, unset_env=false) notify("#{MAINPID}#{pid}", unset_env) end |
.notify(state, unset_env = false) ⇒ Fixnum?
Notify systemd with the provided state, via the notification socket, if any.
Generally this method will be used indirectly through the other methods of the library.
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/puma/sd_notify.rb', line 135 def self.notify(state, unset_env=false) sock = ENV["NOTIFY_SOCKET"] return nil if !sock ENV.delete("NOTIFY_SOCKET") if unset_env begin Addrinfo.unix(sock, :DGRAM).connect { |s| s.write state } rescue StandardError => e raise NotifyError, "#{e.class}: #{e.}", e.backtrace end end |
.ready(unset_env = false) ⇒ Object
When the service manager expects watchdog pings, this sends an
immediate WATCHDOG=1 in the same datagram, which closes the watchdog
timeout window between startup and the first periodic watchdog tick.
55 56 57 |
# File 'lib/puma/sd_notify.rb', line 55 def self.ready(unset_env=false) notify(watchdog? ? "#{READY}\n#{WATCHDOG}" : READY, unset_env) end |
.reloading(unset_env = false) ⇒ Object
59 60 61 |
# File 'lib/puma/sd_notify.rb', line 59 def self.reloading(unset_env=false) notify(RELOADING, unset_env) end |
.status(status, unset_env = false) ⇒ Object
69 70 71 |
# File 'lib/puma/sd_notify.rb', line 69 def self.status(status, unset_env=false) notify("#{STATUS}#{status}", unset_env) end |
.stopping(unset_env = false) ⇒ Object
63 64 65 |
# File 'lib/puma/sd_notify.rb', line 63 def self.stopping(unset_env=false) notify(STOPPING, unset_env) end |
.watchdog(unset_env = false) ⇒ Object
83 84 85 |
# File 'lib/puma/sd_notify.rb', line 83 def self.watchdog(unset_env=false) notify(WATCHDOG, unset_env) end |
.watchdog? ⇒ Boolean
Unlike sd_watchdog_enabled(3), this method does not mutate the environment.
If the $WATCHDOG_USEC environment variable is set, and the $WATCHDOG_PID variable is unset or set to the PID of the current process
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puma/sd_notify.rb', line 100 def self.watchdog? wd_usec = ENV["WATCHDOG_USEC"] wd_pid = ENV["WATCHDOG_PID"] return false if !wd_usec begin wd_usec = Integer(wd_usec) rescue return false end return false if wd_usec <= 0 return true if !wd_pid || wd_pid == $$.to_s false end |