Skip to content

Commit 5e86508

Browse files
authored
Better Kubernetes readiness check. (#42)
1 parent 0dcc634 commit 5e86508

File tree

11 files changed

+144
-26
lines changed

11 files changed

+144
-26
lines changed

bake/async/container/notify/log.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2025, by Samuel Williams.
5+
6+
def initialize(...)
7+
super
8+
9+
require "async/container/notify/log"
10+
end
11+
12+
# Check if the log file exists and the service is ready.
13+
# @parameter path [String] The path to the notification log file, uses the `NOTIFY_LOG` environment variable if not provided.
14+
def ready?(path: Async::Container::Notify::Log.path)
15+
if File.exist?(path)
16+
File.foreach(path) do |line|
17+
message = JSON.parse(line)
18+
if message["ready"] == true
19+
$stderr.puts "Service is ready: #{line}"
20+
return true
21+
end
22+
end
23+
24+
raise "Service is not ready yet."
25+
else
26+
raise "Notification log file does not exist at #{path}"
27+
end
28+
end

guides/getting-started/readme.md

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,3 @@ controller.run
8585
`SIGKILL` is the kill signal. The only behavior is to kill the process, immediately. As the process cannot catch the signal, it cannot cleanup, and thus this is a signal of last resort.
8686

8787
`SIGSTOP` is the pause signal. The only behavior is to pause the process; the signal cannot be caught or ignored. The shell uses pausing (and its counterpart, resuming via `SIGCONT`) to implement job control.
88-
89-
## Integration
90-
91-
### systemd
92-
93-
Install a template file into `/etc/systemd/system/`:
94-
95-
```
96-
# my-daemon.service
97-
[Unit]
98-
Description=My Daemon
99-
AssertPathExists=/srv/
100-
101-
[Service]
102-
Type=notify
103-
WorkingDirectory=/srv/my-daemon
104-
ExecStart=bundle exec my-daemon
105-
Nice=5
106-
107-
[Install]
108-
WantedBy=multi-user.target
109-
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Kubernetes Integration
2+
3+
This guide explains how to use `async-container` with Kubernetes to manage your application as a containerized service.
4+
5+
## Deployment Configuration
6+
7+
Create a deployment configuration file for your application:
8+
9+
```yaml
10+
# my-app-deployment.yaml
11+
apiVersion: apps/v1
12+
kind: Deployment
13+
metadata:
14+
name: my-app
15+
spec:
16+
replicas: 1
17+
selector:
18+
matchLabels:
19+
app: my-app
20+
template:
21+
metadata:
22+
labels:
23+
app: my-app
24+
spec:
25+
containers:
26+
- name: my-app
27+
image: my-app-image:latest
28+
env:
29+
- name: NOTIFY_LOG
30+
value: "/tmp/notify.log"
31+
ports:
32+
- containerPort: 3000
33+
readinessProbe:
34+
exec:
35+
command: ["bundle", "exec", "bake", "async:container:notify:log:ready?"]
36+
initialDelaySeconds: 5
37+
periodSeconds: 5
38+
failureThreshold: 12
39+
```

guides/links.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
getting-started:
2+
order: 1
3+
systemd-integration:
4+
order: 2
5+
kubernetes-integration:
6+
order: 3
7+
docker-integration:
8+
order: 4
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Systemd Integration
2+
3+
This guide explains how to use `async-container` with systemd to manage your application as a service.
4+
5+
## Service File
6+
7+
Install a template file into `/etc/systemd/system/`:
8+
9+
```
10+
# my-daemon.service
11+
[Unit]
12+
Description=My Daemon
13+
14+
[Service]
15+
Type=notify
16+
ExecStart=bundle exec my-daemon
17+
18+
[Install]
19+
WantedBy=multi-user.target
20+
```
21+
22+
Ensure `Type=notify` is set, so that the service can notify systemd when it is ready.

lib/async/container/forked.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def inspect
189189
"\#<#{self.class} name=#{@name.inspect} status=#{@status.inspect} pid=#{@pid.inspect}>"
190190
end
191191

192+
# @returns [String] A string representation of the process.
192193
alias to_s inspect
193194

194195
# Invoke {#terminate!} and then {#wait} for the child process to exit.

lib/async/container/notify/log.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ class Log < Client
1414
# The name of the environment variable which contains the path to the notification socket.
1515
NOTIFY_LOG = "NOTIFY_LOG"
1616

17+
# @returns [String] The path to the notification log file.
18+
# @parameter environment [Hash] The environment variables, defaults to `ENV`.
19+
def self.path(environment = ENV)
20+
environment[NOTIFY_LOG]
21+
end
22+
1723
# Open a notification client attached to the current {NOTIFY_LOG} if possible.
24+
# @parameter environment [Hash] The environment variables, defaults to `ENV`.
1825
def self.open!(environment = ENV)
19-
if path = environment.delete(NOTIFY_LOG)
26+
if path = self.path(environment)
2027
self.new(path)
2128
end
2229
end

lib/async/container/notify/pipe.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2020-2024, by Samuel Williams.
4+
# Copyright, 2020-2025, by Samuel Williams.
55
# Copyright, 2020, by Juan Antonio Martín Lucas.
66

77
require_relative "client"

releases.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## Unreleased
4+
5+
- Introduce `async:container:notify:log:ready?` task for detecting process readiness.
6+
37
## v0.24.0
48

59
- Add support for health check failure metrics.

test/async/container/hybrid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
# Released under the MIT License.
4-
# Copyright, 2019-2024, by Samuel Williams.
4+
# Copyright, 2019-2025, by Samuel Williams.
55

66
require "async/container/hybrid"
77
require "async/container/best"

0 commit comments

Comments
 (0)