2021-09-29 01:03:47 +00:00
|
|
|
use std::collections::HashMap;
|
2021-04-09 00:22:17 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2022-06-21 16:17:30 +00:00
|
|
|
use anyhow::Error;
|
2021-09-29 01:03:47 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-04-09 00:22:17 +00:00
|
|
|
use crate::config::Config;
|
|
|
|
use crate::database::Pool;
|
2022-06-14 10:29:14 +00:00
|
|
|
use crate::ethereum::contracts::ContractSet;
|
2022-01-30 16:18:05 +00:00
|
|
|
use crate::ethereum::nft::process_nft_events;
|
2022-01-30 20:58:05 +00:00
|
|
|
use crate::ethereum::subscriptions::check_subscriptions;
|
2021-04-09 00:22:17 +00:00
|
|
|
|
2022-06-21 16:17:30 +00:00
|
|
|
#[derive(Debug, Eq, Hash, PartialEq)]
|
2022-06-20 20:28:46 +00:00
|
|
|
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-14 10:29:14 +00:00
|
|
|
pub fn run(
|
|
|
|
_config: Config,
|
|
|
|
maybe_contract_set: Option<ContractSet>,
|
|
|
|
db_pool: Pool,
|
|
|
|
) -> () {
|
2022-04-12 12:39:32 +00:00
|
|
|
tokio::spawn(async move {
|
2022-06-20 20:28:46 +00:00
|
|
|
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));
|
2021-09-29 01:03:47 +00:00
|
|
|
let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new();
|
2021-04-09 00:22:17 +00:00
|
|
|
loop {
|
|
|
|
interval.tick().await;
|
2021-11-06 12:48:52 +00:00
|
|
|
|
2022-06-20 20:28:46 +00:00
|
|
|
for (task, last_run) in scheduler_state.iter_mut() {
|
|
|
|
if !is_task_ready(last_run, task.period()) {
|
|
|
|
continue;
|
|
|
|
};
|
2022-06-21 16:17:30 +00:00
|
|
|
let task_result = match task {
|
2022-06-20 20:28:46 +00:00
|
|
|
Task::NftMonitor => {
|
|
|
|
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,
|
|
|
|
contract_set.current_block,
|
|
|
|
&db_pool,
|
|
|
|
&mut token_waitlist_map,
|
2022-06-21 16:17:30 +00:00
|
|
|
).await.map_err(Error::from)
|
|
|
|
} else { Ok(()) }
|
2022-06-20 20:28:46 +00:00
|
|
|
},
|
|
|
|
Task::SubscriptionMonitor => {
|
|
|
|
if let Some(contract_set) = maybe_contract_set.as_ref() {
|
|
|
|
check_subscriptions(
|
|
|
|
&contract_set.web3,
|
|
|
|
&contract_set.subscription,
|
|
|
|
contract_set.current_block,
|
|
|
|
&db_pool,
|
2022-06-21 16:17:30 +00:00
|
|
|
).await.map_err(Error::from)
|
|
|
|
} else { Ok(()) }
|
2022-06-20 20:28:46 +00:00
|
|
|
},
|
|
|
|
};
|
2022-06-21 16:17:30 +00:00
|
|
|
task_result.unwrap_or_else(|err| {
|
|
|
|
log::error!("{:?}: {}", task, err);
|
|
|
|
});
|
2022-06-20 20:28:46 +00:00
|
|
|
*last_run = Some(Utc::now());
|
|
|
|
};
|
2021-04-09 00:22:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|