fedimovies/src/scheduler.rs

49 lines
1.6 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
2021-04-09 00:22:17 +00:00
use std::time::Duration;
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::get_contracts;
use crate::ethereum::nft::process_nft_events;
use crate::ethereum::subscriptions::check_subscriptions;
2021-04-09 00:22:17 +00:00
pub fn run(config: Config, db_pool: Pool) -> () {
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_secs(30));
let maybe_contract_set = if let Some(blockchain_config) = &config.blockchain {
// Create blockchain interface
get_contracts(blockchain_config).await
.map_err(|err| log::error!("{}", err))
.ok()
} else {
None
};
let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new();
2021-04-09 00:22:17 +00:00
loop {
interval.tick().await;
if let Some(contract_set) = maybe_contract_set.as_ref() {
// Monitor events only if ethereum integration is enabled
process_nft_events(
&contract_set.web3,
&contract_set.collectible,
&db_pool,
&mut token_waitlist_map,
).await.unwrap_or_else(|err| {
2021-04-09 00:22:17 +00:00
log::error!("{}", err);
});
check_subscriptions(
&contract_set.web3,
&contract_set.subscription,
&db_pool,
).await.unwrap_or_else(|err| {
log::error!("{}", err);
});
2021-04-09 00:22:17 +00:00
}
}
});
}