50 lines
1.2 KiB
Rust
50 lines
1.2 KiB
Rust
// Db Commons
|
|
use postgres::transaction::Transaction;
|
|
use failure::Error;
|
|
|
|
use game::{games_need_upkeep, game_update, game_write, game_delete};
|
|
use instance::{instances_need_upkeep, instances_idle, instance_update, instance_delete};
|
|
use net::{Db};
|
|
|
|
fn fetch_games(mut tx: Transaction) -> Result<Transaction, Error> {
|
|
let games = games_need_upkeep(&mut tx)?;
|
|
|
|
for mut game in games {
|
|
let game = game.upkeep();
|
|
match game_update(&mut tx, &game) {
|
|
Ok(_) => (),
|
|
Err(e) => {
|
|
info!("{:?}", e);
|
|
game_delete(&mut tx, game.id)?;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(tx)
|
|
}
|
|
|
|
fn fetch_instances(mut tx: Transaction) -> Result<Transaction, Error> {
|
|
for mut instance in instances_need_upkeep(&mut tx)? {
|
|
let (instance, new_games) = instance.upkeep();
|
|
for game in new_games {
|
|
game_write(&mut tx, &game)?;
|
|
}
|
|
instance_update(&mut tx, instance)?;
|
|
}
|
|
|
|
for mut instance in instances_idle(&mut tx)? {
|
|
instance_delete(&mut tx, instance.id)?;
|
|
}
|
|
|
|
Ok(tx)
|
|
}
|
|
|
|
pub fn warden(db: Db) -> Result<(), Error> {
|
|
fetch_games(db.transaction()?)?
|
|
.commit()?;
|
|
|
|
fetch_instances(db.transaction()?)?
|
|
.commit()?;
|
|
|
|
Ok(())
|
|
} |