def __init__(self): super().__init__() self.safety_value = round( ((psutil.virtual_memory().available / 1048576) * 10) / 100) # %10 of memory self.group_average = {'LQueue': 10, 'MQueue': 40, 'HQueue': 300} self.min_value = (self.group_average.get('HQueue') * len(self.group_average)) self.min_group_limitation = self.init_limitation() for name, value in self.min_group_limitation.items(): Setting.put(name, value, force=True) sync_resource = QTimer(self) sync_resource.timeout.connect(self.interval, Qt.DirectConnection) sync_resource.start(3 * 1000 * 60)
def limit(self): try: limit = Setting.get(self.queue_name) if limit: return int(limit) except Exception: # use default limit if exception or not present pass return self.default_limit
def calculate_ratio(self): tmpl = {'up': None, 'down': None, 'down_ratio': 0} actions = { 'LQueue': tmpl.copy(), 'MQueue': tmpl.copy(), 'HQueue': tmpl.copy() } running_services = self.get_running_services() _max = 0 for group_k, group_v in self.min_group_limitation.items(): if self.available_resource() > self.min_value: current_number_of_running_request = running_services.get( group_k) allowed_number_of_request = Setting.get(group_k) initialized_number_of_request = group_v if current_number_of_running_request < allowed_number_of_request: difference = (allowed_number_of_request - current_number_of_running_request) if difference > initialized_number_of_request: down_ratio = round( (difference - initialized_number_of_request) / initialized_number_of_request + .5) actions[group_k]['down'] = True actions[group_k]['down_ratio'] = down_ratio elif current_number_of_running_request >= allowed_number_of_request: actions[group_k]['up'] = True else: allowed_number_of_request = Setting.get(group_k) portion = (allowed_number_of_request / group_v) if portion > _max: _max = portion actions.clear() actions.update({ group_k: { 'up': False, 'down': True, 'down_ratio': 1 } }) return actions
def calculate_ratio(self): tmpl = {'up': None, 'down': None, 'down_ratio': 0} actions = { 'LQueue': tmpl.copy(), 'MQueue': tmpl.copy(), 'HQueue': tmpl.copy() } running_services = self.get_running_services() _max = 0 for group_k, group_v in self.min_group_limitation.items(): if self.available_resource() > self.min_value: r = running_services.get(group_k) l = Setting.get(group_k) m = group_v if r < l: rt = (l - r) if rt > m: rt = round((rt - m) / m + .5) actions[group_k]['down'] = True actions[group_k]['down_ratio'] = rt elif r >= l: actions[group_k]['up'] = True else: l = Setting.get(group_k) rt = (l / group_v) if rt > _max: _max = rt actions.clear() actions.update({ group_k: { 'up': False, 'down': True, 'down_ratio': 1 } }) return actions
def apply_action(self, actions): for key, action in actions.items(): if action['up']: Setting.put( key, Setting.get(key) + self.min_group_limitation.get(key)) if action['down']: down = Setting.get(key) - (self.min_group_limitation.get(key) * action['down_ratio']) if down >= self.min_group_limitation.get(key): Setting.put(key, down)
from core.database import Database # get updates dir, current system version and database version update_path = BASE_APP_PATH + '/updates' current_version = version if version == '1.0' and mode.debug_mode: # drop all tables here, if app_version is 1.0 mg = Migrator(Database.db) table_list = Database.db.get_tables() for table in table_list: try: mg.drop_table(table) except Exception: continue database_version = Setting.get('api_version') if database_version is None: database_version = 0.0 try: num = sys.argv.pop(-1) patch = sys.argv.pop(-1) except: patch = None # if database version is lower than system current version, make update list from needed update file then apply the list if float(database_version) < float(current_version): updates = [] # convert database file name to current_version = 'v_' + str(database_version).replace('.', '_') for dirc in os.listdir(update_path):