system: run and return exit code on stop

This adds an additional running method to `System`, which allows
consumers to retrieve the stopping code.
Additionally, the existing `run()` is reworked to use that
internally, ensuring both codepaths are tested and aligned in
behavior.
This commit is contained in:
Luca BRUNO 2021-11-05 11:02:29 +00:00
parent 81ba7cafaa
commit e458c04ec8
No known key found for this signature in database
GPG Key ID: A9834A2252078E4E
2 changed files with 18 additions and 14 deletions

View File

@ -2,6 +2,9 @@
## Unreleased - 2021-xx-xx
* Add `System::run_until_stop` to allow retrieving the exit code on stop. [#411]
[#411]: https://github.com/actix/actix-net/pull/411
## 2.4.0 - 2021-11-05
* Add `Arbiter::try_current` for situations where thread may or may not have Arbiter context. [#408]

View File

@ -190,23 +190,24 @@ pub struct SystemRunner {
impl SystemRunner {
/// Starts event loop and will return once [System] is [stopped](System::stop).
pub fn run(self) -> io::Result<()> {
// run loop
let code = self.run_until_stop()?;
match code {
0 => Ok(()),
nonzero => Err(io::Error::new(
io::ErrorKind::Other,
format!("Non-zero exit code: {}", nonzero),
)),
}
}
/// Runs the event loop until [stopped](System::stop_with_code), returning the exit code.
pub fn run_until_stop(self) -> io::Result<i32> {
let SystemRunner { rt, stop_rx, .. } = self;
// run loop
match rt.block_on(stop_rx) {
Ok(code) => {
if code != 0 {
Err(io::Error::new(
io::ErrorKind::Other,
format!("Non-zero exit code: {}", code),
))
} else {
Ok(())
}
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
}
rt.block_on(stop_rx)
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}
/// Runs the provided future, blocking the current thread until the future completes.