forest uncommitted changes on multi-host branch

This commit is contained in:
2020-12-29 17:34:43 -06:00
parent 44738c78a9
commit d8d6124005
8 changed files with 385 additions and 123 deletions

View File

@ -1,7 +1,12 @@
from nanoid import generate
from flask import current_app
from typing import List
class OnlineHost:
def __init__(self, id: str, url: str):
self.id = id
self.url = url
class DBModel:
def __init__(self, connection, cursor):
@ -267,14 +272,28 @@ class DBModel:
# ------ HOSTS ---------
def authorized_for_host(self, id, token):
def authorized_for_host(self, id, token) -> bool:
self.cursor.execute("SELECT id FROM hosts WHERE id = %s token = %s", (id, token))
return self.cursor.fetchone() != None
def host_heartbeat(self, id):
def host_heartbeat(self, id) -> None:
self.cursor.execute("UPDATE hosts SET last_health_check = NOW() WHERE id = %s", (id,))
self.connection.commit()
def get_online_hosts(self) -> List[OnlineHost]:
self.cursor.execute("SELECT id, https_url FROM hosts WHERE last_health_check > NOW() - INTERVAL '10 seconds'")
return list(map(lambda x: OnlineHost(id=x[0], url=x[1]), self.cursor.fetchall()))
def create_operation(self, online_hosts: List[OnlineHost], email: str, payload: str) -> None:
self.cursor.execute( "INSERT INTO operations (email, payload) VALUES (%s, %s) RETURNING id", (email, payload) )
operation_id = self.cursor.fetchone()[0]
for host in online_hosts:
self.cursor.execute( "INSERT INTO host_operation (host, operation) VALUES (%s, %s)", (host.id, operation_id) )
self.connection.commit()