sd-notify-0.1.1/0000755000175100017510000000000014155163043012455 5ustar pravipravisd-notify-0.1.1/CHANGELOG.md0000644000175100017510000000050714155163043014270 0ustar pravipravi# Changelog Breaking changes are prefixed with a "[BREAKING]" label. ## master (unreleased) ## 0.1.1 (2021-02-27) This is a release with no actual changes in the source code. It's merely added to tackle #4. ## 0.1.0 (2018-05-15) ### Added - `SdNotify.watchdog?` [[#1](https://github.com/agis/ruby-sdnotify/issues/1)] sd-notify-0.1.1/lib/0000755000175100017510000000000014155163043013223 5ustar pravipravisd-notify-0.1.1/lib/sd_notify.rb0000644000175100017510000000630314155163043015550 0ustar pravipravi# frozen_string_literal: true require "socket" # 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. # # @see https://www.freedesktop.org/software/systemd/man/sd_notify.html module SdNotify # Exception raised when there's an error writing to the notification socket class NotifyError < RuntimeError; end READY = "READY=1" RELOADING = "RELOADING=1" STOPPING = "STOPPING=1" STATUS = "STATUS=" ERRNO = "ERRNO=" MAINPID = "MAINPID=" WATCHDOG = "WATCHDOG=1" FDSTORE = "FDSTORE=1" def self.ready(unset_env=false) notify(READY, unset_env) end def self.reloading(unset_env=false) notify(RELOADING, unset_env) end def self.stopping(unset_env=false) notify(STOPPING, unset_env) end # @param status [String] a custom status string that describes the current # state of the service def self.status(status, unset_env=false) notify("#{STATUS}#{status}", unset_env) end # @param errno [Integer] def self.errno(errno, unset_env=false) notify("#{ERRNO}#{errno}", unset_env) end # @param pid [Integer] def self.mainpid(pid, unset_env=false) notify("#{MAINPID}#{pid}", unset_env) end def self.watchdog(unset_env=false) notify(WATCHDOG, unset_env) end def self.fdstore(unset_env=false) notify(FDSTORE, unset_env) end # @param [Boolean] true if the service manager expects watchdog keep-alive # notification messages to be sent from this process. # # If the $WATCHDOG_USEC environment variable is set, # and the $WATCHDOG_PID variable is unset or set to the PID of the current # process # # @note Unlike sd_watchdog_enabled(3), this method does not mutate the # environment. 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 # 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. # # @param state [String] # @param unset_env [Boolean] # # @return [Fixnum, nil] the number of bytes written to the notification # socket or nil if there was no socket to report to (eg. the program wasn't # started by systemd) # # @raise [NotifyError] if there was an error communicating with the systemd # socket # # @see https://www.freedesktop.org/software/systemd/man/sd_notify.html 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 do |s| s.close_on_exec = true s.write(state) end rescue StandardError => e raise NotifyError, "#{e.class}: #{e.message}", e.backtrace end end end sd-notify-0.1.1/README.md0000644000175100017510000000350214155163043013734 0ustar pravipravi# ruby-sdnotify [![Gem Version](https://badge.fury.io/rb/sd_notify.svg)](https://badge.fury.io/rb/sd_notify) [![Build Status](https://travis-ci.org/agis/ruby-sdnotify.svg?branch=master)](https://travis-ci.org/agis/ruby-sdnotify) [![Documentation](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/agis/ruby-sdnotify) [![License](https://img.shields.io/github/license/mashape/apistatus.svg)](LICENSE) A pure-Ruby implementation of [sd_notify(3)](https://www.freedesktop.org/software/systemd/man/sd_notify.html) that can be used to communicate state changes of Ruby programs to [systemd](https://www.freedesktop.org/wiki/Software/systemd/). Refer to the [API documentation](http://www.rubydoc.info/github/agis/ruby-sdnotify) for more info. ## Getting started Install ruby-sdnotify: ```shell $ gem install sd_notify ``` If you're using Bundler, add it to your Gemfile: ```ruby gem "sd_notify" ``` ## Usage The [API](http://www.rubydoc.info/github/agis/ruby-sdnotify) is mostly tied to the official implementation, therefore refer to the [sd_notify(3) man pages](https://www.freedesktop.org/software/systemd/man/sd_notify.html) for detailed description of how the notification mechanism works. An example involving a dummy workload (assuming the program is shipped as a systemd service): ```ruby require "sd_notify" puts "Hello! Booting..." # doing some initialization work... sleep 2 # notify systemd that we're ready SdNotify.ready sum = 0 5.times do |i| # doing our main work... sleep 1 sum += 1 # notify systemd of our progress SdNotify.status("{sum} jobs completed") end puts "Finished working. Shutting down..." # notify systemd we're shutting down SdNotify.stopping # doing some cleanup work... sleep 2 puts "Bye" ``` ## License ruby-sdnotify is licensed under MIT. See [LICENSE](LICENSE). sd-notify-0.1.1/LICENSE0000644000175100017510000000211214155163043013456 0ustar pravipraviThe MIT License Copyright (c) 2017, 2018, 2019, 2020 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. sd-notify-0.1.1/sd_notify.gemspec0000644000175100017510000000276314155163043016030 0ustar pravipravi######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: sd_notify 0.1.1 ruby lib Gem::Specification.new do |s| s.name = "sd_notify".freeze s.version = "0.1.1" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Agis Anastasopoulos".freeze] s.date = "2021-02-27" s.description = "sd_notify can be used to notify systemd about various service status changes of Ruby programs".freeze s.email = "agis.anast@gmail.com".freeze s.files = ["CHANGELOG.md".freeze, "LICENSE".freeze, "README.md".freeze, "lib/sd_notify.rb".freeze] s.homepage = "https://github.com/agis/ruby-sdnotify".freeze s.licenses = ["MIT".freeze] s.rubygems_version = "3.2.27".freeze s.summary = "Pure Ruby implementation of systemd's sd_notify(3)".freeze if s.respond_to? :specification_version then s.specification_version = 4 end if s.respond_to? :add_runtime_dependency then s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end end