mirror of
https://github.com/norohind/jubilant-system.git
synced 2025-06-17 06:40:57 +03:00
proper shutdown
This commit is contained in:
parent
9ba4975751
commit
64b10b9856
20
main.py
20
main.py
@ -4,6 +4,7 @@ import sys
|
|||||||
import sql_requests
|
import sql_requests
|
||||||
import utils
|
import utils
|
||||||
from EDMCLogging import get_main_logger
|
from EDMCLogging import get_main_logger
|
||||||
|
import signal
|
||||||
|
|
||||||
logger = get_main_logger()
|
logger = get_main_logger()
|
||||||
db = sqlite3.connect('squads.sqlite')
|
db = sqlite3.connect('squads.sqlite')
|
||||||
@ -11,6 +12,8 @@ db = sqlite3.connect('squads.sqlite')
|
|||||||
with open('sql_schema.sql', 'r', encoding='utf-8') as schema_file:
|
with open('sql_schema.sql', 'r', encoding='utf-8') as schema_file:
|
||||||
db.executescript(''.join(schema_file.readlines()))
|
db.executescript(''.join(schema_file.readlines()))
|
||||||
|
|
||||||
|
shutting_down: bool = False
|
||||||
|
|
||||||
"""
|
"""
|
||||||
TODO:
|
TODO:
|
||||||
1. Hooks for update
|
1. Hooks for update
|
||||||
@ -55,6 +58,12 @@ Two modes:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def shutdown_callback(sig, frame) -> None:
|
||||||
|
logger.info(f'Planning shutdown by {sig} signal')
|
||||||
|
global shutting_down
|
||||||
|
shutting_down = True
|
||||||
|
|
||||||
|
|
||||||
def discover():
|
def discover():
|
||||||
"""Discover new squads
|
"""Discover new squads
|
||||||
|
|
||||||
@ -85,6 +94,10 @@ def discover():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
|
if shutting_down:
|
||||||
|
return
|
||||||
|
|
||||||
id_to_try = id_to_try + 1
|
id_to_try = id_to_try + 1
|
||||||
# logger.debug(f'Starting discover loop iteration, tries: {tries} of {tries_limit}, id to try {id_to_try}, '
|
# logger.debug(f'Starting discover loop iteration, tries: {tries} of {tries_limit}, id to try {id_to_try}, '
|
||||||
# f'failed list: {failed}')
|
# f'failed list: {failed}')
|
||||||
@ -128,6 +141,10 @@ def update(squad_id: int = None, amount_to_update: int = 1):
|
|||||||
squads_id_to_update: list = db.execute(sql_requests.select_squads_to_update, (amount_to_update,)).fetchall()
|
squads_id_to_update: list = db.execute(sql_requests.select_squads_to_update, (amount_to_update,)).fetchall()
|
||||||
|
|
||||||
for single_squad_to_update in squads_id_to_update: # if db is empty, then loop will not happen
|
for single_squad_to_update in squads_id_to_update: # if db is empty, then loop will not happen
|
||||||
|
|
||||||
|
if shutting_down:
|
||||||
|
return
|
||||||
|
|
||||||
id_to_update: int = single_squad_to_update[0]
|
id_to_update: int = single_squad_to_update[0]
|
||||||
logger.debug(f'Updating {id_to_update} ID')
|
logger.debug(f'Updating {id_to_update} ID')
|
||||||
utils.update_squad_info(id_to_update, db)
|
utils.update_squad_info(id_to_update, db)
|
||||||
@ -136,6 +153,9 @@ def update(squad_id: int = None, amount_to_update: int = 1):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
signal.signal(signal.SIGTERM, shutdown_callback)
|
||||||
|
signal.signal(signal.SIGINT, shutdown_callback)
|
||||||
|
|
||||||
def help_cli() -> str:
|
def help_cli() -> str:
|
||||||
return """Possible arguments:
|
return """Possible arguments:
|
||||||
main.py discover
|
main.py discover
|
||||||
|
Loading…
x
Reference in New Issue
Block a user