Skip to content

Commit baa2a97

Browse files
committed
Add support for NOTIFY_LOG.
1 parent b1be8ea commit baa2a97

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lib/async/container/notify.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_relative "notify/pipe"
77
require_relative "notify/socket"
88
require_relative "notify/console"
9+
require_relative "notify/log"
910

1011
module Async
1112
module Container
@@ -18,6 +19,7 @@ def self.open!
1819
@client ||= (
1920
Pipe.open! ||
2021
Socket.open! ||
22+
Log.open! ||
2123
Console.open!
2224
)
2325
end

lib/async/container/notify/log.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2020-2024, by Samuel Williams.
5+
6+
require_relative "client"
7+
require "socket"
8+
9+
module Async
10+
module Container
11+
module Notify
12+
class Log < Client
13+
# The name of the environment variable which contains the path to the notification socket.
14+
NOTIFY_LOG = "NOTIFY_LOG"
15+
16+
# Open a notification client attached to the current {NOTIFY_LOG} if possible.
17+
def self.open!(environment = ENV)
18+
if path = environment.delete(NOTIFY_LOG)
19+
self.new(path)
20+
end
21+
end
22+
23+
# Initialize the notification client.
24+
# @parameter path [String] The path to the UNIX socket used for sending messages to the process manager.
25+
def initialize(path)
26+
@path = path
27+
end
28+
29+
# @attribute [String] The path to the UNIX socket used for sending messages to the controller.
30+
attr :path
31+
32+
# Send the given message.
33+
# @parameter message [Hash]
34+
def send(**message)
35+
data = JSON.dump(message)
36+
37+
File.open(@path, "a") do |file|
38+
file.puts(data)
39+
end
40+
end
41+
42+
# Send the specified error.
43+
# `sd_notify` requires an `errno` key, which defaults to `-1` to indicate a generic error.
44+
def error!(text, **message)
45+
message[:errno] ||= -1
46+
47+
super
48+
end
49+
end
50+
end
51+
end
52+
end

lib/async/container/notify/server.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright, 2020, by Olle Jonsson.
66

77
require "tmpdir"
8+
require "socket"
89
require "securerandom"
910

1011
module Async

test/async/container/notify/log.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2020-2025, by Samuel Williams.
5+
# Copyright, 2020, by Olle Jonsson.
6+
7+
require "async/container/controller"
8+
require "async/container/controllers"
9+
10+
require "tmpdir"
11+
12+
describe Async::Container::Notify::Pipe do
13+
let(:notify_script) {Async::Container::Controllers.path_for("notify")}
14+
let(:notify_log) {File.expand_path("notify-#{::Process.pid}-#{SecureRandom.hex(8)}.log", Dir.tmpdir)}
15+
16+
it "receives notification of child status" do
17+
system({"NOTIFY_LOG" => notify_log}, "bundle", "exec", notify_script)
18+
19+
lines = File.readlines(notify_log).map{|line| JSON.parse(line)}
20+
21+
expect(lines.last).to be == {"ready" => true}
22+
end
23+
end

0 commit comments

Comments
 (0)