Skip to content

Commit

Permalink
Fix evtbuzz.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reboot-Codes committed Oct 1, 2024
1 parent ce41ae5 commit cb8df54
Show file tree
Hide file tree
Showing 9 changed files with 322 additions and 180 deletions.
41 changes: 40 additions & 1 deletion clover-hub/src/server/appd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::sync::Arc;
use log::{debug, info};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio_util::sync::CancellationToken;
use url::Url;
use crate::utils::send_ipc_message;

use super::evtbuzz::models::{IPCMessageWithId, CoreUserConfig, Store};

// TODO: Create application manifest schema/models
Expand All @@ -15,6 +17,20 @@ pub async fn appd_main(
cancellation_token: CancellationToken
) {
info!("Starting AppDaemon...");

let init_store = Arc::new(store.clone());
let init_user = Arc::new(user_config.clone());
let (init_from_tx, mut init_from_rx) = unbounded_channel::<IPCMessageWithId>();
cancellation_token.run_until_cancelled(async move {
let _ = send_ipc_message(
&init_store,
&init_user,
init_from_tx,
"clover://appd.clover.reboot-codes.com/status".to_string(),
"finished-init".to_string()
).await;
}).await;

// TODO: Add docker crate to manage applications.

let ipc_recv_token = cancellation_token.clone();
Expand All @@ -36,9 +52,32 @@ pub async fn appd_main(
}
});

let ipc_trans_token = cancellation_token.clone();
let ipc_trans_tx = Arc::new(ipc_tx.clone());
let ipc_trans_handle = tokio::task::spawn(async move {
tokio::select! {
_ = async move {
while let Some(msg) = init_from_rx.recv().await {
match ipc_trans_tx.send(msg) {
Ok(_) => {},
Err(_) => {
debug!("Failed to send message to IPC bus!");
}
}
}
} => {},
_ = ipc_trans_token.cancelled() => {
debug!("ipc_trans exited");
}
}
});

let cleanup_token = cancellation_token.clone();
tokio::select! {
_ = cleanup_token.cancelled() => {
ipc_recv_handle.abort();
ipc_trans_handle.abort();

info!("Cleaning up applications...");
// TODO: Clean up registered applications when server is shutting down.

Expand Down
40 changes: 39 additions & 1 deletion clover-hub/src/server/arbiter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ pub mod models;

use std::sync::Arc;
use log::{debug, info};
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
use tokio_util::sync::CancellationToken;
use url::Url;
use crate::utils::send_ipc_message;

use super::evtbuzz::models::{IPCMessageWithId, CoreUserConfig, Store};

pub async fn arbiter_main(
Expand All @@ -16,6 +18,19 @@ pub async fn arbiter_main(
) {
info!("Starting Arbiter...");

let init_store = Arc::new(store.clone());
let init_user = Arc::new(user_config.clone());
let (init_from_tx, mut init_from_rx) = unbounded_channel::<IPCMessageWithId>();
cancellation_token.run_until_cancelled(async move {
let _ = send_ipc_message(
&init_store,
&init_user,
init_from_tx,
"clover://arbiter.clover.reboot-codes.com/status".to_string(),
"finished-init".to_string()
).await;
}).await;

let ipc_recv_token = cancellation_token.clone();
let ipc_recv_handle = tokio::task::spawn(async move {
tokio::select! {
Expand All @@ -35,9 +50,32 @@ pub async fn arbiter_main(
}
});

let ipc_trans_token = cancellation_token.clone();
let ipc_trans_tx = Arc::new(ipc_tx.clone());
let ipc_trans_handle = tokio::task::spawn(async move {
tokio::select! {
_ = async move {
while let Some(msg) = init_from_rx.recv().await {
match ipc_trans_tx.send(msg) {
Ok(_) => {},
Err(_) => {
debug!("Failed to send message to IPC bus!");
}
}
}
} => {},
_ = ipc_trans_token.cancelled() => {
debug!("ipc_trans exited");
}
}
});

let cleanup_token = cancellation_token.clone();
tokio::select! {
_ = cleanup_token.cancelled() => {
ipc_recv_handle.abort();
ipc_trans_handle.abort();

info!("Cleaning up users...");
// TODO: Clean up registered users when server is shutting down.

Expand Down
Loading

0 comments on commit cb8df54

Please sign in to comment.