mirror of
https://github.com/spalencsar/bearhub.git
synced 2026-07-07 02:24:16 +02:00
[improvements][appimage] AppImage updater daemon replaced by a default Python thread to reduce memory usage
This commit is contained in:
@@ -7,6 +7,7 @@ import subprocess
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
from typing import Set, Type, List
|
||||
|
||||
from bauh.api.abstract.context import ApplicationContext
|
||||
@@ -18,7 +19,7 @@ from bauh.api.abstract.view import MessageType
|
||||
from bauh.api.constants import HOME_PATH
|
||||
from bauh.commons.html import bold
|
||||
from bauh.commons.system import SystemProcess, new_subprocess, ProcessHandler, run_cmd, SimpleProcess
|
||||
from bauh.gems.appimage import query, INSTALLATION_PATH, suggestions, db
|
||||
from bauh.gems.appimage import query, INSTALLATION_PATH, suggestions
|
||||
from bauh.gems.appimage.model import AppImage
|
||||
from bauh.gems.appimage.worker import DatabaseUpdater
|
||||
|
||||
@@ -43,18 +44,19 @@ class AppImageManager(SoftwareManager):
|
||||
self.http_client = context.http_client
|
||||
self.logger = context.logger
|
||||
self.file_downloader = context.file_downloader
|
||||
self.dbs_updater = DatabaseUpdater(http_client=context.http_client, logger=context.logger)
|
||||
self.db_locks = {DB_APPS_PATH: Lock(), DB_RELEASES_PATH: Lock()}
|
||||
self.dbs_updater = DatabaseUpdater(http_client=context.http_client, logger=context.logger, db_locks=self.db_locks)
|
||||
|
||||
def _get_db_connection(self, db_path: str) -> sqlite3.Connection:
|
||||
if os.path.exists(db_path):
|
||||
db.acquire_lock(db_path)
|
||||
self.db_locks[db_path].acquire()
|
||||
return sqlite3.connect(db_path)
|
||||
else:
|
||||
self.logger.warning("Could not get a database connection. File '{}' not found".format(db_path))
|
||||
|
||||
def _close_connection(self, db_path: str, con: sqlite3.Connection):
|
||||
con.close()
|
||||
db.release_lock(db_path)
|
||||
self.db_locks[db_path].release()
|
||||
|
||||
def _gen_app_key(self, app: AppImage):
|
||||
return '{}{}'.format(app.name.lower(), app.github.lower() if app.github else '')
|
||||
@@ -380,9 +382,6 @@ class AppImageManager(SoftwareManager):
|
||||
return False
|
||||
|
||||
def prepare(self):
|
||||
for path in (DB_APPS_PATH, DB_RELEASES_PATH):
|
||||
db.release_lock(path)
|
||||
|
||||
self.dbs_updater.start()
|
||||
|
||||
def list_updates(self, internet_available: bool) -> List[PackageUpdate]:
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import os
|
||||
import time
|
||||
|
||||
|
||||
def acquire_lock(db_path: str):
|
||||
lock_path = db_path + '.lock'
|
||||
while True:
|
||||
if not os.path.exists(lock_path):
|
||||
open(lock_path, 'a').close()
|
||||
break
|
||||
else:
|
||||
time.sleep(0.0001)
|
||||
|
||||
|
||||
def release_lock(db_path: str):
|
||||
lock_path = db_path + '.lock'
|
||||
if os.path.exists(lock_path):
|
||||
os.remove(lock_path)
|
||||
@@ -4,27 +4,27 @@ import os
|
||||
import tarfile
|
||||
import time
|
||||
import traceback
|
||||
from multiprocessing import Process
|
||||
from pathlib import Path
|
||||
from threading import Thread
|
||||
|
||||
import requests
|
||||
|
||||
from bauh.api.http import HttpClient
|
||||
from bauh.gems.appimage import LOCAL_PATH, db
|
||||
from bauh.gems.appimage import LOCAL_PATH
|
||||
|
||||
|
||||
class DatabaseUpdater(Thread if bool(int(os.getenv('BAUH_DEBUG', 0))) else Process):
|
||||
class DatabaseUpdater(Thread):
|
||||
|
||||
URL_DB = 'https://raw.githubusercontent.com/vinifmor/bauh-files/master/appimage/dbs.tar.gz'
|
||||
COMPRESS_FILE_PATH = LOCAL_PATH + '/db.tar.gz'
|
||||
|
||||
def __init__(self, http_client: HttpClient, logger: logging.Logger):
|
||||
def __init__(self, http_client: HttpClient, logger: logging.Logger, db_locks: dict):
|
||||
super(DatabaseUpdater, self).__init__(daemon=True)
|
||||
self.http_client = http_client
|
||||
self.logger = logger
|
||||
self.enabled = bool(int(os.getenv('BAUH_APPIMAGE_DB_UPDATER', 1)))
|
||||
self.sleep = 60 * 20
|
||||
self.db_locks = db_locks
|
||||
self.sleep = int(os.getenv('BAUH_APPIMAGE_DB_UPDATER_TIME', 60 * 20))
|
||||
|
||||
def _download_databases(self):
|
||||
self.logger.info('Retrieving AppImage databases')
|
||||
@@ -44,9 +44,11 @@ class DatabaseUpdater(Thread if bool(int(os.getenv('BAUH_DEBUG', 0))) else Proce
|
||||
if old_db_files:
|
||||
self.logger.info('Deleting old database files')
|
||||
for f in old_db_files:
|
||||
db.acquire_lock(f)
|
||||
os.remove(f)
|
||||
db.release_lock(f)
|
||||
self.db_locks[f].acquire()
|
||||
try:
|
||||
os.remove(f)
|
||||
finally:
|
||||
self.db_locks[f].release()
|
||||
|
||||
self.logger.info('Old database files deleted')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user