sd_notify 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +20 -0
  3. data/README.md +56 -0
  4. data/lib/sd_notify.rb +97 -0
  5. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ca0c254a94d204fddc97bf051fbf5e1a3ad8697
4
+ data.tar.gz: 342501c3de33d0cd45edabbf8f4a9b5adc2ba41f
5
+ SHA512:
6
+ metadata.gz: be54ed4907b91bbd92963cb0cd4b524ec2b2763346f92772f99a5bd2d723faa05a34dea3ed23319c85fb4fd159d60a74dcdad4e5d2c8ecdb34a99136f6d3d4be
7
+ data.tar.gz: 736bd5cbdd81d7a18fe4aabe9c57762545a3c16b428f06bdbba086858249b399ab72c7ee134745a4fe4f5a33e276a940c40bc9d50368682deaffa8054fc3d177
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2017 Agis Anastasopoulos
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # ruby-sdnotify
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ruby-sdnotify.svg)](https://badge.fury.io/rb/ruby-sdnotify)
4
+ [![Documentation](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/agis/ruby-sdnotify)
5
+
6
+ A pure Ruby implementation of [sd_notify(3)](https://www.freedesktop.org/software/systemd/man/sd_notify.html) that can be used to
7
+ communicate state changes of Ruby programs to [systemd](https://www.freedesktop.org/wiki/Software/systemd/).
8
+
9
+ Refer to the [API documentation](http://www.rubydoc.info/github/agis/ruby-sdnotify) for more info.
10
+
11
+ ## Getting started
12
+
13
+ Install ruby-sdnotify:
14
+
15
+ ```shell
16
+ $ gem install ruby-sdnotify
17
+ ```
18
+
19
+ If you're using Bundler, add it to your Gemfile:
20
+
21
+ ```ruby
22
+ gem "ruby-sdnotify"
23
+ ```
24
+
25
+ and run `bundle install`.
26
+
27
+ ## Usage
28
+
29
+ The [API](http://www.rubydoc.info/github/agis/ruby-sdnotify) is mostly tied to
30
+ the official implementation, therefore refer to the [sd_notify(3) man pages](https://www.freedesktop.org/software/systemd/man/sd_notify.html)
31
+ for detailed description of how the notification mechanism works.
32
+
33
+ An example (assuming the program shipped as a systemd service):
34
+
35
+ ```ruby
36
+ require "sd_notify"
37
+
38
+ puts "Hello. Booting..."
39
+ sleep 2 # do some initialization work ...
40
+ SdNotify.ready
41
+
42
+ sum = 0
43
+ 5.times do |i|
44
+ sleep 1 # perform some work
45
+ sum += 1
46
+ SdNotify.status("{sum} jobs completed")
47
+ end
48
+
49
+ puts "Finished working, shutting down..."
50
+ SdNotify.stopping
51
+ sleep 2 # do cleanup work...
52
+ ```
53
+
54
+ ## License
55
+
56
+ ruby-sdnotify is licensed under MIT. See [LICENSE](LICENSE).
@@ -0,0 +1,97 @@
1
+ require "socket"
2
+
3
+ # SdNotify is a pure-Ruby implementation of sd_notify(3). It can be used to
4
+ # notify systemd about state changes. Methods of this package are no-op on
5
+ # non-systemd systems (eg. Darwin).
6
+ #
7
+ # The API maps closely to the original implementation of sd_notify(3),
8
+ # therefore be sure to check the official man pages prior to using SdNotify.
9
+ #
10
+ # @see https://www.freedesktop.org/software/systemd/man/sd_notify.html
11
+ module SdNotify
12
+ # Exception raised when there's an error writing to the notification socket
13
+ class NotifyError < RuntimeError; end
14
+
15
+ READY = "READY=1"
16
+ RELOADING = "RELOADING=1"
17
+ STOPPING = "STOPPING=1"
18
+ STATUS = "STATUS="
19
+ ERRNO = "ERRNO="
20
+ MAINPID = "MAINPID="
21
+ WATCHDOG = "WATCHDOG=1"
22
+ FDSTORE = "FDSTORE=1"
23
+
24
+ def self.ready(unset_env=false)
25
+ notify(READY, unset_env)
26
+ end
27
+
28
+ def self.reloading(unset_env=false)
29
+ notify(RELOADING, unset_env)
30
+ end
31
+
32
+ def self.stopping(unset_env=false)
33
+ notify(STOPPING, unset_env)
34
+ end
35
+
36
+ # @param status [String] a custom status string that describes the current
37
+ # state of the service
38
+ def self.status(status, unset_env=false)
39
+ notify("#{STATUS}#{status}", unset_env)
40
+ end
41
+
42
+ # @param errno [Integer]
43
+ def self.errno(errno, unset_env=false)
44
+ notify("#{ERRNO}#{errno}", unset_env)
45
+ end
46
+
47
+ # @param pid [Integer]
48
+ def self.mainpid(pid, unset_env=false)
49
+ notify("#{MAINPID}#{pid}", unset_env)
50
+ end
51
+
52
+ def self.watchdog(unset_env=false)
53
+ notify(WATCHDOG, unset_env)
54
+ end
55
+
56
+ def self.fdstore(unset_env=false)
57
+ notify(FDSTORE, unset_env)
58
+ end
59
+
60
+ # Notify systemd with the provided state, via the notification socket, if
61
+ # any.
62
+ #
63
+ # Generally this method will be used indirectly through the other methods
64
+ # of the library.
65
+ #
66
+ # @param state [String]
67
+ # @param unset_env [Boolean]
68
+ #
69
+ # @return [Fixnum, nil] the number of bytes written to the notification
70
+ # socket or nil if there was no socket to report to (eg. the program wasn't
71
+ # started by systemd)
72
+ #
73
+ # @raise [NotifyError] if there was an error communicating with the systemd
74
+ # socket
75
+ #
76
+ # @see https://www.freedesktop.org/software/systemd/man/sd_notify.html
77
+ def self.notify(state, unset_env=false)
78
+ sock = ENV["NOTIFY_SOCKET"]
79
+
80
+ return nil if !sock
81
+
82
+ ENV.delete("NOTIFY_SOCKET") if unset_env
83
+
84
+ connected = false
85
+
86
+ begin
87
+ sock = Addrinfo.unix(sock, :DGRAM).connect
88
+ connected = true
89
+ sock.close_on_exec = true
90
+ sock.write(state)
91
+ rescue StandardError => e
92
+ raise NotifyError, "#{e.class}: #{e.message}", e.backtrace
93
+ ensure
94
+ sock.close if connected
95
+ end
96
+ end
97
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sd_notify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Agis Anastasopoulos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: sd_notify can be used to notify systemd about various service status
14
+ changes of Ruby programs
15
+ email: agis.anast@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/sd_notify.rb
23
+ homepage: https://github.com/agis/ruby-sdnotify
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.13
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Pure Ruby implementation of systemd's sd_notify(3)
47
+ test_files: []