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.
- Loading branch information
Georges-Antoine Assi
committed
Aug 26, 2023
1 parent
88c747f
commit 0a756f8
Showing
10 changed files
with
145 additions
and
9 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
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,87 @@ | ||
import os | ||
from datetime import timedelta | ||
from watchdog.observers import Observer | ||
from watchdog.events import FileSystemEventHandler | ||
|
||
from endpoints.scan import scan_platforms | ||
from utils.redis import low_prio_queue | ||
from logger.logger import log | ||
|
||
from config import ( | ||
HIGH_PRIO_STRUCTURE_PATH, | ||
LIBRARY_BASE_PATH, | ||
ENABLE_RESCAN_ON_FILESYSTEM_CHANGE, | ||
RESCAN_ON_FILESYSTEM_CHANGE_DELAY, | ||
) | ||
|
||
path = ( | ||
HIGH_PRIO_STRUCTURE_PATH | ||
if os.path.exists(HIGH_PRIO_STRUCTURE_PATH) | ||
else LIBRARY_BASE_PATH | ||
) | ||
|
||
|
||
class EventHandler(FileSystemEventHandler): | ||
def on_any_event(self, event): | ||
if not ENABLE_RESCAN_ON_FILESYSTEM_CHANGE: | ||
return | ||
|
||
# Ignore .DS_Store files | ||
if event.src_path.endswith(".DS_Store"): | ||
return | ||
|
||
# Ignore modified events | ||
if event.event_type == "modified": | ||
return | ||
|
||
event_src = event.src_path.split(path)[-1] | ||
platform_slug = event_src.split("/")[1] | ||
time_delta = timedelta(minutes=RESCAN_ON_FILESYSTEM_CHANGE_DELAY) | ||
|
||
log.info(f"Filesystem event: {event.event_type} {event_src}") | ||
|
||
low_prio_queue.scheduled_job_registry.remove_jobs() | ||
|
||
# Skip if a scan is already scheduled | ||
for job_id in low_prio_queue.scheduled_job_registry.get_job_ids(): | ||
job = low_prio_queue.fetch_job(job_id) | ||
if ( | ||
job | ||
and job.is_scheduled | ||
and job.func_name == "endpoints.scan.scan_platforms" | ||
): | ||
if job.args[0] == []: | ||
log.info("Full rescan already scheduled") | ||
return | ||
|
||
if platform_slug in job.args[0]: | ||
log.info(f"Scan already scheduled for {platform_slug}") | ||
return | ||
|
||
rescan_in_msg = f"rescanning in {RESCAN_ON_FILESYSTEM_CHANGE_DELAY} minutes." | ||
|
||
# # Any change to a platform directory should trigger a full rescan | ||
if event.is_directory and event_src.count("/") == 1: | ||
log.info(f"Platform directory changed, {rescan_in_msg}") | ||
return low_prio_queue.enqueue_in(time_delta, scan_platforms, []) | ||
|
||
# Otherwise trigger a rescan for the specific platform | ||
log.info(f"Change detected in {platform_slug} folder, {rescan_in_msg}") | ||
return low_prio_queue.enqueue_in( | ||
time_delta, | ||
scan_platforms, | ||
[platform_slug], | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
observer = Observer() | ||
observer.schedule(EventHandler(), path, recursive=True) | ||
observer.start() | ||
|
||
try: | ||
while observer.is_alive(): | ||
observer.join(1) | ||
finally: | ||
observer.stop() | ||
observer.join() |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#!/bin/bash | ||
|
||
cd /back | ||
[[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]] && rqscheduler --host ${REDIS_HOST} --port ${REDIS_PORT} || sleep infinity | ||
[[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]] && python3 scheduler.py || sleep infinity |
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,4 @@ | ||
#!/bin/bash | ||
|
||
cd /back | ||
python3 watcher.py |
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 @@ | ||
#!/bin/bash | ||
|
||
cd /back | ||
[[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]] && rq worker high default low --url redis://${REDIS_HOST}:${REDIS_PORT}/0 || sleep infinity | ||
[[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]] && python3 worker.py || sleep infinity |
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