-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reorganize functionality around clients rather than models
- Loading branch information
Showing
12 changed files
with
432 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# flake8: noqa | ||
from .get import * | ||
from .insert import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import logging | ||
|
||
import aiosqlite | ||
|
||
|
||
async def get_node(db: aiosqlite.Connection, uuid: str) -> int | None: | ||
"""return the primary key if found, otherwise return None""" | ||
|
||
async with db.execute("select id from nodes where uuid = ?", (uuid,)) as cursor: | ||
if cur_node := await cursor.fetchone(): | ||
return cur_node[0] | ||
|
||
return None | ||
|
||
|
||
async def job_exists(db: aiosqlite.Connection, job_id: int) -> bool: | ||
"""return if the job exists in the database""" | ||
|
||
async with db.execute("select id from jobs where job_id = ?", (job_id,)) as cursor: | ||
if await cursor.fetchone(): | ||
logging.warning( | ||
f""" | ||
job {job_id} already in database. | ||
check why multiple requests are being sent. | ||
""" | ||
) | ||
return True | ||
|
||
return False | ||
|
||
|
||
async def ghost_exists(db: aiosqlite.Connection, job_id: int) -> bool: | ||
"""return if the ghost job exists in the database""" | ||
|
||
async with db.execute( | ||
"select id from ghost_jobs where job_id = ?", (job_id,) | ||
) as cursor: | ||
if await cursor.fetchone(): | ||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import aiosqlite | ||
|
||
from gantry.db.get import get_node | ||
|
||
|
||
def insert_dict(table: str, input: dict, ignore=False) -> tuple[str, tuple]: | ||
""" | ||
crafts an sqlite insert statement from a dictionary. | ||
args: | ||
table: name of the table to insert into | ||
input: dictionary of values to insert | ||
ignore: whether to ignore duplicate entries | ||
returns: tuple of (query, values) | ||
""" | ||
|
||
columns = ", ".join(input.keys()) | ||
values = ", ".join(["?" for _ in range(len(input))]) | ||
query = f"INSERT INTO {table} ({columns}) VALUES ({values})" | ||
|
||
if ignore: | ||
query = query.replace("INSERT", "INSERT OR IGNORE") | ||
|
||
# using a tuple of values from the dictionary | ||
values_tuple = tuple(input.values()) | ||
return query, values_tuple | ||
|
||
|
||
async def insert_ghost(db: aiosqlite.Connection, job_id: int) -> None: | ||
"""Inserts a ghost job into the database.""" | ||
|
||
await db.execute(("insert into ghost_jobs (name) values (?)"), (job_id,)) | ||
|
||
|
||
async def insert_node(db: aiosqlite.Connection, node: dict) -> int: | ||
"""Inserts a node into the database.""" | ||
|
||
async with db.execute( | ||
*insert_dict( | ||
"nodes", | ||
node, | ||
# deal with races | ||
ignore=True, | ||
) | ||
) as cursor: | ||
pk = cursor.lastrowid | ||
|
||
if pk == 0: | ||
# the ignore part of the query was triggered, some other call | ||
# must have inserted the node before this one | ||
pk = await get_node(db, node["uuid"]) | ||
|
||
return pk | ||
|
||
|
||
async def insert_job(db: aiosqlite.Connection, job: dict) -> int: | ||
"""Inserts a job into the database.""" | ||
|
||
async with db.execute( | ||
*insert_dict( | ||
"jobs", | ||
job, | ||
# if the job somehow gets added into the db (pod+id being unique) | ||
# then ignore the insert | ||
ignore=True, | ||
) | ||
) as cursor: | ||
return cursor.lastrowid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
# flake8: noqa | ||
from .build import Build | ||
from .vm import VM | ||
from .job import Job |
Oops, something went wrong.