|
| 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 | +## Controllers |
| 62 | + |
| 63 | +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. |
| 64 | + |
| 65 | +``` ruby |
| 66 | +require "async/container" |
| 67 | + |
| 68 | +Console.logger.debug! |
| 69 | + |
| 70 | +class Controller < Async::Container::Controller |
| 71 | + def create_container |
| 72 | + Async::Container::Forked.new |
| 73 | + # or Async::Container::Threaded.new |
| 74 | + # or Async::Container::Hybrid.new |
| 75 | + end |
| 76 | + |
| 77 | + def setup(container) |
| 78 | + container.run count: 2, restart: true do |instance| |
| 79 | + while true |
| 80 | + Console.debug(instance, "Sleeping...") |
| 81 | + sleep(1) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
| 86 | + |
| 87 | +controller = Controller.new |
| 88 | + |
| 89 | +controller.run |
| 90 | + |
| 91 | +# If you send SIGHUP to this process, it will recreate the container. |
| 92 | +``` |
| 93 | + |
| 94 | +### Controller Signal Handling |
| 95 | + |
| 96 | +Controllers are designed to run at the process level and are therefore responsible for processing signals. When your controller process receives these signals: |
| 97 | + |
| 98 | +- `SIGHUP` → Gracefully reload the container (restart with new configuration). |
| 99 | +- `SIGINT` → Begin graceful shutdown of the entire controller and all children. |
| 100 | +- `SIGTERM` → Begin immediate shutdown of the controller and all children. |
| 101 | + |
| 102 | +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 commit comments