Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/async/container/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "notify/pipe"
require_relative "notify/socket"
require_relative "notify/console"
require_relative "notify/log"

module Async
module Container
Expand All @@ -18,6 +19,7 @@ def self.open!
@client ||= (
Pipe.open! ||
Socket.open! ||
Log.open! ||
Console.open!
)
end
Expand Down
52 changes: 52 additions & 0 deletions lib/async/container/notify/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2024, by Samuel Williams.

require_relative "client"
require "socket"

module Async
module Container
module Notify
class Log < Client
# The name of the environment variable which contains the path to the notification socket.
NOTIFY_LOG = "NOTIFY_LOG"

# Open a notification client attached to the current {NOTIFY_LOG} if possible.
def self.open!(environment = ENV)
if path = environment.delete(NOTIFY_LOG)
self.new(path)
end
end

# Initialize the notification client.
# @parameter path [String] The path to the UNIX socket used for sending messages to the process manager.
def initialize(path)
@path = path
end

# @attribute [String] The path to the UNIX socket used for sending messages to the controller.
attr :path

# Send the given message.
# @parameter message [Hash]
def send(**message)
data = JSON.dump(message)

File.open(@path, "a") do |file|
file.puts(data)
end
end

# Send the specified error.
# `sd_notify` requires an `errno` key, which defaults to `-1` to indicate a generic error.
def error!(text, **message)
message[:errno] ||= -1

super
end
end
end
end
end
1 change: 1 addition & 0 deletions lib/async/container/notify/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Copyright, 2020, by Olle Jonsson.

require "tmpdir"
require "socket"
require "securerandom"

module Async
Expand Down
23 changes: 23 additions & 0 deletions test/async/container/notify/log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2025, by Samuel Williams.
# Copyright, 2020, by Olle Jonsson.

require "async/container/controller"
require "async/container/controllers"

require "tmpdir"

describe Async::Container::Notify::Pipe do
let(:notify_script) {Async::Container::Controllers.path_for("notify")}
let(:notify_log) {File.expand_path("notify-#{::Process.pid}-#{SecureRandom.hex(8)}.log", Dir.tmpdir)}

it "receives notification of child status" do
system({"NOTIFY_LOG" => notify_log}, "bundle", "exec", notify_script)

lines = File.readlines(notify_log).map{|line| JSON.parse(line)}

expect(lines.last).to be == {"ready" => true}
end
end
Loading