Compare commits
14 Commits
v0.7.4
...
036abcc407
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
036abcc407 | ||
| f777ec9b1e | |||
| 2415088792 | |||
| a35d15edc2 | |||
| 613947ac88 | |||
| 145e067454 | |||
|
82de5b260f
|
|||
| eb360e565c | |||
|
c18c8a885c
|
|||
|
762da1e672
|
|||
| 3bc512ab48 | |||
|
7d8071d41b
|
|||
|
1cc4138ec7
|
|||
|
00517daaaa
|
32
CHANGELOG.md
32
CHANGELOG.md
@@ -6,6 +6,38 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.8.2] - 2025-11-14
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- add publish
|
||||||
|
- add readme
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- *(deps)* update all dependencies (#38)
|
||||||
|
|
||||||
|
### Other
|
||||||
|
- *(deps)* update rust crate tracing-subscriber to v0.3.20 (#37)
|
||||||
|
|
||||||
|
## [0.8.1] - 2025-08-09
|
||||||
|
|
||||||
|
### Other
|
||||||
|
- error logging
|
||||||
|
|
||||||
|
## [0.8.0] - 2025-08-08
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- add docs
|
||||||
|
- update readme
|
||||||
|
|
||||||
|
## [0.7.5] - 2025-07-24
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- print big inner
|
||||||
|
|
||||||
|
### Other
|
||||||
|
- more error correction
|
||||||
|
- correct error test to not be as verbose
|
||||||
|
|
||||||
## [0.7.4] - 2025-07-24
|
## [0.7.4] - 2025-07-24
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
299
Cargo.lock
generated
299
Cargo.lock
generated
@@ -2,21 +2,6 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "addr2line"
|
|
||||||
version = "0.24.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1"
|
|
||||||
dependencies = [
|
|
||||||
"gimli",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "adler2"
|
|
||||||
version = "2.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aho-corasick"
|
name = "aho-corasick"
|
||||||
version = "1.1.3"
|
version = "1.1.3"
|
||||||
@@ -28,15 +13,15 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.98"
|
version = "1.0.100"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.88"
|
version = "0.1.89"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e539d3fca749fcee5236ab05e93a52867dd549cc157c8cb7f99595f3cedffdb5"
|
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -49,21 +34,6 @@ version = "1.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "backtrace"
|
|
||||||
version = "0.3.74"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a"
|
|
||||||
dependencies = [
|
|
||||||
"addr2line",
|
|
||||||
"cfg-if",
|
|
||||||
"libc",
|
|
||||||
"miniz_oxide",
|
|
||||||
"object",
|
|
||||||
"rustc-demangle",
|
|
||||||
"windows-targets",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.6.0"
|
version = "2.6.0"
|
||||||
@@ -186,32 +156,15 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi 0.13.3+wasi-0.2.2",
|
"wasi 0.13.3+wasi-0.2.2",
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "gimli"
|
|
||||||
version = "0.31.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.3.9"
|
version = "0.3.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "io-uring"
|
|
||||||
version = "0.7.8"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b86e202f00093dcba4275d4636b93ef9dd75d025ae560d2521b45ea28ab49013"
|
|
||||||
dependencies = [
|
|
||||||
"bitflags",
|
|
||||||
"cfg-if",
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@@ -220,9 +173,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.169"
|
version = "0.2.177"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
@@ -242,11 +195,11 @@ checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchers"
|
name = "matchers"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
|
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"regex-automata 0.1.10",
|
"regex-automata",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -255,15 +208,6 @@ version = "2.7.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "miniz_oxide"
|
|
||||||
version = "0.8.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1"
|
|
||||||
dependencies = [
|
|
||||||
"adler2",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mio"
|
name = "mio"
|
||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
@@ -273,12 +217,12 @@ dependencies = [
|
|||||||
"hermit-abi",
|
"hermit-abi",
|
||||||
"libc",
|
"libc",
|
||||||
"wasi 0.11.0+wasi-snapshot-preview1",
|
"wasi 0.11.0+wasi-snapshot-preview1",
|
||||||
"windows-sys",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "notmad"
|
name = "notmad"
|
||||||
version = "0.7.3"
|
version = "0.8.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
@@ -295,21 +239,11 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nu-ansi-term"
|
name = "nu-ansi-term"
|
||||||
version = "0.46.0"
|
version = "0.50.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
|
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"overload",
|
"windows-sys 0.61.2",
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "object"
|
|
||||||
version = "0.36.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e"
|
|
||||||
dependencies = [
|
|
||||||
"memchr",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -318,12 +252,6 @@ version = "1.20.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "overload"
|
|
||||||
version = "0.1.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@@ -344,7 +272,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -425,27 +353,6 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex"
|
|
||||||
version = "1.11.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
|
||||||
dependencies = [
|
|
||||||
"aho-corasick",
|
|
||||||
"memchr",
|
|
||||||
"regex-automata 0.4.9",
|
|
||||||
"regex-syntax 0.8.5",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-automata"
|
|
||||||
version = "0.1.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
|
|
||||||
dependencies = [
|
|
||||||
"regex-syntax 0.6.29",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-automata"
|
name = "regex-automata"
|
||||||
version = "0.4.9"
|
version = "0.4.9"
|
||||||
@@ -454,27 +361,15 @@ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"aho-corasick",
|
"aho-corasick",
|
||||||
"memchr",
|
"memchr",
|
||||||
"regex-syntax 0.8.5",
|
"regex-syntax",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "regex-syntax"
|
|
||||||
version = "0.6.29"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "regex-syntax"
|
name = "regex-syntax"
|
||||||
version = "0.8.5"
|
version = "0.8.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rustc-demangle"
|
|
||||||
version = "0.1.24"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scopeguard"
|
name = "scopeguard"
|
||||||
version = "1.2.0"
|
version = "1.2.0"
|
||||||
@@ -516,12 +411,12 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "socket2"
|
name = "socket2"
|
||||||
version = "0.5.7"
|
version = "0.6.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
|
checksum = "17129e116933cf371d018bb80ae557e889637989d8638274fb25622827b03881"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys",
|
"windows-sys 0.60.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -537,18 +432,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "2.0.12"
|
version = "2.0.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
checksum = "f63587ca0f12b72a0600bcba1d40081f830876000bb46dd2337a3051618f4fc8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror-impl"
|
name = "thiserror-impl"
|
||||||
version = "2.0.12"
|
version = "2.0.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
|
checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -567,29 +462,26 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.46.1"
|
version = "1.48.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0cc3a2344dafbe23a245241fe8b09735b521110d30fcefbbd5feb1797ca35d17"
|
checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"backtrace",
|
|
||||||
"bytes",
|
"bytes",
|
||||||
"io-uring",
|
|
||||||
"libc",
|
"libc",
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"slab",
|
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-macros"
|
name = "tokio-macros"
|
||||||
version = "2.5.0"
|
version = "2.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
|
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@@ -598,9 +490,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio-util"
|
name = "tokio-util"
|
||||||
version = "0.7.15"
|
version = "0.7.17"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "66a539a9ad6d5d281510d5bd368c973d636c02dbf8a67300bfb6b950696ad7df"
|
checksum = "2efa149fe76073d6e8fd97ef4f4eca7b67f599660115591483572e406e165594"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
"futures-core",
|
"futures-core",
|
||||||
@@ -655,14 +547,14 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tracing-subscriber"
|
name = "tracing-subscriber"
|
||||||
version = "0.3.19"
|
version = "0.3.20"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008"
|
checksum = "2054a14f5307d601f88daf0553e1cbf472acc4f2c51afab632431cdcd72124d5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"matchers",
|
"matchers",
|
||||||
"nu-ansi-term",
|
"nu-ansi-term",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"regex",
|
"regex-automata",
|
||||||
"sharded-slab",
|
"sharded-slab",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thread_local",
|
"thread_local",
|
||||||
@@ -720,26 +612,10 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "windows-link"
|
||||||
version = "0.3.9"
|
version = "0.2.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||||
dependencies = [
|
|
||||||
"winapi-i686-pc-windows-gnu",
|
|
||||||
"winapi-x86_64-pc-windows-gnu",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-i686-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
@@ -747,7 +623,25 @@ version = "0.52.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-targets",
|
"windows-targets 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.60.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets 0.53.5",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.61.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||||
|
dependencies = [
|
||||||
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -756,14 +650,31 @@ version = "0.52.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows_aarch64_gnullvm",
|
"windows_aarch64_gnullvm 0.52.6",
|
||||||
"windows_aarch64_msvc",
|
"windows_aarch64_msvc 0.52.6",
|
||||||
"windows_i686_gnu",
|
"windows_i686_gnu 0.52.6",
|
||||||
"windows_i686_gnullvm",
|
"windows_i686_gnullvm 0.52.6",
|
||||||
"windows_i686_msvc",
|
"windows_i686_msvc 0.52.6",
|
||||||
"windows_x86_64_gnu",
|
"windows_x86_64_gnu 0.52.6",
|
||||||
"windows_x86_64_gnullvm",
|
"windows_x86_64_gnullvm 0.52.6",
|
||||||
"windows_x86_64_msvc",
|
"windows_x86_64_msvc 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-targets"
|
||||||
|
version = "0.53.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"
|
||||||
|
dependencies = [
|
||||||
|
"windows-link",
|
||||||
|
"windows_aarch64_gnullvm 0.53.1",
|
||||||
|
"windows_aarch64_msvc 0.53.1",
|
||||||
|
"windows_i686_gnu 0.53.1",
|
||||||
|
"windows_i686_gnullvm 0.53.1",
|
||||||
|
"windows_i686_msvc 0.53.1",
|
||||||
|
"windows_x86_64_gnu 0.53.1",
|
||||||
|
"windows_x86_64_gnullvm 0.53.1",
|
||||||
|
"windows_x86_64_msvc 0.53.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -772,48 +683,96 @@ version = "0.52.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_gnullvm"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_aarch64_msvc"
|
name = "windows_aarch64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_aarch64_msvc"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnu"
|
name = "windows_i686_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnu"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_gnullvm"
|
name = "windows_i686_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_gnullvm"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_i686_msvc"
|
name = "windows_i686_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_i686_msvc"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnu"
|
name = "windows_x86_64_gnu"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnu"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_gnullvm"
|
name = "windows_x86_64_gnullvm"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_gnullvm"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows_x86_64_msvc"
|
name = "windows_x86_64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows_x86_64_msvc"
|
||||||
|
version = "0.53.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wit-bindgen-rt"
|
name = "wit-bindgen-rt"
|
||||||
version = "0.33.0"
|
version = "0.33.0"
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ members = ["crates/*"]
|
|||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.7.4"
|
version = "0.8.2"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
mad = { path = "crates/mad" }
|
mad = { path = "crates/mad" }
|
||||||
|
|||||||
159
README.md
159
README.md
@@ -1,39 +1,92 @@
|
|||||||
# MAD
|
# MAD - Lifecycle Manager for Rust Applications
|
||||||
|
|
||||||
Mad is a life-cycle manager for long running rust operations.
|
[](https://crates.io/crates/notmad)
|
||||||
|
[](https://docs.rs/notmad)
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
- Webservers
|
## Overview
|
||||||
- Queue bindings
|
|
||||||
- gRPC servers etc
|
|
||||||
- Cron runners
|
|
||||||
|
|
||||||
It is supposed to be the main thing the application runs, and everything from it is spawned and managed by it.
|
MAD is a robust lifecycle manager designed for long-running Rust operations. It provides a simple, composable way to manage multiple concurrent services within your application, handling graceful startup and shutdown automatically.
|
||||||
|
|
||||||
|
### Perfect for:
|
||||||
|
- 🌐 Web servers
|
||||||
|
- 📨 Queue consumers and message processors
|
||||||
|
- 🔌 gRPC servers
|
||||||
|
- ⏰ Cron job runners
|
||||||
|
- 🔄 Background workers
|
||||||
|
- 📡 Any long-running async operations
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Component-based architecture** - Build your application from composable, reusable components
|
||||||
|
- **Graceful shutdown** - Automatic handling of shutdown signals with proper cleanup
|
||||||
|
- **Concurrent execution** - Run multiple components in parallel with tokio
|
||||||
|
- **Error handling** - Built-in error propagation and logging
|
||||||
|
- **Cancellation tokens** - Coordinate shutdown across all components
|
||||||
|
- **Minimal boilerplate** - Focus on your business logic, not lifecycle management
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add MAD to your `Cargo.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
notmad = "0.7.5"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
async-trait = "0.1"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
Here's a simple example of a component that simulates a long-running server:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
struct WaitServer {}
|
use mad::{Component, Mad};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
// Define your component
|
||||||
|
struct WebServer {
|
||||||
|
port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Component for WaitServer {
|
impl Component for WebServer {
|
||||||
fn name(&self) -> Option<String> {
|
fn name(&self) -> Option<String> {
|
||||||
Some("NeverEndingRun".into())
|
Some(format!("WebServer on port {}", self.port))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run(&self, cancellation: CancellationToken) -> Result<(), mad::MadError> {
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), mad::MadError> {
|
||||||
let millis_wait = rand::thread_rng().gen_range(50..1000);
|
println!("Starting web server on port {}", self.port);
|
||||||
|
|
||||||
// Simulates a server running for some time. Is normally supposed to be futures blocking indefinitely
|
// Your server logic here
|
||||||
tokio::time::sleep(std::time::Duration::from_millis(millis_wait)).await;
|
// The cancellation token will be triggered on shutdown
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
println!("Shutting down web server");
|
||||||
|
}
|
||||||
|
_ = self.serve() => {
|
||||||
|
println!("Server stopped");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WebServer {
|
||||||
|
async fn serve(&self) {
|
||||||
|
// Simulate a running server
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(3600)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
// Build and run your application
|
||||||
Mad::builder()
|
Mad::builder()
|
||||||
.add(WaitServer {})
|
.add(WebServer { port: 8080 })
|
||||||
.add(WaitServer {})
|
.add(WebServer { port: 8081 }) // You can add multiple instances
|
||||||
.add(WaitServer {})
|
|
||||||
.run()
|
.run()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -41,11 +94,75 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Advanced Usage
|
||||||
|
|
||||||
|
### Custom Components
|
||||||
|
|
||||||
|
Components can be anything that implements the `Component` trait:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use mad::{Component, Mad};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
struct QueueProcessor {
|
||||||
|
queue_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for QueueProcessor {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("QueueProcessor-{}", self.queue_name))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), mad::MadError> {
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
// Process messages from queue
|
||||||
|
self.process_next_message().await?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Handling
|
||||||
|
|
||||||
|
MAD provides comprehensive error handling through the `MadError` type with automatic conversion from `anyhow::Error`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), mad::MadError> {
|
||||||
|
// Errors automatically convert from anyhow::Error to MadError
|
||||||
|
database_operation().await?;
|
||||||
|
|
||||||
|
// Or return explicit errors
|
||||||
|
if some_condition {
|
||||||
|
return Err(anyhow::anyhow!("Something went wrong").into());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Can be found (here)[crates/mad/examples]
|
Check out the [examples directory](crates/mad/examples) for more detailed examples:
|
||||||
|
|
||||||
- basic
|
- **basic** - Simple component lifecycle
|
||||||
- fn
|
- **fn** - Using functions as components
|
||||||
- signals
|
- **signals** - Handling system signals
|
||||||
- error_log
|
- **error_log** - Error handling and logging
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
||||||
|
|
||||||
|
## Author
|
||||||
|
|
||||||
|
Created and maintained by [kjuulh](https://github.com/kjuulh)
|
||||||
|
|
||||||
|
## Repository
|
||||||
|
|
||||||
|
Find the source code at [https://github.com/kjuulh/mad](https://github.com/kjuulh/mad)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ license = "MIT"
|
|||||||
repository = "https://github.com/kjuulh/mad"
|
repository = "https://github.com/kjuulh/mad"
|
||||||
authors = ["kjuulh"]
|
authors = ["kjuulh"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
readme = "../../README.md"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
|||||||
18
crates/mad/examples/comprehensive/Cargo.toml
Normal file
18
crates/mad/examples/comprehensive/Cargo.toml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
[package]
|
||||||
|
name = "mad-comprehensive-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "comprehensive"
|
||||||
|
path = "main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
notmad = { path = "../.." }
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tokio-util = "0.7"
|
||||||
|
async-trait = "0.1"
|
||||||
|
anyhow = "1"
|
||||||
|
tracing = "0.1"
|
||||||
|
tracing-subscriber = "0.3"
|
||||||
|
rand = "0.8"
|
||||||
332
crates/mad/examples/comprehensive/main.rs
Normal file
332
crates/mad/examples/comprehensive/main.rs
Normal file
@@ -0,0 +1,332 @@
|
|||||||
|
//! Comprehensive example demonstrating MAD's full capabilities.
|
||||||
|
//!
|
||||||
|
//! This example shows:
|
||||||
|
//! - Multiple component types (struct, closure, conditional)
|
||||||
|
//! - Component lifecycle (setup, run, close)
|
||||||
|
//! - Error handling and propagation
|
||||||
|
//! - Graceful shutdown with cancellation tokens
|
||||||
|
//! - Concurrent component execution
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use notmad::{Component, Mad, MadError};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
use tokio::time::{Duration, interval};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use tracing::{error, info, warn};
|
||||||
|
|
||||||
|
/// A web server component that simulates handling HTTP requests.
|
||||||
|
struct WebServer {
|
||||||
|
port: u16,
|
||||||
|
request_count: Arc<AtomicUsize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for WebServer {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("web-server-{}", self.port))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup(&self) -> Result<(), MadError> {
|
||||||
|
info!("Setting up web server on port {}", self.port);
|
||||||
|
// In a real application, you might:
|
||||||
|
// - Bind to the port
|
||||||
|
// - Set up TLS certificates
|
||||||
|
// - Initialize middleware
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
info!("Web server on port {} is ready", self.port);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
info!("Web server on port {} started", self.port);
|
||||||
|
let mut interval = interval(Duration::from_secs(1));
|
||||||
|
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
info!("Web server on port {} received shutdown signal", self.port);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
// Simulate handling requests
|
||||||
|
let count = self.request_count.fetch_add(1, Ordering::Relaxed);
|
||||||
|
info!("Server on port {} handled request #{}", self.port, count + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn close(&self) -> Result<(), MadError> {
|
||||||
|
info!("Shutting down web server on port {}", self.port);
|
||||||
|
// In a real application, you might:
|
||||||
|
// - Drain existing connections
|
||||||
|
// - Save server state
|
||||||
|
// - Close database connections
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
|
let total = self.request_count.load(Ordering::Relaxed);
|
||||||
|
info!(
|
||||||
|
"Web server on port {} shut down. Total requests handled: {}",
|
||||||
|
self.port, total
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A background job processor that simulates processing tasks from a queue.
|
||||||
|
struct JobProcessor {
|
||||||
|
queue_name: String,
|
||||||
|
processing_interval: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for JobProcessor {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("job-processor-{}", self.queue_name))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup(&self) -> Result<(), MadError> {
|
||||||
|
info!("Connecting to queue: {}", self.queue_name);
|
||||||
|
// Simulate connecting to a message queue
|
||||||
|
tokio::time::sleep(Duration::from_millis(150)).await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
info!("Job processor for {} started", self.queue_name);
|
||||||
|
let mut interval = interval(self.processing_interval);
|
||||||
|
let mut job_count = 0;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
info!("Job processor for {} stopping...", self.queue_name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
job_count += 1;
|
||||||
|
info!("Processing job #{} from {}", job_count, self.queue_name);
|
||||||
|
|
||||||
|
// Simulate job processing
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
|
||||||
|
// Simulate occasional errors (but don't fail the component)
|
||||||
|
if job_count % 10 == 0 {
|
||||||
|
warn!("Job #{} from {} required retry", job_count, self.queue_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info!(
|
||||||
|
"Job processor for {} processed {} jobs",
|
||||||
|
self.queue_name, job_count
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn close(&self) -> Result<(), MadError> {
|
||||||
|
info!("Disconnecting from queue: {}", self.queue_name);
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A health check component that monitors system health.
|
||||||
|
struct HealthChecker {
|
||||||
|
check_interval: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for HealthChecker {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some("health-checker".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
info!("Health checker started");
|
||||||
|
let mut interval = interval(self.check_interval);
|
||||||
|
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
info!("Health checker stopping...");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
// Simulate health checks
|
||||||
|
let cpu_usage = rand::random::<f32>() * 100.0;
|
||||||
|
let memory_usage = rand::random::<f32>() * 100.0;
|
||||||
|
|
||||||
|
info!("System health: CPU={:.1}%, Memory={:.1}%", cpu_usage, memory_usage);
|
||||||
|
|
||||||
|
if cpu_usage > 90.0 {
|
||||||
|
warn!("High CPU usage detected: {:.1}%", cpu_usage);
|
||||||
|
}
|
||||||
|
if memory_usage > 90.0 {
|
||||||
|
warn!("High memory usage detected: {:.1}%", memory_usage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A component that will fail after some time to demonstrate error handling.
|
||||||
|
struct FailingComponent {
|
||||||
|
fail_after: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for FailingComponent {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some("failing-component".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
info!(
|
||||||
|
"Failing component started (will fail after {:?})",
|
||||||
|
self.fail_after
|
||||||
|
);
|
||||||
|
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
info!("Failing component cancelled before failure");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
_ = tokio::time::sleep(self.fail_after) => {
|
||||||
|
error!("Failing component encountered an error!");
|
||||||
|
Err(anyhow::anyhow!("Simulated component failure").into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Debug component that logs system status periodically.
|
||||||
|
struct DebugComponent;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for DebugComponent {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some("debug-component".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancel: CancellationToken) -> Result<(), MadError> {
|
||||||
|
info!("Debug mode enabled - verbose logging active");
|
||||||
|
let mut interval = interval(Duration::from_secs(5));
|
||||||
|
|
||||||
|
while !cancel.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel.cancelled() => break,
|
||||||
|
_ = interval.tick() => {
|
||||||
|
info!("DEBUG: System running normally");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Debug component shutting down");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
// Initialize tracing for logging
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_target(false)
|
||||||
|
.without_time()
|
||||||
|
.init();
|
||||||
|
|
||||||
|
info!("Starting comprehensive MAD example application");
|
||||||
|
|
||||||
|
// Check if we should enable the failing component
|
||||||
|
let enable_failure_demo = std::env::var("ENABLE_FAILURE").is_ok();
|
||||||
|
|
||||||
|
// Check if we should enable debug mode
|
||||||
|
let debug_mode = std::env::var("DEBUG").is_ok();
|
||||||
|
|
||||||
|
// Shared state for demonstration
|
||||||
|
let request_count = Arc::new(AtomicUsize::new(0));
|
||||||
|
|
||||||
|
// Build and run the application
|
||||||
|
let result = Mad::builder()
|
||||||
|
// Add web servers
|
||||||
|
.add(WebServer {
|
||||||
|
port: 8080,
|
||||||
|
request_count: request_count.clone(),
|
||||||
|
})
|
||||||
|
.add(WebServer {
|
||||||
|
port: 8081,
|
||||||
|
request_count: request_count.clone(),
|
||||||
|
})
|
||||||
|
// Add job processors
|
||||||
|
.add(JobProcessor {
|
||||||
|
queue_name: "high-priority".to_string(),
|
||||||
|
processing_interval: Duration::from_secs(2),
|
||||||
|
})
|
||||||
|
.add(JobProcessor {
|
||||||
|
queue_name: "low-priority".to_string(),
|
||||||
|
processing_interval: Duration::from_secs(5),
|
||||||
|
})
|
||||||
|
// Add health checker
|
||||||
|
.add(HealthChecker {
|
||||||
|
check_interval: Duration::from_secs(3),
|
||||||
|
})
|
||||||
|
// Conditionally add a debug component using add_fn
|
||||||
|
.add_conditional(debug_mode, DebugComponent)
|
||||||
|
// Conditionally add failing component to demonstrate error handling
|
||||||
|
.add_conditional(
|
||||||
|
enable_failure_demo,
|
||||||
|
FailingComponent {
|
||||||
|
fail_after: Duration::from_secs(10),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
// Add a simple metrics reporter using add_fn
|
||||||
|
.add_fn(|cancel: CancellationToken| async move {
|
||||||
|
info!("Metrics reporter started");
|
||||||
|
let mut interval = interval(Duration::from_secs(10));
|
||||||
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
|
while !cancel.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel.cancelled() => break,
|
||||||
|
_ = interval.tick() => {
|
||||||
|
let uptime = start.elapsed();
|
||||||
|
info!("Application uptime: {:?}", uptime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Metrics reporter stopped");
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
// Configure graceful shutdown timeout
|
||||||
|
.cancellation(Some(Duration::from_secs(5)))
|
||||||
|
// Run the application
|
||||||
|
.run()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(()) => {
|
||||||
|
info!("Application shut down successfully");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Application failed: {}", e);
|
||||||
|
|
||||||
|
// Check if it's an aggregate error with multiple failures
|
||||||
|
if let MadError::AggregateError(ref agg) = e {
|
||||||
|
error!("Multiple component failures detected:");
|
||||||
|
for (i, err) in agg.get_errors().iter().enumerate() {
|
||||||
|
error!(" {}. {}", i + 1, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(e.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
crates/mad/examples/multi_service/Cargo.toml
Normal file
15
crates/mad/examples/multi_service/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "mad-multi-service-example"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "multi_service"
|
||||||
|
path = "main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
notmad = { path = "../.." }
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tokio-util = "0.7"
|
||||||
|
async-trait = "0.1"
|
||||||
|
anyhow = "1"
|
||||||
214
crates/mad/examples/multi_service/main.rs
Normal file
214
crates/mad/examples/multi_service/main.rs
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
//! Example demonstrating running multiple services with MAD.
|
||||||
|
//!
|
||||||
|
//! This example shows how to run a web server, queue processor, and
|
||||||
|
//! scheduled task together, with graceful shutdown on Ctrl+C.
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use notmad::{Component, Mad, MadError};
|
||||||
|
use tokio::time::{Duration, interval};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
/// A simple web server component.
|
||||||
|
///
|
||||||
|
/// In a real application, this would bind to a port and handle HTTP requests.
|
||||||
|
/// Here we simulate it by periodically logging that we're "handling" requests.
|
||||||
|
struct WebServer {
|
||||||
|
port: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for WebServer {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("WebServer:{}", self.port))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn setup(&self) -> Result<(), MadError> {
|
||||||
|
println!("[{}] Binding to port...", self.name().unwrap());
|
||||||
|
// Simulate server setup time
|
||||||
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
println!("[{}] Ready to accept connections", self.name().unwrap());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
println!("[{}] Server started", self.name().unwrap());
|
||||||
|
|
||||||
|
// Simulate handling requests until shutdown
|
||||||
|
let mut request_id = 0;
|
||||||
|
let mut interval = interval(Duration::from_secs(2));
|
||||||
|
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
println!("[{}] Shutdown signal received", self.name().unwrap());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
request_id += 1;
|
||||||
|
println!("[{}] Handling request #{}", self.name().unwrap(), request_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn close(&self) -> Result<(), MadError> {
|
||||||
|
println!("[{}] Closing connections...", self.name().unwrap());
|
||||||
|
// Simulate graceful connection drain
|
||||||
|
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||||
|
println!("[{}] Server stopped", self.name().unwrap());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A queue processor that consumes messages from a queue.
|
||||||
|
///
|
||||||
|
/// This simulates processing messages from a message queue like
|
||||||
|
/// RabbitMQ, Kafka, or AWS SQS.
|
||||||
|
struct QueueProcessor {
|
||||||
|
queue_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for QueueProcessor {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("QueueProcessor:{}", self.queue_name))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
println!("[{}] Started processing", self.name().unwrap());
|
||||||
|
|
||||||
|
let mut message_count = 0;
|
||||||
|
|
||||||
|
// Process messages until shutdown
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
// Simulate waiting for and processing a message
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
println!("[{}] Stopping message processing", self.name().unwrap());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = tokio::time::sleep(Duration::from_secs(1)) => {
|
||||||
|
message_count += 1;
|
||||||
|
println!("[{}] Processed message #{}", self.name().unwrap(), message_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"[{}] Processed {} messages total",
|
||||||
|
self.name().unwrap(),
|
||||||
|
message_count
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A scheduled task that runs periodically.
|
||||||
|
///
|
||||||
|
/// This could be used for tasks like:
|
||||||
|
/// - Cleaning up old data
|
||||||
|
/// - Generating reports
|
||||||
|
/// - Syncing with external systems
|
||||||
|
struct ScheduledTask {
|
||||||
|
task_name: String,
|
||||||
|
interval_secs: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl Component for ScheduledTask {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(format!("ScheduledTask:{}", self.task_name))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, cancellation: CancellationToken) -> Result<(), MadError> {
|
||||||
|
println!(
|
||||||
|
"[{}] Scheduled to run every {} seconds",
|
||||||
|
self.name().unwrap(),
|
||||||
|
self.interval_secs
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut interval = interval(Duration::from_secs(self.interval_secs));
|
||||||
|
let mut run_count = 0;
|
||||||
|
|
||||||
|
while !cancellation.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
println!("[{}] Scheduler stopping", self.name().unwrap());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
run_count += 1;
|
||||||
|
println!("[{}] Executing run #{}", self.name().unwrap(), run_count);
|
||||||
|
|
||||||
|
// Simulate task execution
|
||||||
|
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||||
|
|
||||||
|
println!("[{}] Run #{} completed", self.name().unwrap(), run_count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
println!("Starting multi-service application");
|
||||||
|
println!("Press Ctrl+C to trigger graceful shutdown");
|
||||||
|
println!("----------------------------------------");
|
||||||
|
|
||||||
|
// Build the application with multiple services
|
||||||
|
Mad::builder()
|
||||||
|
// Add a web server on port 8080
|
||||||
|
.add(WebServer { port: 8080 })
|
||||||
|
// Add another web server on port 8081 (e.g., admin interface)
|
||||||
|
.add(WebServer { port: 8081 })
|
||||||
|
// Add queue processors for different queues
|
||||||
|
.add(QueueProcessor {
|
||||||
|
queue_name: "orders".to_string(),
|
||||||
|
})
|
||||||
|
.add(QueueProcessor {
|
||||||
|
queue_name: "notifications".to_string(),
|
||||||
|
})
|
||||||
|
// Add scheduled tasks
|
||||||
|
.add(ScheduledTask {
|
||||||
|
task_name: "cleanup".to_string(),
|
||||||
|
interval_secs: 5,
|
||||||
|
})
|
||||||
|
.add(ScheduledTask {
|
||||||
|
task_name: "report_generator".to_string(),
|
||||||
|
interval_secs: 10,
|
||||||
|
})
|
||||||
|
// Add a monitoring component using a closure
|
||||||
|
.add_fn(|cancel| async move {
|
||||||
|
println!("[Monitor] Starting system monitor");
|
||||||
|
let mut interval = interval(Duration::from_secs(15));
|
||||||
|
|
||||||
|
while !cancel.is_cancelled() {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancel.cancelled() => {
|
||||||
|
println!("[Monitor] Stopping monitor");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ = interval.tick() => {
|
||||||
|
println!("[Monitor] All systems operational");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
// Set graceful shutdown timeout to 3 seconds
|
||||||
|
.cancellation(Some(Duration::from_secs(3)))
|
||||||
|
// Run all components
|
||||||
|
.run()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
println!("----------------------------------------");
|
||||||
|
println!("All services shut down successfully");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
86
crates/mad/examples/nested_errors/main.rs
Normal file
86
crates/mad/examples/nested_errors/main.rs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
use async_trait::async_trait;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
struct NestedErrorComponent {
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl notmad::Component for NestedErrorComponent {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some(self.name.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, _cancellation: CancellationToken) -> Result<(), notmad::MadError> {
|
||||||
|
// Simulate a deeply nested error
|
||||||
|
let io_error = std::io::Error::new(
|
||||||
|
std::io::ErrorKind::PermissionDenied,
|
||||||
|
"access denied to /etc/secret",
|
||||||
|
);
|
||||||
|
|
||||||
|
Err(anyhow::Error::from(io_error)
|
||||||
|
.context("failed to read configuration file")
|
||||||
|
.context("unable to initialize database connection pool")
|
||||||
|
.context(format!("component '{}' startup failed", self.name))
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AnotherFailingComponent;
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl notmad::Component for AnotherFailingComponent {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some("another-component".into())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, _cancellation: CancellationToken) -> Result<(), notmad::MadError> {
|
||||||
|
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||||
|
|
||||||
|
Err(anyhow::anyhow!("network timeout after 30s")
|
||||||
|
.context("failed to connect to external API")
|
||||||
|
.context("service health check failed")
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter("mad=debug")
|
||||||
|
.init();
|
||||||
|
|
||||||
|
let result = notmad::Mad::builder()
|
||||||
|
.add(NestedErrorComponent {
|
||||||
|
name: "database-service".into(),
|
||||||
|
})
|
||||||
|
.add(AnotherFailingComponent)
|
||||||
|
.run()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(()) => println!("Success!"),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("\n=== Error occurred ===");
|
||||||
|
eprintln!("{}", e);
|
||||||
|
|
||||||
|
// Also demonstrate how to walk the error chain manually
|
||||||
|
if let notmad::MadError::AggregateError(ref agg) = e {
|
||||||
|
eprintln!("\n=== Detailed error chains ===");
|
||||||
|
for (i, error) in agg.get_errors().iter().enumerate() {
|
||||||
|
eprintln!("\nComponent {} error chain:", i + 1);
|
||||||
|
if let notmad::MadError::Inner(inner) = error {
|
||||||
|
for (j, cause) in inner.chain().enumerate() {
|
||||||
|
eprintln!(" {}. {}", j + 1, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let notmad::MadError::Inner(ref inner) = e {
|
||||||
|
eprintln!("\n=== Error chain ===");
|
||||||
|
for (i, cause) in inner.chain().enumerate() {
|
||||||
|
eprintln!(" {}. {}", i + 1, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,83 @@
|
|||||||
|
//! # MAD - Lifecycle Manager for Rust Applications
|
||||||
|
//!
|
||||||
|
//! MAD is a robust lifecycle manager designed for long-running Rust operations. It provides
|
||||||
|
//! a simple, composable way to manage multiple concurrent services within your application,
|
||||||
|
//! handling graceful startup and shutdown automatically.
|
||||||
|
//!
|
||||||
|
//! ## Overview
|
||||||
|
//!
|
||||||
|
//! MAD helps you build applications composed of multiple long-running components that need
|
||||||
|
//! to be orchestrated together. It handles:
|
||||||
|
//!
|
||||||
|
//! - **Concurrent execution** of multiple components
|
||||||
|
//! - **Graceful shutdown** with cancellation tokens
|
||||||
|
//! - **Error aggregation** from multiple components
|
||||||
|
//! - **Lifecycle management** with setup, run, and close phases
|
||||||
|
//!
|
||||||
|
//! ## Quick Start
|
||||||
|
//!
|
||||||
|
//! ```rust,no_run
|
||||||
|
//! use notmad::{Component, Mad};
|
||||||
|
//! use async_trait::async_trait;
|
||||||
|
//! use tokio_util::sync::CancellationToken;
|
||||||
|
//!
|
||||||
|
//! struct MyService {
|
||||||
|
//! name: String,
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! #[async_trait]
|
||||||
|
//! impl Component for MyService {
|
||||||
|
//! fn name(&self) -> Option<String> {
|
||||||
|
//! Some(self.name.clone())
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! async fn run(&self, cancellation: CancellationToken) -> Result<(), notmad::MadError> {
|
||||||
|
//! // Your service logic here
|
||||||
|
//! while !cancellation.is_cancelled() {
|
||||||
|
//! // Do work...
|
||||||
|
//! tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
//! }
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! #[tokio::main]
|
||||||
|
//! async fn main() -> anyhow::Result<()> {
|
||||||
|
//! Mad::builder()
|
||||||
|
//! .add(MyService { name: "service-1".into() })
|
||||||
|
//! .add(MyService { name: "service-2".into() })
|
||||||
|
//! .run()
|
||||||
|
//! .await?;
|
||||||
|
//! Ok(())
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! ## Component Lifecycle
|
||||||
|
//!
|
||||||
|
//! Components go through three phases:
|
||||||
|
//!
|
||||||
|
//! 1. **Setup**: Optional initialization phase before components start running
|
||||||
|
//! 2. **Run**: Main execution phase where components perform their work
|
||||||
|
//! 3. **Close**: Optional cleanup phase after components stop
|
||||||
|
//!
|
||||||
|
//! ## Error Handling
|
||||||
|
//!
|
||||||
|
//! MAD provides comprehensive error handling through [`MadError`], which can:
|
||||||
|
//! - Wrap errors from individual components
|
||||||
|
//! - Aggregate multiple errors when several components fail
|
||||||
|
//! - Automatically convert from `anyhow::Error`
|
||||||
|
//!
|
||||||
|
//! ## Shutdown Behavior
|
||||||
|
//!
|
||||||
|
//! MAD handles shutdown gracefully:
|
||||||
|
//! - Responds to SIGTERM and Ctrl+C signals
|
||||||
|
//! - Propagates cancellation tokens to all components
|
||||||
|
//! - Waits for components to finish cleanup
|
||||||
|
//! - Configurable cancellation timeout
|
||||||
|
|
||||||
use futures::stream::FuturesUnordered;
|
use futures::stream::FuturesUnordered;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
use std::{fmt::Display, sync::Arc};
|
use std::{fmt::Display, sync::Arc, error::Error};
|
||||||
use tokio::signal::unix::{SignalKind, signal};
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
@@ -9,23 +86,48 @@ use crate::waiter::Waiter;
|
|||||||
|
|
||||||
mod waiter;
|
mod waiter;
|
||||||
|
|
||||||
|
/// Error type for MAD operations.
|
||||||
|
///
|
||||||
|
/// This enum represents all possible errors that can occur during
|
||||||
|
/// the lifecycle of MAD components.
|
||||||
#[derive(thiserror::Error, Debug)]
|
#[derive(thiserror::Error, Debug)]
|
||||||
pub enum MadError {
|
pub enum MadError {
|
||||||
#[error("component failed: {0}")]
|
/// Generic error wrapper for anyhow errors.
|
||||||
Inner(#[source] anyhow::Error),
|
///
|
||||||
|
/// This variant is used when components return errors via the `?` operator
|
||||||
|
/// or when converting from `anyhow::Error`.
|
||||||
|
#[error(transparent)]
|
||||||
|
Inner(anyhow::Error),
|
||||||
|
|
||||||
#[error("component(s) failed: {run}")]
|
/// Error that occurred during the run phase of a component.
|
||||||
RunError { run: anyhow::Error },
|
#[error(transparent)]
|
||||||
|
RunError {
|
||||||
|
run: anyhow::Error
|
||||||
|
},
|
||||||
|
|
||||||
#[error("component(s) failed: {close}")]
|
/// Error that occurred during the close phase of a component.
|
||||||
CloseError { close: anyhow::Error },
|
#[error("component(s) failed during close")]
|
||||||
|
CloseError {
|
||||||
|
#[source]
|
||||||
|
close: anyhow::Error
|
||||||
|
},
|
||||||
|
|
||||||
#[error("component(s) failed: {0}")]
|
/// Multiple errors from different components.
|
||||||
|
///
|
||||||
|
/// This is used when multiple components fail simultaneously,
|
||||||
|
/// allowing all errors to be reported rather than just the first one.
|
||||||
|
#[error("{0}")]
|
||||||
AggregateError(AggregateError),
|
AggregateError(AggregateError),
|
||||||
|
|
||||||
|
/// Returned when a component doesn't implement the optional setup method.
|
||||||
|
///
|
||||||
|
/// This is not typically an error condition as setup is optional.
|
||||||
#[error("setup not defined")]
|
#[error("setup not defined")]
|
||||||
SetupNotDefined,
|
SetupNotDefined,
|
||||||
|
|
||||||
|
/// Returned when a component doesn't implement the optional close method.
|
||||||
|
///
|
||||||
|
/// This is not typically an error condition as close is optional.
|
||||||
#[error("close not defined")]
|
#[error("close not defined")]
|
||||||
CloseNotDefined,
|
CloseNotDefined,
|
||||||
}
|
}
|
||||||
@@ -36,12 +138,30 @@ impl From<anyhow::Error> for MadError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// Container for multiple errors from different components.
|
||||||
|
///
|
||||||
|
/// When multiple components fail, their errors are collected
|
||||||
|
/// into this struct to provide complete error reporting.
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub struct AggregateError {
|
pub struct AggregateError {
|
||||||
errors: Vec<MadError>,
|
errors: Vec<MadError>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AggregateError {
|
impl AggregateError {
|
||||||
|
/// Returns a slice of all contained errors.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// match result {
|
||||||
|
/// Err(notmad::MadError::AggregateError(agg)) => {
|
||||||
|
/// for error in agg.get_errors() {
|
||||||
|
/// eprintln!("Component error: {}", error);
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// _ => {}
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
pub fn get_errors(&self) -> &[MadError] {
|
pub fn get_errors(&self) -> &[MadError] {
|
||||||
&self.errors
|
&self.errors
|
||||||
}
|
}
|
||||||
@@ -54,20 +174,55 @@ impl Display for AggregateError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.errors.len() == 1 {
|
if self.errors.len() == 1 {
|
||||||
return f.write_str(&self.errors.first().unwrap().to_string());
|
return write!(f, "{}", self.errors[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
f.write_str("MadError::AggregateError: (")?;
|
writeln!(f, "{} component errors occurred:", self.errors.len())?;
|
||||||
|
for (i, error) in self.errors.iter().enumerate() {
|
||||||
|
write!(f, "\n[Component {}] {}", i + 1, error)?;
|
||||||
|
|
||||||
for error in &self.errors {
|
// Print the error chain for each component error
|
||||||
f.write_str(&error.to_string())?;
|
let mut source = error.source();
|
||||||
f.write_str(", ")?;
|
let mut level = 1;
|
||||||
|
while let Some(err) = source {
|
||||||
|
write!(f, "\n {}. {}", level, err)?;
|
||||||
|
source = err.source();
|
||||||
|
level += 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
f.write_str(")")
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The main lifecycle manager for running multiple components.
|
||||||
|
///
|
||||||
|
/// `Mad` orchestrates the lifecycle of multiple components, ensuring they
|
||||||
|
/// start up in order, run concurrently, and shut down gracefully.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::{Component, Mad};
|
||||||
|
/// use async_trait::async_trait;
|
||||||
|
/// use tokio_util::sync::CancellationToken;
|
||||||
|
///
|
||||||
|
/// struct MyComponent;
|
||||||
|
///
|
||||||
|
/// #[async_trait]
|
||||||
|
/// impl Component for MyComponent {
|
||||||
|
/// async fn run(&self, _cancel: CancellationToken) -> Result<(), notmad::MadError> {
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// # async fn example() -> Result<(), notmad::MadError> {
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add(MyComponent)
|
||||||
|
/// .run()
|
||||||
|
/// .await?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub struct Mad {
|
pub struct Mad {
|
||||||
components: Vec<Arc<dyn Component + Send + Sync + 'static>>,
|
components: Vec<Arc<dyn Component + Send + Sync + 'static>>,
|
||||||
|
|
||||||
@@ -80,6 +235,18 @@ struct CompletionResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Mad {
|
impl Mad {
|
||||||
|
/// Creates a new `Mad` builder.
|
||||||
|
///
|
||||||
|
/// This is the entry point for constructing a MAD application.
|
||||||
|
/// Components are added using the builder pattern before calling `run()`.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::Mad;
|
||||||
|
///
|
||||||
|
/// let mut app = Mad::builder();
|
||||||
|
/// ```
|
||||||
pub fn builder() -> Self {
|
pub fn builder() -> Self {
|
||||||
Self {
|
Self {
|
||||||
components: Vec::default(),
|
components: Vec::default(),
|
||||||
@@ -88,12 +255,65 @@ impl Mad {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a component to the MAD application.
|
||||||
|
///
|
||||||
|
/// Components will be set up in the order they are added,
|
||||||
|
/// run concurrently, and closed in the order they were added.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `component` - Any type that implements `Component` or `IntoComponent`
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::{Component, Mad};
|
||||||
|
/// # use async_trait::async_trait;
|
||||||
|
/// # use tokio_util::sync::CancellationToken;
|
||||||
|
/// # struct MyService;
|
||||||
|
/// # #[async_trait]
|
||||||
|
/// # impl Component for MyService {
|
||||||
|
/// # async fn run(&self, _: CancellationToken) -> Result<(), notmad::MadError> { Ok(()) }
|
||||||
|
/// # }
|
||||||
|
///
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add(MyService)
|
||||||
|
/// .add(MyService);
|
||||||
|
/// ```
|
||||||
pub fn add(&mut self, component: impl IntoComponent) -> &mut Self {
|
pub fn add(&mut self, component: impl IntoComponent) -> &mut Self {
|
||||||
self.components.push(component.into_component());
|
self.components.push(component.into_component());
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Conditionally adds a component based on a boolean condition.
|
||||||
|
///
|
||||||
|
/// If the condition is false, a waiter component is added instead,
|
||||||
|
/// which simply waits for cancellation without doing any work.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `condition` - If true, adds the component; if false, adds a waiter
|
||||||
|
/// * `component` - The component to add if condition is true
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::Mad;
|
||||||
|
/// # use notmad::Component;
|
||||||
|
/// # use async_trait::async_trait;
|
||||||
|
/// # use tokio_util::sync::CancellationToken;
|
||||||
|
/// # struct DebugService;
|
||||||
|
/// # #[async_trait]
|
||||||
|
/// # impl Component for DebugService {
|
||||||
|
/// # async fn run(&self, _: CancellationToken) -> Result<(), notmad::MadError> { Ok(()) }
|
||||||
|
/// # }
|
||||||
|
///
|
||||||
|
/// let enable_debug = std::env::var("DEBUG").is_ok();
|
||||||
|
///
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add_conditional(enable_debug, DebugService);
|
||||||
|
/// ```
|
||||||
pub fn add_conditional(&mut self, condition: bool, component: impl IntoComponent) -> &mut Self {
|
pub fn add_conditional(&mut self, condition: bool, component: impl IntoComponent) -> &mut Self {
|
||||||
if condition {
|
if condition {
|
||||||
self.components.push(component.into_component());
|
self.components.push(component.into_component());
|
||||||
@@ -105,12 +325,53 @@ impl Mad {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a waiter component that does nothing but wait for cancellation.
|
||||||
|
///
|
||||||
|
/// This is useful when you need a placeholder component or want
|
||||||
|
/// the application to keep running without any specific work.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # async fn example() {
|
||||||
|
/// use notmad::Mad;
|
||||||
|
///
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add_wait() // Keeps the app running until shutdown signal
|
||||||
|
/// .run()
|
||||||
|
/// .await;
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn add_wait(&mut self) -> &mut Self {
|
pub fn add_wait(&mut self) -> &mut Self {
|
||||||
self.components.push(Waiter::default().into_component());
|
self.components.push(Waiter::default().into_component());
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a closure or function as a component.
|
||||||
|
///
|
||||||
|
/// This is a convenient way to add simple components without
|
||||||
|
/// creating a full struct that implements `Component`.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `f` - A closure that takes a `CancellationToken` and returns a future
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::Mad;
|
||||||
|
/// use tokio_util::sync::CancellationToken;
|
||||||
|
///
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add_fn(|cancel: CancellationToken| async move {
|
||||||
|
/// while !cancel.is_cancelled() {
|
||||||
|
/// println!("Working...");
|
||||||
|
/// tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
/// }
|
||||||
|
/// Ok(())
|
||||||
|
/// });
|
||||||
|
/// ```
|
||||||
pub fn add_fn<F, Fut>(&mut self, f: F) -> &mut Self
|
pub fn add_fn<F, Fut>(&mut self, f: F) -> &mut Self
|
||||||
where
|
where
|
||||||
F: Fn(CancellationToken) -> Fut + Send + Sync + 'static,
|
F: Fn(CancellationToken) -> Fut + Send + Sync + 'static,
|
||||||
@@ -121,12 +382,63 @@ impl Mad {
|
|||||||
self.add(comp)
|
self.add(comp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configures the cancellation timeout behavior.
|
||||||
|
///
|
||||||
|
/// When a shutdown signal is received, MAD will:
|
||||||
|
/// 1. Send cancellation tokens to all components
|
||||||
|
/// 2. Wait for the specified duration
|
||||||
|
/// 3. Force shutdown if components haven't stopped
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `should_cancel` - Duration to wait after cancellation before forcing shutdown.
|
||||||
|
/// Pass `None` to wait indefinitely.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # async fn example() {
|
||||||
|
/// use notmad::Mad;
|
||||||
|
/// use std::time::Duration;
|
||||||
|
///
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .cancellation(Some(Duration::from_secs(30))) // 30 second grace period
|
||||||
|
/// .run()
|
||||||
|
/// .await;
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub fn cancellation(&mut self, should_cancel: Option<std::time::Duration>) -> &mut Self {
|
pub fn cancellation(&mut self, should_cancel: Option<std::time::Duration>) -> &mut Self {
|
||||||
self.should_cancel = should_cancel;
|
self.should_cancel = should_cancel;
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Runs all components until completion or shutdown.
|
||||||
|
///
|
||||||
|
/// This method:
|
||||||
|
/// 1. Calls `setup()` on all components (in order)
|
||||||
|
/// 2. Starts all components concurrently
|
||||||
|
/// 3. Waits for shutdown signal (SIGTERM, Ctrl+C) or component failure
|
||||||
|
/// 4. Sends cancellation to all components
|
||||||
|
/// 5. Calls `close()` on all components (in order)
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * `Ok(())` if all components shut down cleanly
|
||||||
|
/// * `Err(MadError)` if any component fails
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust,no_run
|
||||||
|
/// # use notmad::Mad;
|
||||||
|
/// # async fn example() -> Result<(), notmad::MadError> {
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add_wait()
|
||||||
|
/// .run()
|
||||||
|
/// .await?;
|
||||||
|
/// # Ok(())
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
pub async fn run(&mut self) -> Result<(), MadError> {
|
pub async fn run(&mut self) -> Result<(), MadError> {
|
||||||
tracing::info!("running mad setup");
|
tracing::info!("running mad setup");
|
||||||
|
|
||||||
@@ -289,24 +601,148 @@ async fn signal_unix_terminate() {
|
|||||||
sigterm.recv().await;
|
sigterm.recv().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trait for implementing MAD components.
|
||||||
|
///
|
||||||
|
/// Components represent individual services or tasks that run as part
|
||||||
|
/// of your application. Each component has its own lifecycle with
|
||||||
|
/// optional setup and cleanup phases.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::{Component, MadError};
|
||||||
|
/// use async_trait::async_trait;
|
||||||
|
/// use tokio_util::sync::CancellationToken;
|
||||||
|
///
|
||||||
|
/// struct DatabaseConnection {
|
||||||
|
/// url: String,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[async_trait]
|
||||||
|
/// impl Component for DatabaseConnection {
|
||||||
|
/// fn name(&self) -> Option<String> {
|
||||||
|
/// Some("database".to_string())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// async fn setup(&self) -> Result<(), MadError> {
|
||||||
|
/// println!("Connecting to database...");
|
||||||
|
/// // Initialize connection pool
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// async fn run(&self, cancel: CancellationToken) -> Result<(), MadError> {
|
||||||
|
/// // Keep connection alive, handle queries
|
||||||
|
/// cancel.cancelled().await;
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// async fn close(&self) -> Result<(), MadError> {
|
||||||
|
/// println!("Closing database connection...");
|
||||||
|
/// // Clean up resources
|
||||||
|
/// Ok(())
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
pub trait Component {
|
pub trait Component {
|
||||||
|
/// Returns an optional name for the component.
|
||||||
|
///
|
||||||
|
/// This name is used in logging and error messages to identify
|
||||||
|
/// which component is being processed.
|
||||||
|
///
|
||||||
|
/// # Default
|
||||||
|
///
|
||||||
|
/// Returns `None` if not overridden.
|
||||||
fn name(&self) -> Option<String> {
|
fn name(&self) -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Optional setup phase called before the component starts running.
|
||||||
|
///
|
||||||
|
/// Use this for initialization tasks like:
|
||||||
|
/// - Establishing database connections
|
||||||
|
/// - Loading configuration
|
||||||
|
/// - Preparing resources
|
||||||
|
///
|
||||||
|
/// # Default
|
||||||
|
///
|
||||||
|
/// Returns `MadError::SetupNotDefined` which is handled gracefully.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// If setup fails with an error other than `SetupNotDefined`,
|
||||||
|
/// the entire application will stop before any components start running.
|
||||||
async fn setup(&self) -> Result<(), MadError> {
|
async fn setup(&self) -> Result<(), MadError> {
|
||||||
Err(MadError::SetupNotDefined)
|
Err(MadError::SetupNotDefined)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Main execution phase of the component.
|
||||||
|
///
|
||||||
|
/// This method should contain the primary logic of your component.
|
||||||
|
/// It should respect the cancellation token and shut down gracefully
|
||||||
|
/// when cancellation is requested.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `cancellation_token` - Signal for graceful shutdown
|
||||||
|
///
|
||||||
|
/// # Implementation Guidelines
|
||||||
|
///
|
||||||
|
/// - Check `cancellation_token.is_cancelled()` periodically
|
||||||
|
/// - Use `tokio::select!` with `cancellation_token.cancelled()` for async operations
|
||||||
|
/// - Clean up resources before returning
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Any error returned will trigger shutdown of all other components.
|
||||||
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError>;
|
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError>;
|
||||||
|
|
||||||
|
/// Optional cleanup phase called after the component stops.
|
||||||
|
///
|
||||||
|
/// Use this for cleanup tasks like:
|
||||||
|
/// - Flushing buffers
|
||||||
|
/// - Closing connections
|
||||||
|
/// - Saving state
|
||||||
|
///
|
||||||
|
/// # Default
|
||||||
|
///
|
||||||
|
/// Returns `MadError::CloseNotDefined` which is handled gracefully.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Errors during close are logged but don't prevent other components
|
||||||
|
/// from closing.
|
||||||
async fn close(&self) -> Result<(), MadError> {
|
async fn close(&self) -> Result<(), MadError> {
|
||||||
Err(MadError::CloseNotDefined)
|
Err(MadError::CloseNotDefined)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trait for converting types into components.
|
||||||
|
///
|
||||||
|
/// This trait is automatically implemented for all types that implement
|
||||||
|
/// `Component + Send + Sync + 'static`, allowing them to be added to MAD
|
||||||
|
/// directly without manual conversion.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use notmad::{Component, IntoComponent, Mad};
|
||||||
|
/// # use async_trait::async_trait;
|
||||||
|
/// # use tokio_util::sync::CancellationToken;
|
||||||
|
///
|
||||||
|
/// struct MyService;
|
||||||
|
///
|
||||||
|
/// # #[async_trait]
|
||||||
|
/// # impl Component for MyService {
|
||||||
|
/// # async fn run(&self, _: CancellationToken) -> Result<(), notmad::MadError> { Ok(()) }
|
||||||
|
/// # }
|
||||||
|
///
|
||||||
|
/// // MyService automatically implements IntoComponent
|
||||||
|
/// Mad::builder()
|
||||||
|
/// .add(MyService); // Works directly
|
||||||
|
/// ```
|
||||||
pub trait IntoComponent {
|
pub trait IntoComponent {
|
||||||
|
/// Converts self into an Arc-wrapped component.
|
||||||
fn into_component(self) -> Arc<dyn Component + Send + Sync + 'static>;
|
fn into_component(self) -> Arc<dyn Component + Send + Sync + 'static>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,3 +782,132 @@ where
|
|||||||
self.execute(cancellation_token).await
|
self.execute(cancellation_token).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use anyhow::Context;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_error_chaining_display() {
|
||||||
|
// Test single error with context chain
|
||||||
|
let base_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
|
||||||
|
let error = anyhow::Error::from(base_error)
|
||||||
|
.context("failed to read configuration")
|
||||||
|
.context("unable to initialize database")
|
||||||
|
.context("service startup failed");
|
||||||
|
|
||||||
|
let mad_error = MadError::Inner(error);
|
||||||
|
let display = format!("{}", mad_error);
|
||||||
|
|
||||||
|
// Should display the top-level error message
|
||||||
|
assert!(display.contains("service startup failed"));
|
||||||
|
|
||||||
|
// Test error chain iteration
|
||||||
|
if let MadError::Inner(ref e) = mad_error {
|
||||||
|
let chain: Vec<String> = e.chain().map(|c| c.to_string()).collect();
|
||||||
|
assert_eq!(chain.len(), 4);
|
||||||
|
assert_eq!(chain[0], "service startup failed");
|
||||||
|
assert_eq!(chain[1], "unable to initialize database");
|
||||||
|
assert_eq!(chain[2], "failed to read configuration");
|
||||||
|
assert_eq!(chain[3], "file not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_aggregate_error_display() {
|
||||||
|
let error1 = MadError::Inner(
|
||||||
|
anyhow::anyhow!("database connection failed")
|
||||||
|
.context("failed to connect to PostgreSQL")
|
||||||
|
);
|
||||||
|
|
||||||
|
let error2 = MadError::Inner(
|
||||||
|
anyhow::anyhow!("port already in use")
|
||||||
|
.context("failed to bind to port 8080")
|
||||||
|
.context("web server initialization failed")
|
||||||
|
);
|
||||||
|
|
||||||
|
let aggregate = MadError::AggregateError(AggregateError {
|
||||||
|
errors: vec![error1, error2],
|
||||||
|
});
|
||||||
|
|
||||||
|
let display = format!("{}", aggregate);
|
||||||
|
|
||||||
|
// Check that it shows multiple errors
|
||||||
|
assert!(display.contains("2 component errors occurred"));
|
||||||
|
assert!(display.contains("[Component 1]"));
|
||||||
|
assert!(display.contains("[Component 2]"));
|
||||||
|
|
||||||
|
// Check that context chains are displayed
|
||||||
|
assert!(display.contains("failed to connect to PostgreSQL"));
|
||||||
|
assert!(display.contains("database connection failed"));
|
||||||
|
assert!(display.contains("web server initialization failed"));
|
||||||
|
assert!(display.contains("failed to bind to port 8080"));
|
||||||
|
assert!(display.contains("port already in use"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_single_error_aggregate() {
|
||||||
|
let error = MadError::Inner(anyhow::anyhow!("single error"));
|
||||||
|
let aggregate = AggregateError {
|
||||||
|
errors: vec![error],
|
||||||
|
};
|
||||||
|
|
||||||
|
let display = format!("{}", aggregate);
|
||||||
|
// Single error should be displayed directly
|
||||||
|
assert!(display.contains("single error"));
|
||||||
|
assert!(!display.contains("component errors occurred"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_error_source_chain() {
|
||||||
|
let error = MadError::Inner(
|
||||||
|
anyhow::anyhow!("root cause")
|
||||||
|
.context("middle layer")
|
||||||
|
.context("top layer")
|
||||||
|
);
|
||||||
|
|
||||||
|
// Test that we can access the error chain
|
||||||
|
if let MadError::Inner(ref e) = error {
|
||||||
|
let chain: Vec<String> = e.chain().map(|c| c.to_string()).collect();
|
||||||
|
assert_eq!(chain.len(), 3);
|
||||||
|
assert_eq!(chain[0], "top layer");
|
||||||
|
assert_eq!(chain[1], "middle layer");
|
||||||
|
assert_eq!(chain[2], "root cause");
|
||||||
|
} else {
|
||||||
|
panic!("Expected MadError::Inner");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_component_error_propagation() {
|
||||||
|
struct FailingComponent;
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl Component for FailingComponent {
|
||||||
|
fn name(&self) -> Option<String> {
|
||||||
|
Some("test-component".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run(&self, _cancel: CancellationToken) -> Result<(), MadError> {
|
||||||
|
Err(anyhow::anyhow!("IO error")
|
||||||
|
.context("failed to open file")
|
||||||
|
.context("component initialization failed")
|
||||||
|
.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = Mad::builder()
|
||||||
|
.add(FailingComponent)
|
||||||
|
.cancellation(Some(std::time::Duration::from_millis(100)))
|
||||||
|
.run()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(result.is_err());
|
||||||
|
let error = result.unwrap_err();
|
||||||
|
|
||||||
|
// Check error display
|
||||||
|
let display = format!("{}", error);
|
||||||
|
assert!(display.contains("component initialization failed"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
//! Waiter components for MAD.
|
||||||
|
//!
|
||||||
|
//! This module provides waiter components that simply wait for cancellation
|
||||||
|
//! without performing any work. Useful for keeping the application alive
|
||||||
|
//! or as placeholders in conditional component loading.
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
@@ -5,6 +11,10 @@ use tokio_util::sync::CancellationToken;
|
|||||||
|
|
||||||
use crate::{Component, MadError};
|
use crate::{Component, MadError};
|
||||||
|
|
||||||
|
/// A default waiter component that panics if run.
|
||||||
|
///
|
||||||
|
/// This is used internally as a placeholder that should never
|
||||||
|
/// actually be executed.
|
||||||
pub struct DefaultWaiter {}
|
pub struct DefaultWaiter {}
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Component for DefaultWaiter {
|
impl Component for DefaultWaiter {
|
||||||
@@ -13,6 +23,20 @@ impl Component for DefaultWaiter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A wrapper component that waits for cancellation.
|
||||||
|
///
|
||||||
|
/// Instead of running the wrapped component's logic, this simply
|
||||||
|
/// waits for the cancellation token. This is useful for conditionally
|
||||||
|
/// disabling components while keeping the same structure.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```rust,ignore
|
||||||
|
/// use mad::Waiter;
|
||||||
|
///
|
||||||
|
/// // Instead of running the service, just wait
|
||||||
|
/// let waiter = Waiter::new(service.into_component());
|
||||||
|
/// ```
|
||||||
pub struct Waiter {
|
pub struct Waiter {
|
||||||
comp: Arc<dyn Component + Send + Sync + 'static>,
|
comp: Arc<dyn Component + Send + Sync + 'static>,
|
||||||
}
|
}
|
||||||
@@ -26,6 +50,10 @@ impl Default for Waiter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Waiter {
|
impl Waiter {
|
||||||
|
/// Creates a new waiter that wraps the given component.
|
||||||
|
///
|
||||||
|
/// The wrapped component's name will be used (prefixed with "waiter/"),
|
||||||
|
/// but its run method will not be called.
|
||||||
pub fn new(c: Arc<dyn Component + Send + Sync + 'static>) -> Self {
|
pub fn new(c: Arc<dyn Component + Send + Sync + 'static>) -> Self {
|
||||||
Self { comp: c }
|
Self { comp: c }
|
||||||
}
|
}
|
||||||
@@ -33,6 +61,10 @@ impl Waiter {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Component for Waiter {
|
impl Component for Waiter {
|
||||||
|
/// Returns the name of the waiter, prefixed with "waiter/".
|
||||||
|
///
|
||||||
|
/// If the wrapped component has a name, it will be "waiter/{name}".
|
||||||
|
/// Otherwise, returns "waiter".
|
||||||
fn name(&self) -> Option<String> {
|
fn name(&self) -> Option<String> {
|
||||||
match self.comp.name() {
|
match self.comp.name() {
|
||||||
Some(name) => Some(format!("waiter/{name}")),
|
Some(name) => Some(format!("waiter/{name}")),
|
||||||
@@ -40,6 +72,10 @@ impl Component for Waiter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Waits for cancellation without performing any work.
|
||||||
|
///
|
||||||
|
/// This method simply waits for the cancellation token to be triggered,
|
||||||
|
/// then returns successfully.
|
||||||
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError> {
|
async fn run(&self, cancellation_token: CancellationToken) -> Result<(), MadError> {
|
||||||
cancellation_token.cancelled().await;
|
cancellation_token.cancelled().await;
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
# yaml-language-server: $schema=https://git.front.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
|
# yaml-language-server: $schema=https://git.kjuulh.io/kjuulh/cuddle/raw/branch/main/schemas/base.json
|
||||||
|
|
||||||
base: "git@git.front.kjuulh.io:kjuulh/cuddle-rust-lib-plan.git"
|
base: "git@git.kjuulh.io:kjuulh/cuddle-rust-lib-plan.git"
|
||||||
|
|
||||||
vars:
|
vars:
|
||||||
service: "mad"
|
service: "mad"
|
||||||
registry: kasperhermansen
|
registry: kasperhermansen
|
||||||
|
rust:
|
||||||
|
publish: {}
|
||||||
|
|
||||||
please:
|
please:
|
||||||
project:
|
project:
|
||||||
@@ -12,6 +14,6 @@ please:
|
|||||||
repository: "mad"
|
repository: "mad"
|
||||||
branch: main
|
branch: main
|
||||||
settings:
|
settings:
|
||||||
api_url: "https://git.front.kjuulh.io"
|
api_url: "https://git.kjuulh.io"
|
||||||
actions:
|
actions:
|
||||||
rust:
|
rust:
|
||||||
|
|||||||
Reference in New Issue
Block a user