File tree Expand file tree Collapse file tree 4 files changed +78
-0
lines changed
test/async/container/notify Expand file tree Collapse file tree 4 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 6
6
require_relative "notify/pipe"
7
7
require_relative "notify/socket"
8
8
require_relative "notify/console"
9
+ require_relative "notify/log"
9
10
10
11
module Async
11
12
module Container
@@ -18,6 +19,7 @@ def self.open!
18
19
@client ||= (
19
20
Pipe . open! ||
20
21
Socket . open! ||
22
+ Log . open! ||
21
23
Console . open!
22
24
)
23
25
end
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 5
5
# Copyright, 2020, by Olle Jonsson.
6
6
7
7
require "tmpdir"
8
+ require "socket"
8
9
require "securerandom"
9
10
10
11
module Async
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments