upkeep based on phase times now
This commit is contained in:
parent
626a578469
commit
574f260a30
@ -4,6 +4,7 @@ exports.up = async knex => {
|
||||
table.index('id');
|
||||
table.timestamps(true, true);
|
||||
table.binary('data').notNullable();
|
||||
table.timestamp('upkeep');
|
||||
table.boolean('finished')
|
||||
.defaultTo(false)
|
||||
.notNullable()
|
||||
@ -26,6 +27,7 @@ exports.up = async knex => {
|
||||
.notNullable()
|
||||
.index();
|
||||
|
||||
table.timestamp('upkeep');
|
||||
});
|
||||
|
||||
await knex.schema.createTable('players', table => {
|
||||
|
||||
@ -16,7 +16,7 @@ tungstenite = "0.6"
|
||||
bcrypt = "0.2"
|
||||
|
||||
dotenv = "0.9.0"
|
||||
postgres = { version = "0.15", features = ["with-uuid"] }
|
||||
postgres = { version = "0.15", features = ["with-uuid", "with-chrono"] }
|
||||
r2d2 = "*"
|
||||
r2d2_postgres = "*"
|
||||
|
||||
|
||||
@ -614,13 +614,14 @@ pub fn game_write(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
||||
let game_bytes = to_vec(&game)?;
|
||||
|
||||
let query = "
|
||||
INSERT INTO games (id, data)
|
||||
VALUES ($1, $2)
|
||||
INSERT INTO games (id, data, upkeep)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id;
|
||||
";
|
||||
|
||||
// no games should be sent to db that are not in progress
|
||||
let result = tx
|
||||
.query(query, &[&game.id, &game_bytes])?;
|
||||
.query(query, &[&game.id, &game_bytes, &game.phase_end])?;
|
||||
|
||||
result.iter().next().ok_or(format_err!("no game written"))?;
|
||||
|
||||
@ -657,17 +658,11 @@ pub fn game_get(tx: &mut Transaction, id: Uuid) -> Result<Game, Error> {
|
||||
}
|
||||
|
||||
pub fn games_need_upkeep(tx: &mut Transaction) -> Result<Vec<Game>, Error> {
|
||||
// let query = "
|
||||
// SELECT data, id
|
||||
// FROM games
|
||||
// WHERE updated_at < now() - interval '5 seconds'
|
||||
// AND finished = false;
|
||||
// ";
|
||||
|
||||
let query = "
|
||||
SELECT data, id
|
||||
FROM games
|
||||
WHERE finished = false
|
||||
AND upkeep < now()
|
||||
FOR UPDATE;
|
||||
";
|
||||
let result = tx
|
||||
@ -799,13 +794,13 @@ pub fn game_update(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
||||
|
||||
let query = "
|
||||
UPDATE games
|
||||
SET data = $1, finished = $2, updated_at = now()
|
||||
WHERE id = $3
|
||||
SET data = $1, finished = $2, upkeep = $3, updated_at = now()
|
||||
WHERE id = $4
|
||||
RETURNING id, data;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&game_bytes, &game.finished(), &game.id])?;
|
||||
.query(query, &[&game_bytes, &game.finished(), &game.phase_end, &game.id])?;
|
||||
|
||||
result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?;
|
||||
|
||||
|
||||
@ -534,13 +534,13 @@ pub fn instance_update(tx: &mut Transaction, instance: Instance) -> Result<Insta
|
||||
|
||||
let query = "
|
||||
UPDATE instances
|
||||
SET data = $1, open = $2, finished = $3, updated_at = now()
|
||||
WHERE id = $4
|
||||
SET data = $1, open = $2, finished = $3, upkeep = $4, updated_at = now()
|
||||
WHERE id = $5
|
||||
RETURNING id, data;
|
||||
";
|
||||
|
||||
let result = tx
|
||||
.query(query, &[&instance_bytes, &instance.open, &instance.finished(), &instance.id])?;
|
||||
.query(query, &[&instance_bytes, &instance.open, &instance.finished(), &instance.phase_end, &instance.id])?;
|
||||
|
||||
result.iter().next().ok_or(err_msg("no instance row returned"))?;
|
||||
|
||||
@ -616,7 +616,8 @@ pub fn instances_need_upkeep(tx: &mut Transaction) -> Result<Vec<Instance>, Erro
|
||||
SELECT data, id
|
||||
FROM instances
|
||||
WHERE finished = false
|
||||
AND id != '00000000-0000-0000-0000-000000000000'
|
||||
AND open = false
|
||||
AND upkeep < now()
|
||||
FOR UPDATE;
|
||||
";
|
||||
|
||||
|
||||
@ -102,7 +102,7 @@ pub fn start() {
|
||||
if let Err(e) = warden(db_connection) {
|
||||
info!("{:?}", e);
|
||||
}
|
||||
sleep(Duration::new(5, 0));
|
||||
sleep(Duration::new(1, 0));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user