Skip to content

Commit

Permalink
use signal handle to save status
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrtye committed Dec 17, 2024
1 parent e0ec383 commit 87c48a5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions apiserver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,22 @@ def get_info_by_ticker(tickers) -> dict[str, str]:

def update_status(symbols) -> None:
info = get_info_by_ticker(symbols)

MAPPING[symbols][0].update(info)
with open(MAPPING[symbols][1], "w") as file:
json.dump(MAPPING[symbols][0], file)


def save_status() -> None:
for symbols in MAPPING.keys():
with open(MAPPING[symbols][1], "w") as file:
json.dump(MAPPING[symbols][0], file)


def handle_sigterm(signum, frame) -> None:
save_status()
print(
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"All status saved before exiting",
)
raise SystemExit(0)


def main() -> None:
Expand All @@ -248,12 +260,13 @@ def main() -> None:
schedule.every().hour.at(f":{str(UPDATE_INTERVAL * 4).zfill(2)}").do(
update_status, symbols=COMMODITIES
)
schedule.every().day.at("10:24").do(save_status)

print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "API server started")

while True:
schedule.run_pending()
time.sleep(10)
time.sleep(60)


if __name__ == "__main__":
Expand Down

0 comments on commit 87c48a5

Please sign in to comment.