lookyloo/bin/run_backend.py

107 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2024-01-12 17:15:41 +01:00
from __future__ import annotations
import argparse
import os
import time
from pathlib import Path
from subprocess import Popen
from redis import Redis
from redis.exceptions import ConnectionError
2021-09-07 12:59:31 +02:00
2021-10-18 13:06:43 +02:00
from lookyloo.default import get_homedir, get_socket_path
def check_running(name: str) -> bool:
socket_path = get_socket_path(name)
if not os.path.exists(socket_path):
return False
try:
r = Redis(unix_socket_path=socket_path)
return True if r.ping() else False
except ConnectionError:
return False
2024-01-12 17:15:41 +01:00
def launch_cache(storage_directory: Path | None=None) -> None:
if not storage_directory:
storage_directory = get_homedir()
if not check_running('cache'):
Popen(["./run_redis.sh"], cwd=(storage_directory / 'cache'))
2024-01-12 17:15:41 +01:00
def shutdown_cache(storage_directory: Path | None=None) -> None:
if not storage_directory:
storage_directory = get_homedir()
2021-11-26 18:35:15 +01:00
r = Redis(unix_socket_path=get_socket_path('cache'))
2021-12-02 14:39:32 +01:00
r.shutdown(save=True)
2021-11-26 18:35:15 +01:00
print('Redis cache database shutdown.')
2024-01-12 17:15:41 +01:00
def launch_indexing(storage_directory: Path | None=None) -> None:
if not storage_directory:
storage_directory = get_homedir()
if not check_running('indexing'):
Popen(["./run_redis.sh"], cwd=(storage_directory / 'indexing'))
2024-01-12 17:15:41 +01:00
def shutdown_indexing(storage_directory: Path | None=None) -> None:
if not storage_directory:
storage_directory = get_homedir()
2021-11-26 18:35:15 +01:00
r = Redis(unix_socket_path=get_socket_path('indexing'))
2021-12-02 14:39:32 +01:00
r.shutdown(save=True)
2021-11-26 18:35:15 +01:00
print('Redis indexing database shutdown.')
2024-01-12 17:15:41 +01:00
def launch_all() -> None:
launch_cache()
launch_indexing()
2024-01-12 17:15:41 +01:00
def check_all(stop: bool=False) -> None:
backends: dict[str, bool] = {'cache': False, 'indexing': False}
while True:
2021-12-02 14:39:32 +01:00
for db_name in backends.keys():
try:
2021-12-02 14:39:32 +01:00
backends[db_name] = check_running(db_name)
except Exception:
2021-12-02 14:39:32 +01:00
backends[db_name] = False
if stop:
2021-12-02 14:39:32 +01:00
if not any(running for running in backends.values()):
break
else:
2021-12-02 14:39:32 +01:00
if all(running for running in backends.values()):
break
2021-12-02 14:39:32 +01:00
for db_name, running in backends.items():
if not stop and not running:
print(f"Waiting on {db_name} to start")
if stop and running:
print(f"Waiting on {db_name} to stop")
time.sleep(1)
2024-01-12 17:15:41 +01:00
def stop_all() -> None:
shutdown_cache()
shutdown_indexing()
2024-01-12 17:15:41 +01:00
def main() -> None:
parser = argparse.ArgumentParser(description='Manage backend DBs.')
parser.add_argument("--start", action='store_true', default=False, help="Start all")
parser.add_argument("--stop", action='store_true', default=False, help="Stop all")
parser.add_argument("--status", action='store_true', default=True, help="Show status")
args = parser.parse_args()
if args.start:
launch_all()
if args.stop:
stop_all()
if not args.stop and args.status:
check_all()
if __name__ == '__main__':
main()