This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Overseer #1152
Merged
Merged
Overseer #1152
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
86247fa
Initial commit
montekki 12ae947
Licenses, spaces, docs
montekki ff6b2a1
Add a spawner
montekki 4cd6e50
Watch spawned subsystems with a FuturesUnordered
montekki 1b11e87
Move the types around a bit
montekki a2ce8e4
Suggested fixes by Max
montekki ae4b0f1
Add a handler to talk to the Overseer
montekki 92989b4
FromOverseer and ToOverseer msgs and stopping
montekki 3c6bf20
Docs and return errors
montekki 30bbc29
Dont broadcast, have add a from field to messages
montekki 564964c
Allow communication between subsystems and outside world
montekki 069e1ba
A message with a oneshot to send result example
montekki f5cd1d6
Remove leftover can_recv_msg
montekki d8faa65
Remove from field from messages
montekki f367849
Dont be generic over stuff
montekki d55fd6e
Gather messages with StreamUnordered
montekki ffb6e64
Fix comments and formatting
montekki eac7503
More docs fixes and an example
montekki 1ddd41f
Apply suggestions from code review
montekki 7462573
Fixes from review
montekki 639bcc7
Dropping a handler results in a flaky test
montekki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "overseer" | ||
| version = "0.1.0" | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| edition = "2018" | ||
|
|
||
| [dependencies] | ||
| futures = "0.3.5" | ||
| log = "0.4.8" | ||
|
|
||
| [dev-dependencies] | ||
| futures = { version = "0.3.5", features = ["thread-pool"] } | ||
| futures-timer = "3.0.2" | ||
| femme = "2.0.1" | ||
| log = "0.4.8" | ||
| kv-log-macro = "1.0.6" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright 2020 Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Shows a basic usage of the `Overseer`: | ||
| //! * Spawning subsystems and subsystem child jobs | ||
| //! * Establishing message passing | ||
|
|
||
| use std::time::Duration; | ||
| use futures::{pending, executor}; | ||
| use futures_timer::Delay; | ||
| use kv_log_macro as log; | ||
|
|
||
| use overseer::{Overseer, Subsystem, SubsystemContext, SpawnedSubsystem}; | ||
|
|
||
| #[derive(Clone, Copy, Debug, Eq, PartialEq, std::hash::Hash)] | ||
| pub enum SubsystemId { | ||
| Subsystem1, | ||
| Subsystem2, | ||
| Subsystem3, | ||
| } | ||
|
|
||
| struct Subsystem1; | ||
|
|
||
| impl Subsystem1 { | ||
| async fn run(mut ctx: SubsystemContext<usize, SubsystemId>) { | ||
| loop { | ||
| match ctx.try_recv().await { | ||
| Ok(Some(msg)) => { | ||
| log::info!("Subsystem1 received message {}", msg); | ||
| } | ||
| Ok(None) => (), | ||
| Err(_) => {} | ||
| } | ||
|
|
||
| Delay::new(Duration::from_secs(1)).await; | ||
| ctx.broadcast_msg(10).await; | ||
| } | ||
| } | ||
|
|
||
| fn new() -> Self { | ||
| Self | ||
| } | ||
| } | ||
|
|
||
| impl Subsystem<usize, SubsystemId> for Subsystem1 { | ||
| fn start(&mut self, ctx: SubsystemContext<usize, SubsystemId>) -> SpawnedSubsystem { | ||
| SpawnedSubsystem(Box::pin(async move { | ||
| Self::run(ctx).await; | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| struct Subsystem2; | ||
|
|
||
| impl Subsystem2 { | ||
| async fn run(mut ctx: SubsystemContext<usize, SubsystemId>) { | ||
| ctx.spawn(Box::pin(async { | ||
| loop { | ||
| log::info!("Job tick"); | ||
| Delay::new(Duration::from_secs(1)).await; | ||
| } | ||
| })).await.unwrap(); | ||
|
|
||
| loop { | ||
| match ctx.try_recv().await { | ||
| Ok(Some(msg)) => { | ||
| log::info!("Subsystem2 received message {}", msg); | ||
| continue; | ||
| } | ||
| Ok(None) => { pending!(); } | ||
| Err(_) => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn new() -> Self { | ||
| Self | ||
| } | ||
| } | ||
|
|
||
| impl Subsystem<usize, SubsystemId> for Subsystem2 { | ||
| fn start(&mut self, ctx: SubsystemContext<usize, SubsystemId>) -> SpawnedSubsystem { | ||
| SpawnedSubsystem(Box::pin(async move { | ||
| Self::run(ctx).await; | ||
| })) | ||
| } | ||
| } | ||
|
|
||
| struct Subsystem3; | ||
|
|
||
| impl Subsystem<usize, SubsystemId> for Subsystem3 { | ||
| fn start(&mut self, mut ctx: SubsystemContext<usize, SubsystemId>) -> SpawnedSubsystem { | ||
| SpawnedSubsystem(Box::pin(async move { | ||
| // TODO: ctx actually has to be used otherwise the channels are dropped | ||
| loop { | ||
| // ignore all incoming msgs | ||
| while let Ok(Some(_)) = ctx.try_recv().await { | ||
| } | ||
| log::info!("Subsystem3 tick"); | ||
| Delay::new(Duration::from_secs(1)).await; | ||
|
|
||
| pending!(); | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| fn can_recv_msg(&self, _msg: &usize) -> bool { false } | ||
| } | ||
|
|
||
| fn main() { | ||
| femme::with_level(femme::LevelFilter::Trace); | ||
| let spawner = executor::ThreadPool::new().unwrap(); | ||
|
|
||
| futures::executor::block_on(async { | ||
| let subsystems: Vec<(SubsystemId, Box<dyn Subsystem<usize, SubsystemId> + Send>)> = vec![ | ||
| (SubsystemId::Subsystem1, Box::new(Subsystem1::new())), | ||
| (SubsystemId::Subsystem2, Box::new(Subsystem2::new())), | ||
| ]; | ||
|
|
||
| let overseer = Overseer::new(subsystems, spawner); | ||
| overseer.run().await; | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.