增加kare dbus信号监听

This commit is contained in:
luoxueyi 2024-06-03 16:00:21 +08:00
parent 1141476aea
commit d4abda0a1f
2 changed files with 110 additions and 4 deletions

View File

@ -2,12 +2,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import re import re
import dbus
import json import json
import traceback import traceback
import shutil import shutil
import sqlite3 import sqlite3
import logging import logging
import datetime import datetime
import threading
from operator import itemgetter from operator import itemgetter
from gettext import gettext as _ from gettext import gettext as _
from SystemUpdater.Core.errors import * from SystemUpdater.Core.errors import *
@ -41,6 +43,7 @@ class Sqlite3Server(object):
self.__need_DataMigration(mark_FirstMigration = False) self.__need_DataMigration(mark_FirstMigration = False)
self.current_purge_pkgs = [] self.current_purge_pkgs = []
self.init_metadata() self.init_metadata()
self.transaction_manager = TransactionManager(self.window_main)
def init_metadata(self): def init_metadata(self):
self.deb_metadata = {} self.deb_metadata = {}
@ -778,3 +781,104 @@ def listtojsonstr(lists):
import json import json
jsonfile = json.dumps(lists) jsonfile = json.dumps(lists)
return jsonfile return jsonfile
class TransactionManager:
TRANSTYPE_INSTALL = 30
TRANSTYPE_PURGE = 10
def __init__(self, updateDaemon):
self.updateDaemon = updateDaemon
self.name_timers = {}
self.bus = dbus.SystemBus()
def signal_handle_install(self,name,ret):
logging.info("receive installed:%s, %d",name,ret)
self.stop_timer(name)
self.updateDaemon.dbus_send.InstalldebFinished (True if ret==0 else False,'kare install success:'+name,'')
def signal_handler_purge(self,name,ret):
logging.info("receive purged:%s, %d",name,ret)
self.stop_timer(name)
self.updateDaemon.dbus_send.PurgePackagesFinished (True if ret==0 else False,'kare remove success:'+name,'')
def signal_installed(self):
self.bus.add_signal_receiver (
self.signal_handle_install,
signal_name = "installFinished",
dbus_interface = "org.kylin.kare",
bus_name = "org.kylin.kare",
path = "/org/kylin/kare"
)
def signal_purged(self):
self.bus.add_signal_receiver (
self.signal_handler_purge,
signal_name = "removeFinished",
dbus_interface = "org.kylin.kare",
bus_name = "org.kylin.kare",
path = "/org/kylin/kare"
)
def start_transaction(self, name, transTimeout = TRANSTYPE_INSTALL):
if transTimeout == self.TRANSTYPE_INSTALL:
self.signal_installed()
if transTimeout == self.TRANSTYPE_PURGE:
self.signal_purged()
logging.info("Transaction running:'%s', timeout: %s min", name, transTimeout)
timer = threading.Timer(60 * transTimeout, function=self.timeout_callback, args=(name,))
timer.start()
self.name_timers[name] = timer
logging.info(self.name_timers)
def timeout_callback(self, name):
logging.info("Transaction '%s' timed out.",name)
self.exit_transaction(name)
del self.name_timers[name]
if not self.name_timers:
self.disconnect_signal()
def exit_transaction(self, name):
logging.info("Exiting transaction '%s'.",name)
def stop_timer(self, name):
if name in self.name_timers.keys():
self.name_timers[name].cancel()
del self.name_timers[name]
logging.info("Timer for transaction '%s' stopped.",name)
else:
logging.info("No active timer found for transaction '%s'.",name)
if not self.name_timers:
self.disconnect_signal()
def stop_all_timers(self):
for name, timer in self.name_timers.items():
timer.cancel()
logging.info("timer cancel '%s'.",name)
self.name_timers.clear()
self.disconnect_signal()
def disconnect_signal(self):
# return #发现某些时候收不到返回信号,暂时不断开
logging.info("disconnect from removeFinished")
self.bus.remove_signal_receiver (
self.signal_handler_purge,
signal_name = "removeFinished",
dbus_interface = "org.kylin.kare",
bus_name = "org.kylin.kare",
path = "/org/kylin/kare"
)
logging.info("disconnect from installFinished")
self.bus.remove_signal_receiver (
self.signal_handle_install,
signal_name = "installFinished",
dbus_interface = "org.kylin.kare",
bus_name = "org.kylin.kare",
path = "/org/kylin/kare"
)

View File

@ -138,7 +138,8 @@ class UpdateManager():
kareRet = kareiInterface.install ("openkylin2.0", deb_path) kareRet = kareiInterface.install ("openkylin2.0", deb_path)
if kareRet: if kareRet:
self.dbus_send.InstalldebFinished(True,'kare install call success','') logging.info("kare install call success:%s", deb_path)
self.sqlite3_server.transaction_manager.start_transaction(deb_path, 30)
else: else:
self.dbus_send.InstalldebFinished(False,'kare install call error','') self.dbus_send.InstalldebFinished(False,'kare install call error','')
except Exception as e: except Exception as e:
@ -150,13 +151,14 @@ class UpdateManager():
kareiInterface = dbus.Interface(dbus.SystemBus().get_object('org.kylin.kare', '/org/kylin/kare'), kareiInterface = dbus.Interface(dbus.SystemBus().get_object('org.kylin.kare', '/org/kylin/kare'),
dbus_interface='org.kylin.kare') dbus_interface='org.kylin.kare')
for pl in pkgs_list: for pl in pkgs_list:
logging.info("kare remove:%s", pl) logging.info("kare remove: %s", pl)
kareRet = kareiInterface.remove(pl) kareRet = kareiInterface.remove(pl)
if kareRet: if kareRet:
self.dbus_send.PurgePackagesFinished (True,'kare remove call success','') logging.info("kare remove call success:%s", pl)
self.sqlite3_server.transaction_manager.start_transaction(pl, 10)
else: else:
self.dbus_send.PurgePackagesFinished (False,'kare remove error','') self.dbus_send.PurgePackagesFinished (False,'kare remove call error','')
except Exception as e: except Exception as e:
logging.error("kare remove error:%s", str(e)) logging.error("kare remove error:%s", str(e))
self.dbus_send.PurgePackagesFinished (False,'kare remove error','') self.dbus_send.PurgePackagesFinished (False,'kare remove error','')