File size: 2,439 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
use super::CoordinatorWorkerMessage;
use bastion::prelude::*;
use std::thread;
use std::time::Duration;
use std::time::Instant;
const CRONJOB_SYNC_INTERVAL_SECONDS: u64 = 24 * 60 * 60; // 24 hours
const CRONJOB_HEARTBEAT_INTERVAL_SECONDS: u64 = 5 * 60; // 5 minutes
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CronjobWorkerMessage {
#[allow(dead_code)]
DoSomething,
}
pub async fn cronjob_worker(
ctx: BastionContext,
) -> Result<(), ()> {
let mut progress_time = Instant::now();
loop {
let elapsed: u64 = progress_time.elapsed().as_secs();
if elapsed >= CRONJOB_SYNC_INTERVAL_SECONDS {
debug!("cronjob_worker: wake up");
let distributor = Distributor::named("coordinator_worker");
let tell_result = distributor.tell_everyone(CoordinatorWorkerMessage::TriggerSync);
if let Err(error) = tell_result {
error!("Unable to send TriggerSync to coordinator_worker. error: {:?}", error);
}
progress_time = Instant::now();
}
let timeout = Duration::from_secs(CRONJOB_HEARTBEAT_INTERVAL_SECONDS);
let message: SignedMessage = match ctx.try_recv_timeout(timeout).await {
Ok(message) => message,
Err(error) => {
if let ReceiveError::Timeout(_duration) = error {
// debug!("cronjob_worker: timeout happened");
continue;
}
error!("cronjob_worker: Unknown error happened. error: {:?}", error);
continue;
}
};
MessageHandler::new(message)
.on_tell(|message: CronjobWorkerMessage, _| {
println!(
"cronjob_worker: child {}, received broadcast message: {:?}",
ctx.current().id(),
message
);
match message {
CronjobWorkerMessage::DoSomething => {
println!("CronjobWorkerMessage::DoSomething");
thread::sleep(Duration::from_millis(1000));
}
}
})
.on_fallback(|unknown, _sender_addr| {
error!(
"cronjob_worker {}, received an unknown message!:\n{:?}",
ctx.current().id(),
unknown
);
});
}
}
|