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.index('id');
|
||||||
table.timestamps(true, true);
|
table.timestamps(true, true);
|
||||||
table.binary('data').notNullable();
|
table.binary('data').notNullable();
|
||||||
|
table.timestamp('upkeep');
|
||||||
table.boolean('finished')
|
table.boolean('finished')
|
||||||
.defaultTo(false)
|
.defaultTo(false)
|
||||||
.notNullable()
|
.notNullable()
|
||||||
@ -26,6 +27,7 @@ exports.up = async knex => {
|
|||||||
.notNullable()
|
.notNullable()
|
||||||
.index();
|
.index();
|
||||||
|
|
||||||
|
table.timestamp('upkeep');
|
||||||
});
|
});
|
||||||
|
|
||||||
await knex.schema.createTable('players', table => {
|
await knex.schema.createTable('players', table => {
|
||||||
|
|||||||
@ -16,7 +16,7 @@ tungstenite = "0.6"
|
|||||||
bcrypt = "0.2"
|
bcrypt = "0.2"
|
||||||
|
|
||||||
dotenv = "0.9.0"
|
dotenv = "0.9.0"
|
||||||
postgres = { version = "0.15", features = ["with-uuid"] }
|
postgres = { version = "0.15", features = ["with-uuid", "with-chrono"] }
|
||||||
r2d2 = "*"
|
r2d2 = "*"
|
||||||
r2d2_postgres = "*"
|
r2d2_postgres = "*"
|
||||||
|
|
||||||
|
|||||||
@ -614,13 +614,14 @@ pub fn game_write(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
|||||||
let game_bytes = to_vec(&game)?;
|
let game_bytes = to_vec(&game)?;
|
||||||
|
|
||||||
let query = "
|
let query = "
|
||||||
INSERT INTO games (id, data)
|
INSERT INTO games (id, data, upkeep)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2, $3)
|
||||||
RETURNING id;
|
RETURNING id;
|
||||||
";
|
";
|
||||||
|
|
||||||
|
// no games should be sent to db that are not in progress
|
||||||
let result = tx
|
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"))?;
|
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> {
|
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 = "
|
let query = "
|
||||||
SELECT data, id
|
SELECT data, id
|
||||||
FROM games
|
FROM games
|
||||||
WHERE finished = false
|
WHERE finished = false
|
||||||
|
AND upkeep < now()
|
||||||
FOR UPDATE;
|
FOR UPDATE;
|
||||||
";
|
";
|
||||||
let result = tx
|
let result = tx
|
||||||
@ -799,13 +794,13 @@ pub fn game_update(tx: &mut Transaction, game: &Game) -> Result<(), Error> {
|
|||||||
|
|
||||||
let query = "
|
let query = "
|
||||||
UPDATE games
|
UPDATE games
|
||||||
SET data = $1, finished = $2, updated_at = now()
|
SET data = $1, finished = $2, upkeep = $3, updated_at = now()
|
||||||
WHERE id = $3
|
WHERE id = $4
|
||||||
RETURNING id, data;
|
RETURNING id, data;
|
||||||
";
|
";
|
||||||
|
|
||||||
let result = tx
|
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))?;
|
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 = "
|
let query = "
|
||||||
UPDATE instances
|
UPDATE instances
|
||||||
SET data = $1, open = $2, finished = $3, updated_at = now()
|
SET data = $1, open = $2, finished = $3, upkeep = $4, updated_at = now()
|
||||||
WHERE id = $4
|
WHERE id = $5
|
||||||
RETURNING id, data;
|
RETURNING id, data;
|
||||||
";
|
";
|
||||||
|
|
||||||
let result = tx
|
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"))?;
|
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
|
SELECT data, id
|
||||||
FROM instances
|
FROM instances
|
||||||
WHERE finished = false
|
WHERE finished = false
|
||||||
AND id != '00000000-0000-0000-0000-000000000000'
|
AND open = false
|
||||||
|
AND upkeep < now()
|
||||||
FOR UPDATE;
|
FOR UPDATE;
|
||||||
";
|
";
|
||||||
|
|
||||||
|
|||||||
@ -102,7 +102,7 @@ pub fn start() {
|
|||||||
if let Err(e) = warden(db_connection) {
|
if let Err(e) = warden(db_connection) {
|
||||||
info!("{:?}", e);
|
info!("{:?}", e);
|
||||||
}
|
}
|
||||||
sleep(Duration::new(5, 0));
|
sleep(Duration::new(1, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user