forked from 3wordchant/capsul-flask
forest uncommitted changes on multi-host branch
This commit is contained in:
@ -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()
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user