forked from rommapp/romm
-
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.
Add scheduled tasks for library scan + titledb update
- Loading branch information
Georges-Antoine Assi
committed
Aug 23, 2023
1 parent
2e4738a
commit 6a09c2d
Showing
14 changed files
with
208 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,6 @@ backend/romm_test/logs | |
|
||
# service worker | ||
frontend/dev-dist | ||
|
||
# outside data | ||
switch_titledb.json |
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
2 changes: 1 addition & 1 deletion
2
backend/handler/ps2_opl_index.py → backend/handler/fixtures/ps2_opl_index.json
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,4 +1,4 @@ | ||
opl_index = { | ||
{ | ||
"SLES_556.71": { | ||
"Name": "Fifa '14", | ||
"Region": "PAL" | ||
|
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,5 @@ | ||
from rq_scheduler import Scheduler | ||
|
||
from utils.redis import low_prio_queue | ||
|
||
scheduler = Scheduler(queue=low_prio_queue, connection=low_prio_queue.connection) |
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,36 @@ | ||
from utils.redis import redis_connectable | ||
from logger.logger import log | ||
from . import scheduler | ||
from utils.exceptions import SchedulerException | ||
|
||
|
||
async def run(): | ||
from endpoints.scan import scan_platforms | ||
|
||
log.info("Scheduled library scan started...") | ||
await scan_platforms("", False) | ||
log.info("Scheduled library scan done.") | ||
|
||
|
||
def schedule(): | ||
if not redis_connectable: | ||
raise SchedulerException("Redis not connectable, library scan not scheduled.") | ||
|
||
existing_jobs = scheduler.get_jobs(func_name="tasks.scan_library.run") | ||
if existing_jobs: | ||
raise SchedulerException("Library scan already scheduled.") | ||
|
||
return scheduler.cron( | ||
"0 3 * * *", # At 3:00 AM every day | ||
func="tasks.scan_library.run", | ||
repeat=None, | ||
) | ||
|
||
|
||
def unschedule(): | ||
existing_jobs = scheduler.get_jobs(func_name="tasks.scan_library.run") | ||
|
||
if not existing_jobs: | ||
raise SchedulerException("No library scan scheduled.") | ||
|
||
scheduler.cancel(*existing_jobs) |
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,59 @@ | ||
import requests | ||
import os | ||
from pathlib import Path | ||
|
||
from utils.redis import redis_connectable | ||
from logger.logger import log | ||
from typing import Final | ||
from . import scheduler | ||
from utils.exceptions import SchedulerException | ||
|
||
RAW_URL: Final = "https://raw.githubusercontent.com/blawar/titledb/master/US.en.json" | ||
FIXTURE_FILE_PATH = ( | ||
Path(os.path.dirname(__file__)).parent | ||
/ "handler" | ||
/ "fixtures" | ||
/ "switch_titledb.json" | ||
) | ||
|
||
print(FIXTURE_FILE_PATH) | ||
|
||
|
||
async def run(): | ||
log.info("Scheduled TitleDB update started...") | ||
|
||
try: | ||
response = requests.get(RAW_URL) | ||
response.raise_for_status() | ||
|
||
with open(FIXTURE_FILE_PATH, "wb") as fixture: | ||
fixture.write(response.content) | ||
|
||
log.info("TitleDB update done.") | ||
except requests.exceptions.RequestException as e: | ||
log.error("TitleDB update failed.", exc_info=True) | ||
log.error(e) | ||
|
||
|
||
def schedule(): | ||
if not redis_connectable: | ||
raise SchedulerException("Redis not connectable, titleDB update not scheduled.") | ||
|
||
existing_jobs = scheduler.get_jobs(func_name="tasks.update_switch_titledb.run") | ||
if existing_jobs: | ||
raise SchedulerException("TitleDB update already scheduled.") | ||
|
||
return scheduler.cron( | ||
"0 3 * * *", # At 3:00 AM every day | ||
func="tasks.update_switch_titledb.run", | ||
repeat=None, | ||
) | ||
|
||
|
||
def unschedule(): | ||
existing_jobs = scheduler.get_jobs(func_name="tasks.update_switch_titledb.run") | ||
|
||
if not existing_jobs: | ||
raise SchedulerException("No TitleDB update scheduled.") | ||
|
||
scheduler.cancel(*existing_jobs) |
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
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,17 @@ | ||
from redis import Redis, ConnectionError | ||
from rq import Queue | ||
|
||
from config import REDIS_HOST, REDIS_PORT | ||
|
||
|
||
redis_client = Redis(host=REDIS_HOST, port=int(REDIS_PORT), db=0) | ||
redis_url = f"redis://{REDIS_HOST}:{REDIS_PORT}" | ||
|
||
try: | ||
redis_connectable = redis_client.ping() | ||
except ConnectionError: | ||
redis_connectable = False | ||
|
||
high_prio_queue = Queue(name="high", connection=redis_client) | ||
default_queue = Queue(name="default", connection=redis_client) | ||
low_prio_queue = Queue(name="low", connection=redis_client) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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