git-next/crates/server/src/actors/webhook/mod.rs

55 lines
1.5 KiB
Rust
Raw Normal View History

// crate::server::actors::webhook
mod router;
mod server;
use std::net::SocketAddr;
use actix::prelude::*;
use git_next_repo_actor::webhook::WebhookMessage;
pub use router::AddWebhookRecipient;
pub use router::WebhookRouter;
use tracing::Instrument;
#[derive(Debug)]
pub struct WebhookActor {
socket_addr: SocketAddr,
span: tracing::Span,
spawn_handle: Option<actix::SpawnHandle>,
message_receiver: Recipient<WebhookMessage>,
}
impl WebhookActor {
pub fn new(socket_addr: SocketAddr, message_receiver: Recipient<WebhookMessage>) -> Self {
let span = tracing::info_span!("WebhookActor");
Self {
socket_addr,
span,
message_receiver,
spawn_handle: None,
}
}
}
impl Actor for WebhookActor {
type Context = actix::Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
let _gaurd = self.span.enter();
let address: Recipient<WebhookMessage> = self.message_receiver.clone();
let server = server::start(self.socket_addr, address);
let spawn_handle = ctx.spawn(server.in_current_span().into_actor(self));
self.spawn_handle.replace(spawn_handle);
}
}
#[derive(Debug, Message)]
#[rtype(result = "()")]
pub struct ShutdownWebhook;
impl Handler<ShutdownWebhook> for WebhookActor {
type Result = ();
fn handle(&mut self, _msg: ShutdownWebhook, ctx: &mut Self::Context) -> Self::Result {
self.spawn_handle.take();
ctx.stop();
}
}