fedimovies/src/scheduler.rs

125 lines
3.6 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2021-04-09 00:22:17 +00:00
use std::time::Duration;
use anyhow::Error;
use chrono::{DateTime, Utc};
use uuid::Uuid;
2021-04-09 00:22:17 +00:00
use crate::config::Config;
use crate::database::Pool;
use crate::ethereum::contracts::Blockchain;
use crate::ethereum::nft::process_nft_events;
use crate::ethereum::subscriptions::check_subscriptions;
2021-04-09 00:22:17 +00:00
#[derive(Debug, Eq, Hash, PartialEq)]
enum Task {
NftMonitor,
SubscriptionMonitor,
}
impl Task {
/// Returns task period (in seconds)
fn period(&self) -> i64 {
match self {
Self::NftMonitor => 30,
Self::SubscriptionMonitor => 300,
}
}
}
fn is_task_ready(last_run: &Option<DateTime<Utc>>, period: i64) -> bool {
match last_run {
Some(last_run) => {
let time_passed = Utc::now() - *last_run;
time_passed.num_seconds() >= period
},
None => true,
}
}
2022-06-29 17:30:36 +00:00
async fn nft_monitor_task(
maybe_blockchain: Option<&mut Blockchain>,
db_pool: &Pool,
token_waitlist_map: &mut HashMap<Uuid, DateTime<Utc>>,
) -> Result<(), Error> {
let blockchain = match maybe_blockchain {
Some(blockchain) => blockchain,
None => return Ok(()),
};
let collectible = match &blockchain.contract_set.collectible {
Some(contract) => contract,
None => return Ok(()), // feature not enabled
};
2022-06-29 17:30:36 +00:00
process_nft_events(
&blockchain.contract_set.web3,
&collectible,
2022-06-29 17:30:36 +00:00
&mut blockchain.sync_state,
db_pool,
token_waitlist_map,
).await?;
Ok(())
}
async fn subscription_monitor_task(
maybe_blockchain: Option<&mut Blockchain>,
db_pool: &Pool,
) -> Result<(), Error> {
let blockchain = match maybe_blockchain {
Some(blockchain) => blockchain,
None => return Ok(()),
};
let subscription = match &blockchain.contract_set.subscription {
Some(contract) => contract,
None => return Ok(()), // feature not enabled
};
2022-06-29 17:30:36 +00:00
check_subscriptions(
&blockchain.contract_set.web3,
&subscription,
2022-06-29 17:30:36 +00:00
&mut blockchain.sync_state,
db_pool,
).await.map_err(Error::from)
}
2022-06-14 10:29:14 +00:00
pub fn run(
_config: Config,
mut maybe_blockchain: Option<Blockchain>,
2022-06-14 10:29:14 +00:00
db_pool: Pool,
) -> () {
tokio::spawn(async move {
let mut scheduler_state = HashMap::new();
scheduler_state.insert(Task::NftMonitor, None);
scheduler_state.insert(Task::SubscriptionMonitor, None);
let mut interval = tokio::time::interval(Duration::from_secs(5));
let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new();
2021-04-09 00:22:17 +00:00
loop {
interval.tick().await;
for (task, last_run) in scheduler_state.iter_mut() {
if !is_task_ready(last_run, task.period()) {
continue;
};
let task_result = match task {
Task::NftMonitor => {
2022-06-29 17:30:36 +00:00
nft_monitor_task(
maybe_blockchain.as_mut(),
&db_pool,
&mut token_waitlist_map,
).await
},
Task::SubscriptionMonitor => {
2022-06-29 17:30:36 +00:00
subscription_monitor_task(
maybe_blockchain.as_mut(),
&db_pool,
).await
},
};
task_result.unwrap_or_else(|err| {
log::error!("{:?}: {}", task, err);
});
*last_run = Some(Utc::now());
};
2021-04-09 00:22:17 +00:00
}
});
}