-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial supervisor just for logging FTM
- Loading branch information
Showing
10 changed files
with
162 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,121 @@ | ||
use crate::actors::PubsubPort; | ||
use crate::actors::{ | ||
messages::{GiftUnwrapperMessage, RelayEventDispatcherMessage, SupervisorMessage}, | ||
EventEnqueuer, GiftUnwrapper, NostrPort, RelayEventDispatcher, | ||
}; | ||
use anyhow::Result; | ||
use nostr_sdk::prelude::*; | ||
use ractor::{cast, Actor, ActorProcessingErr, ActorRef, SupervisionEvent}; | ||
use tracing::error; | ||
|
||
pub struct Supervisor<T, U> { | ||
_phantom: std::marker::PhantomData<(T, U)>, | ||
} | ||
impl<T, U> Default for Supervisor<T, U> | ||
where | ||
T: NostrPort, | ||
U: PubsubPort, | ||
{ | ||
fn default() -> Self { | ||
Self { | ||
_phantom: std::marker::PhantomData, | ||
} | ||
} | ||
} | ||
|
||
#[ractor::async_trait] | ||
impl<T, U> Actor for Supervisor<T, U> | ||
where | ||
T: NostrPort, | ||
U: PubsubPort, | ||
{ | ||
type Msg = SupervisorMessage; | ||
type State = ActorRef<RelayEventDispatcherMessage>; | ||
type Arguments = (T, U, Keys); | ||
|
||
async fn pre_start( | ||
&self, | ||
myself: ActorRef<Self::Msg>, | ||
(nostr_subscriber, google_publisher, reportinator_keys): (T, U, Keys), | ||
) -> Result<Self::State, ActorProcessingErr> { | ||
// Spawn actors and wire them together | ||
let (event_dispatcher, _event_dispatcher_handle) = Actor::spawn_linked( | ||
Some("event_dispatcher".to_string()), | ||
RelayEventDispatcher::default(), | ||
nostr_subscriber, | ||
myself.get_cell(), | ||
) | ||
.await?; | ||
|
||
let (gift_unwrapper, _gift_unwrapper_handle) = Actor::spawn_linked( | ||
Some("gift_unwrapper".to_string()), | ||
GiftUnwrapper, | ||
reportinator_keys.clone(), | ||
myself.get_cell(), | ||
) | ||
.await?; | ||
|
||
cast!( | ||
event_dispatcher, | ||
RelayEventDispatcherMessage::SubscribeToEventReceived(Box::new(gift_unwrapper.clone())) | ||
)?; | ||
|
||
let (event_enqueuer, _event_enqueuer_handle) = Actor::spawn_linked( | ||
Some("event_enqueuer".to_string()), | ||
EventEnqueuer::default(), | ||
google_publisher, | ||
myself.get_cell(), | ||
) | ||
.await?; | ||
|
||
cast!( | ||
gift_unwrapper, | ||
GiftUnwrapperMessage::SubscribeToEventUnwrapped(Box::new(event_enqueuer)) | ||
)?; | ||
|
||
// Connect as the last message once everything is wired up | ||
cast!(event_dispatcher, RelayEventDispatcherMessage::Connect)?; | ||
|
||
Ok(event_dispatcher) | ||
} | ||
|
||
async fn handle( | ||
&self, | ||
_myself: ActorRef<Self::Msg>, | ||
message: Self::Msg, | ||
state: &mut Self::State, | ||
) -> Result<(), ActorProcessingErr> { | ||
match message { | ||
SupervisorMessage::Publish(report) => { | ||
cast!(state, RelayEventDispatcherMessage::Publish(report))?; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
// For the moment we just log the errors and exit the whole system | ||
async fn handle_supervisor_evt( | ||
&self, | ||
myself: ActorRef<Self::Msg>, | ||
message: SupervisionEvent, | ||
_state: &mut Self::State, | ||
) -> Result<(), ActorProcessingErr> { | ||
match message { | ||
SupervisionEvent::ActorTerminated(who, _state, maybe_msg) => { | ||
if let Some(msg) = maybe_msg { | ||
error!("Actor terminated: {:?}, reason: {}", who, msg); | ||
} else { | ||
error!("Actor terminated: {:?}", who); | ||
} | ||
myself.stop(None) | ||
} | ||
SupervisionEvent::ActorPanicked(dead_actor, panic_msg) => { | ||
error!("Actor panicked: {:?}, panic: {}", dead_actor, panic_msg); | ||
} | ||
SupervisionEvent::ActorStarted(_actor) => {} | ||
SupervisionEvent::ProcessGroupChanged(_group) => {} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod actors; | ||
pub mod adapters; | ||
pub mod domain_objects; | ||
pub mod service_manager; |
This file contains 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 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