Skip to content

Commit

Permalink
Creating executor manually to guarantee shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
isFakeAccount committed Mar 24, 2024
1 parent 823ef96 commit 20c783a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
13 changes: 9 additions & 4 deletions backup_and_cheating/back_up_and_cheating.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
from __future__ import annotations

import asyncio
import concurrent.futures
import functools
import sys
import time
from os import getenv
from pathlib import Path
from traceback import format_exc

import aioschedule as schedule
from dotenv import load_dotenv

from backup_drive import backup_databased
from dotenv import load_dotenv

sys.path.append(str(Path(sys.argv[0]).absolute().parent.parent))

from utility_functions import get_mongo_client, get_mongo_collection, send_message_to_admin, create_reddit_instance, send_traceback_to_discord, create_logger
from utility_functions import create_logger, create_reddit_instance, get_mongo_client, get_mongo_collection, send_message_to_admin, send_traceback_to_discord

load_dotenv("../.env")
backup_cheating_logger = create_logger(__name__)
Expand Down Expand Up @@ -53,7 +54,11 @@ async def backup() -> None:
async with get_mongo_client() as mongo_client:
users_collection = await get_mongo_collection("users", mongo_client=mongo_client)
users = await users_collection.find({}).to_list(length=None)
await asyncio.to_thread(backup_databased, users)

loop = asyncio.get_running_loop()
func_call = functools.partial(backup_databased, users)
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
await loop.run_in_executor(pool, func_call)


async def task_scheduler() -> None:
Expand Down
4 changes: 2 additions & 2 deletions backup_and_cheating/backup_drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import Any, Sequence

import json
import ujson
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
Expand Down Expand Up @@ -78,4 +78,4 @@ def build_data_based(data_based: list[dict[str, object]]) -> None:
del user["_id"]

with bz2.open("dataBased.json.bz2", "wb") as fp:
fp.write(json.dumps(data_based, indent=4).encode("utf-8"))
fp.write(ujson.dumps(data_based, indent=4).encode("utf-8"))
2 changes: 2 additions & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pre-commit
python-dotenv
types-PyYAML
types-aiofiles
types-ujson
ujson
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ types-aiofiles==23.2.0.20240311
# via -r requirements.in
types-pyyaml==6.0.12.20240311
# via -r requirements.in
types-ujson==5.9.0.0
# via -r requirements.in
typing-extensions==4.10.0
# via
# aiosqlite
# mypy
ujson==5.9.0
# via -r requirements.in
update-checker==0.18.0
# via asyncpraw
uritemplate==4.1.1
Expand Down

0 comments on commit 20c783a

Please sign in to comment.