async-container 0.34.3 → 0.34.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 977328d9a4dc6b4b7c86f634de0dba5f97b6e9da9c404b39ba1beea29f5711cc
4
- data.tar.gz: 766a0c91a80afa6e3867ecdaae4bcfc8fb75dde5a6e9d12a3c2afe29790d4e9b
3
+ metadata.gz: 45d1096677e99abf4e408324f81089d3a09dffe06faec9802505f0982a286c9c
4
+ data.tar.gz: 9297103dfdd8c2181881a7cc6639afcc4d16ea3acfcd4f6074429c6a06c229f5
5
5
  SHA512:
6
- metadata.gz: deb356cc50190aaa1a766e1eb53f4f573d2ca07c766f5b8549ebec923d6c5b451c4f9b371864ef83313d1e66658c52cbb11cc3c6196da4b0608d2a0e7e08d25a
7
- data.tar.gz: 456c21fa8ac6df624ee6558ab0935ab3d6175722908c815d0a726cced3de8708029053ba76268b1e8bc7a7e7fa185c474dcbc27ded2405ec164fe1e81246c19a
6
+ metadata.gz: c1e6e6c690ab46402878c54a91565decb35a5cfbb6f10b49e393949fafe2cba33da780bbdbb9a2b01dd9ad80487a16e83c7188667f22ee6e8c8696008a0e8ce2
7
+ data.tar.gz: fce543a64ebb908baa70bd0c5912b92b1395e3a99fc257cc3280cd3527f23215001e1aebfa50946f6fd956011f981b84e92a8ced7d4079e063fa9be542d1e8c6
checksums.yaml.gz.sig CHANGED
Binary file
@@ -0,0 +1,27 @@
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
+ return true
20
+ end
21
+ end
22
+
23
+ raise "Service is not ready yet."
24
+ else
25
+ raise "Notification log file does not exist at #{path}"
26
+ end
27
+ end
@@ -0,0 +1,144 @@
1
+ # Getting Started
2
+
3
+ This guide explains how to use `async-container` to build basic scalable systems.
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your project:
8
+
9
+ ~~~ bash
10
+ $ bundle add async-container
11
+ ~~~
12
+
13
+ ## Core Concepts
14
+
15
+ `async-container` has several core concepts:
16
+
17
+ - {ruby Async::Container::Forked} and {ruby Async::Container::Threaded} are used to manage one or more child processes and threads respectively for parallel execution. While threads share the address space which can reduce overall memory usage, processes have better isolation and fault tolerance.
18
+ - {ruby Async::Container::Controller} manages one or more containers and handles graceful restarts. Containers should be implemented in such a way that multiple containers can be running at the same time.
19
+
20
+ ## Containers
21
+
22
+ A container represents a set of child processes (or threads) which are doing work for you.
23
+
24
+ ``` ruby
25
+ require "async/container"
26
+
27
+ Console.logger.debug!
28
+
29
+ container = Async::Container.new
30
+
31
+ container.spawn do |task|
32
+ Console.debug task, "Sleeping..."
33
+ sleep(1)
34
+ Console.debug task, "Waking up!"
35
+ end
36
+
37
+ Console.debug "Waiting for container..."
38
+ container.wait
39
+ Console.debug "Finished."
40
+ ```
41
+
42
+ ### Stopping Child Processes
43
+
44
+ Containers provide three approaches for stopping child processes (or threads). When you call `container.stop()`, a progressive approach is used:
45
+
46
+ - **Interrupt** means **"Please start shutting down gracefully"**. This is the gentlest shutdown request, giving applications maximum time to finish current work and cleanup resources.
47
+
48
+ - **Terminate** means **"Shut down now"**. This is more urgent - the process should stop what it's doing and terminate promptly, but still has a chance to cleanup.
49
+
50
+ - **Kill** means **"Die immediately"**. This forcefully terminates the process with no cleanup opportunity. This is the method of last resort.
51
+
52
+ The escalation sequence follows this pattern:
53
+ 1. interrupt → wait for timeout → still running?
54
+ 2. terminate → wait for timeout → still running?
55
+ 3. kill → process terminated.
56
+
57
+ This gives well-behaved processes multiple opportunities to shut down gracefully, while ensuring that unresponsive processes are eventually killed.
58
+
59
+ **Implementation Note:** For forked containers, these methods send Unix signals (`SIGINT`, `SIGTERM`, `SIGKILL`). For threaded containers, they use different mechanisms appropriate to threads. The container abstraction hides these implementation details.
60
+
61
+ ### Health Checking and Timeouts
62
+
63
+ Containers can monitor child processes to detect hung or unresponsive processes using two timeout mechanisms:
64
+
65
+ - **`startup_timeout`**: Maximum time a process can take to become ready (call `instance.ready!`) before being terminated. This detects processes that hang during startup.
66
+ - **`health_check_timeout`**: Maximum time between health check messages after a process has become ready. This detects processes that stop responding after they've started.
67
+
68
+ Both timeouts use a single clock that starts when the process starts. The clock resets when the process becomes ready, transitioning from startup timeout monitoring to health check timeout monitoring.
69
+
70
+ ``` ruby
71
+ require "async/container"
72
+
73
+ container = Async::Container.new
74
+
75
+ container.run(
76
+ count: 2,
77
+ restart: true,
78
+ # Allow up to 60 seconds for startup:
79
+ startup_timeout: 60,
80
+ # Require health checks every 30 seconds after ready:
81
+ health_check_timeout: 30
82
+ ) do |instance|
83
+ # Send status updates during startup:
84
+ instance.status!("Preparing...")
85
+
86
+ # Do initialization work...
87
+
88
+ # Signal readiness:
89
+ instance.ready!
90
+
91
+ # After ready, send periodic health checks:
92
+ while true
93
+ instance.ready!
94
+ sleep(10)
95
+ end
96
+ end
97
+
98
+ container.wait
99
+ ```
100
+
101
+ **Note:** Any message sent through the notification pipe (including `status!` and `ready!`) resets the timeout clock. This allows processes to take time during startup while still detecting hung processes.
102
+
103
+ ## Controllers
104
+
105
+ The controller provides the life-cycle management for one or more containers of processes. It provides behaviour like starting, restarting, reloading and stopping. You can see some [example implementations in Falcon](https://github.com/socketry/falcon/blob/master/lib/falcon/controller/). If the process running the controller receives `SIGHUP` it will recreate the container gracefully.
106
+
107
+ ``` ruby
108
+ require "async/container"
109
+
110
+ Console.logger.debug!
111
+
112
+ class Controller < Async::Container::Controller
113
+ def create_container
114
+ Async::Container::Forked.new
115
+ # or Async::Container::Threaded.new
116
+ # or Async::Container::Hybrid.new
117
+ end
118
+
119
+ def setup(container)
120
+ container.run count: 2, restart: true do |instance|
121
+ while true
122
+ Console.debug(instance, "Sleeping...")
123
+ sleep(1)
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ controller = Controller.new
130
+
131
+ controller.run
132
+
133
+ # If you send SIGHUP to this process, it will recreate the container.
134
+ ```
135
+
136
+ ### Controller Signal Handling
137
+
138
+ Controllers are designed to run at the process level and are therefore responsible for processing signals. When your controller process receives these signals:
139
+
140
+ - `SIGHUP` → Gracefully reload the container (restart with new configuration).
141
+ - `SIGINT` → Begin graceful shutdown of the entire controller and all children.
142
+ - `SIGTERM` → Begin immediate shutdown of the controller and all children.
143
+
144
+ Ideally, do not send `SIGKILL` to a controller, as it will immediately terminate the controller without giving it a chance to gracefully shut down child processes. This can leave orphaned processes running and prevent proper cleanup.
@@ -0,0 +1,25 @@
1
+ # Automatically generated context index for Utopia::Project guides.
2
+ # Do not edit then files in this directory directly, instead edit the guides and then run `bake utopia:project:agent:context:update`.
3
+ ---
4
+ description: Abstract container-based parallelism using threads and processes where
5
+ appropriate.
6
+ metadata:
7
+ documentation_uri: https://socketry.github.io/async-container/
8
+ source_code_uri: https://github.com/socketry/async-container.git
9
+ files:
10
+ - path: getting-started.md
11
+ title: Getting Started
12
+ description: This guide explains how to use `async-container` to build basic scalable
13
+ systems.
14
+ - path: policies.md
15
+ title: Container Policies
16
+ description: This guide explains how to use policies to monitor container health
17
+ and implement custom failure handling strategies.
18
+ - path: systemd-integration.md
19
+ title: Systemd Integration
20
+ description: This guide explains how to use `async-container` with systemd to manage
21
+ your application as a service.
22
+ - path: kubernetes-integration.md
23
+ title: Kubernetes Integration
24
+ description: This guide explains how to use `async-container` with Kubernetes to
25
+ manage your application as a containerized service.
@@ -0,0 +1,45 @@
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
+ ```
40
+
41
+ ## Graceful Shutdown
42
+
43
+ Controllers handle `SIGTERM` gracefully (same as `SIGINT`). This ensures proper graceful shutdown when Kubernetes terminates pods during rolling updates, scaling down, or pod eviction.
44
+
45
+ **Note**: Kubernetes sends `SIGTERM` to containers when terminating pods. With graceful handling, your application will have time to clean up resources, finish in-flight requests, and shut down gracefully before Kubernetes sends `SIGKILL` (after the termination grace period).
@@ -0,0 +1,168 @@
1
+ # Container Policies
2
+
3
+ This guide explains how to use policies to monitor container health and implement custom failure handling strategies.
4
+
5
+ ## Motivation
6
+
7
+ Containers restart failing child processes automatically, but sometimes you need more intelligent behavior:
8
+
9
+ - **Detect failure patterns**: Repeated segfaults indicate serious bugs that won't fix themselves by restarting.
10
+ - **Prevent resource waste**: Stop trying to restart processes that will never succeed.
11
+ - **Monitor health**: Track failure rates and alert when thresholds are exceeded.
12
+ - **Custom responses**: Implement application-specific logic for different failure types.
13
+
14
+ Use policies when you need:
15
+ - **Segfault detection**: Stop the container after multiple segfaults indicate memory corruption.
16
+ - **Failure rate monitoring**: Alert or stop when children fail too frequently.
17
+ - **Custom logging**: Track specific failure types for debugging.
18
+ - **Graceful degradation**: Give unhealthy children extra time before killing them.
19
+
20
+ ## Default Behavior
21
+
22
+ Containers use {ruby Async::Container::Policy::DEFAULT} unless you specify otherwise. The default policy:
23
+
24
+ - Allows children to restart indefinitely if configured with `restart: true`.
25
+ - Kills children immediately when health checks fail.
26
+ - Kills children immediately when startup timeouts are exceeded.
27
+
28
+ This is appropriate for most applications, but you can customize it.
29
+
30
+ ## Creating Custom Policies
31
+
32
+ Policies are Ruby classes that inherit from {ruby Async::Container::Policy} and override callback methods:
33
+
34
+ ``` ruby
35
+ require "async/container"
36
+
37
+ class SegfaultDetectionPolicy < Async::Container::Policy
38
+ def initialize(max_segfaults: 3, window: 60)
39
+ @max_segfaults = max_segfaults
40
+ @segfault_rate = Async::Container::Rate.new(window: window)
41
+ end
42
+
43
+ def child_exit(container, child, status, name:, key:, **options)
44
+ if segfault?(status)
45
+ @segfault_rate.add(1)
46
+
47
+ segfault_count = @segfault_rate.total
48
+
49
+ Console.warn(self, "Segfault detected",
50
+ name: name,
51
+ count: segfault_count,
52
+ rate: @segfault_rate.per_minute
53
+ )
54
+ end
55
+
56
+ # Stop container if too many segfaults
57
+ if segfault_count >= @max_segfaults
58
+ unless container.stopping?
59
+ Console.error(self, "Too many segfaults, stopping container",
60
+ count: segfault_count,
61
+ rate: @segfault_rate.per_second
62
+ )
63
+ container.stop(false)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ controller = Async::Container::Controller.new
70
+
71
+ # Use the custom policy:
72
+ def controller.make_policy
73
+ SegfaultDetectionPolicy.new(max_segfaults: 5, window: 120)
74
+ end
75
+
76
+ # ...
77
+
78
+ controller.run
79
+ ```
80
+
81
+ ## Policy Callbacks
82
+
83
+ Policies can implement these callbacks:
84
+
85
+ ### `child_spawn`
86
+
87
+ Called when a child process starts:
88
+
89
+ ``` ruby
90
+ def child_spawn(container, child, name:, key:, **options)
91
+ Console.info(self, "Child started", name: name)
92
+ end
93
+ ```
94
+
95
+ Use this for tracking which children are running or initializing per-child state.
96
+
97
+ ### `child_exit`
98
+
99
+ Called when a child process exits (success or failure):
100
+
101
+ ``` ruby
102
+ def child_exit(container, child, status, name:, key:, **options)
103
+ if success?(status)
104
+ Console.info(self, "Child succeeded", name: name)
105
+ else
106
+ Console.warn(self, "Child failed",
107
+ name: name,
108
+ exit_code: exit_code(status),
109
+ signal: signal(status)
110
+ )
111
+ end
112
+ end
113
+ ```
114
+
115
+ This is the main callback for implementing failure detection logic. You can:
116
+ - Track failure rates.
117
+ - Detect specific failure types (segfaults, aborts).
118
+ - Call `container.stop` to stop the entire container.
119
+
120
+ ### `health_check_failed`
121
+
122
+ Called when a health check timeout is exceeded. The default implementation logs and kills the child:
123
+
124
+ ``` ruby
125
+ def health_check_failed(container, child, age:, timeout:, **options)
126
+ Console.warn(self, "Health check failed", child: child, age: age)
127
+
128
+ # Send alert before killing
129
+ send_alert("Health check failed for child")
130
+
131
+ # Call default behavior
132
+ super
133
+ end
134
+ ```
135
+
136
+ Override this to:
137
+ - Send alerts before killing.
138
+ - Give children more time (don't kill immediately).
139
+ - Implement custom recovery logic.
140
+
141
+ ### `startup_failed`
142
+
143
+ Called when a startup timeout is exceeded. The default implementation logs and kills the child:
144
+
145
+ ``` ruby
146
+ def startup_failed(container, child, age:, timeout:, **options)
147
+ # Custom logging with more context
148
+ Console.error(self, "Child never became ready",
149
+ child: child,
150
+ age: age,
151
+ timeout: timeout
152
+ )
153
+
154
+ # Kill the child (default behavior)
155
+ super
156
+ end
157
+ ```
158
+
159
+ ## Helper Methods
160
+
161
+ The {ruby Async::Container::Policy} class provides helper methods for detecting specific failure conditions:
162
+
163
+ - `segfault?(status)` - Returns true if the process was terminated by SIGSEGV.
164
+ - `abort?(status)` - Returns true if the process was terminated by SIGABRT.
165
+ - `killed?(status)` - Returns true if the process was terminated by SIGKILL.
166
+ - `success?(status)` - Returns true if the process exited successfully.
167
+ - `signal(status)` - Returns the signal number that terminated the process.
168
+ - `exit_code(status)` - Returns the exit code.
@@ -0,0 +1,28 @@
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.
23
+
24
+ ## Graceful Shutdown
25
+
26
+ Controllers handle `SIGTERM` gracefully (same as `SIGINT`). This ensures proper graceful shutdown when systemd stops the service.
27
+
28
+ **Note**: systemd sends `SIGTERM` to services when stopping them. With graceful handling, your application will have time to clean up resources, finish in-flight requests, and shut down gracefully before systemd escalates to `SIGKILL` (after the timeout specified in the service file).
@@ -5,6 +5,6 @@
5
5
 
6
6
  module Async
7
7
  module Container
8
- VERSION = "0.34.3"
8
+ VERSION = "0.34.4"
9
9
  end
10
10
  end
data/readme.md CHANGED
@@ -28,6 +28,10 @@ Please see the [project documentation](https://socketry.github.io/async-containe
28
28
 
29
29
  Please see the [project releases](https://socketry.github.io/async-container/releases/index) for all releases.
30
30
 
31
+ ### v0.34.4
32
+
33
+ - Add missing `bake` and `context` files to the release.
34
+
31
35
  ### v0.34.3
32
36
 
33
37
  - `Controller#restart` and `Controller#reload` now include a `status:` message in the `ready!` notification, so `systemctl status` shows "Running with N children." instead of the stale "Initializing controller..." message.
@@ -68,11 +72,6 @@ Please see the [project releases](https://socketry.github.io/async-container/rel
68
72
 
69
73
  - Introduce `Client#healthy!` for sending health check messages.
70
74
 
71
- ### v0.28.0
72
-
73
- - Add `startup_timeout` parameter to `spawn` and `run` methods for detecting processes that hang during startup and never become ready.
74
- - Health check timeout now only applies after a process becomes ready, preventing premature timeouts for slow-starting applications.
75
-
76
75
  ## Contributing
77
76
 
78
77
  We welcome contributions to this project.
data/releases.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Releases
2
2
 
3
+ ## v0.34.4
4
+
5
+ - Add missing `bake` and `context` files to the release.
6
+
3
7
  ## v0.34.3
4
8
 
5
9
  - `Controller#restart` and `Controller#reload` now include a `status:` message in the `ready!` notification, so `systemctl status` shows "Running with N children." instead of the stale "Initializing controller..." message.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async-container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.34.3
4
+ version: 0.34.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -61,6 +61,12 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
+ - bake/async/container/notify/log.rb
65
+ - context/getting-started.md
66
+ - context/index.yaml
67
+ - context/kubernetes-integration.md
68
+ - context/policies.md
69
+ - context/systemd-integration.md
64
70
  - lib/async/container.rb
65
71
  - lib/async/container/best.rb
66
72
  - lib/async/container/channel.rb
@@ -107,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
113
  - !ruby/object:Gem::Version
108
114
  version: '0'
109
115
  requirements: []
110
- rubygems_version: 4.0.3
116
+ rubygems_version: 4.0.6
111
117
  specification_version: 4
112
118
  summary: Abstract container-based parallelism using threads and processes where appropriate.
113
119
  test_files: []
metadata.gz.sig CHANGED
Binary file