Esempio n. 1
0
    def unread_site_msg_count(self):
        user_id = str(self.scope["user"].id)
        self.send_unread_msg_count()

        try:
            msgs = self.chan.listen()
            # 开始之前关闭连接,因为server端可能关闭了连接,而 client 还在 CONN_MAX_AGE 中
            close_old_connections()
            for message in msgs:
                if message['type'] != 'message':
                    continue

                try:
                    msg = json.loads(message['data'].decode())
                except json.JSONDecoder as e:
                    logger.debug('Decode json error: ', e)
                    continue
                if not msg:
                    continue

                logger.debug('New site msg recv, may be mine: {}'.format(msg))
                users = msg.get('users', [])
                logger.debug('Message users: {}'.format(users))
                if user_id in users:
                    self.send_unread_msg_count()
        except ConnectionError:
            logger.error('Redis chan closed')
        finally:
            logger.info('Notification ws thread end')
            close_old_connections()
Esempio n. 2
0
 def disconnect(self, close_code):
     try:
         if self.chan is not None:
             self.chan.close()
         self.close()
     finally:
         close_old_connections()
         logger.info('Notification websocket disconnect')
Esempio n. 3
0
def on_celery_task_pre_run(task_id='', **kwargs):
    # 关闭之前的数据库连接
    close_old_connections()

    # 保存 Lang context
    key = TASK_LANG_CACHE_KEY.format(task_id)
    task_lang = cache.get(key)
    if task_lang:
        translation.activate(task_lang)
Esempio n. 4
0
 def perform_sync(self):
     logger.info('Start perform sync ldap users from server to cache')
     try:
         self.pre_sync()
         self.sync()
         self.post_sync()
     except Exception as e:
         error_msg = str(e)
         logger.error(error_msg)
         self.set_task_error_msg(error_msg)
     finally:
         logger.info('End perform sync ldap users from server to cache')
         close_old_connections()
Esempio n. 5
0
 def keep_subscribe_settings_change():
     while True:
         try:
             sub = setting_pub_sub.subscribe()
             msgs = sub.listen()
             # 开始之前关闭连接,因为server端可能关闭了连接,而 client 还在 CONN_MAX_AGE 中
             close_old_connections()
             for msg in msgs:
                 if msg["type"] != "message":
                     continue
                 item = msg['data'].decode()
                 logger.debug("Found setting change: {}".format(str(item)))
                 Setting.refresh_item(item)
         except Exception as e:
             logger.exception(f'subscribe_settings_change: {e}')
             Setting.refresh_all_settings()
         finally:
             close_old_connections()
Esempio n. 6
0
    def start_heartbeat(self):
        while True:
            heartbeat_data = {
                'cpu_load': get_cpu_load(),
                'memory_used': get_memory_usage(),
                'disk_used': get_disk_usage(path=settings.BASE_DIR),
                'sessions': [],
            }
            status_serializer = StatusSerializer(data=heartbeat_data)
            status_serializer.is_valid()
            status_serializer.validated_data.pop('sessions', None)
            terminal = self.get_or_register_terminal()
            status_serializer.validated_data['terminal'] = terminal

            try:
                status_serializer.save()
                time.sleep(self.interval)
            except OperationalError:
                close_old_connections()
Esempio n. 7
0
 def keep_subscribe_org_mapping():
     while True:
         try:
             subscribe = orgs_mapping_for_memory_pub_sub.subscribe()
             msgs = subscribe.listen()
             # 开始之前关闭连接,因为server端可能关闭了连接,而 client 还在 CONN_MAX_AGE 中
             close_old_connections()
             for message in msgs:
                 if message['type'] != 'message':
                     continue
                 if message['data'] == b'error':
                     raise ValueError
                 Organization.expire_orgs_mapping()
                 logger.debug('Expire orgs mapping: ' +
                              str(message['data']))
         except Exception as e:
             logger.exception(f'subscribe_orgs_mapping_expire: {e}')
             Organization.expire_orgs_mapping()
         finally:
             # 结束收关闭连接
             close_old_connections()
Esempio n. 8
0
 def keep_subscribe_node_assets_relation():
     while True:
         try:
             subscribe = node_assets_mapping_for_memory_pub_sub.subscribe()
             msgs = subscribe.listen()
             # 开始之前关闭连接,因为server端可能关闭了连接,而 client 还在 CONN_MAX_AGE 中
             close_old_connections()
             for message in msgs:
                 if message["type"] != "message":
                     continue
                 org_id = message['data'].decode()
                 root_org_id = Organization.ROOT_ID
                 Node.expire_node_all_asset_ids_mapping_from_memory(org_id)
                 Node.expire_node_all_asset_ids_mapping_from_memory(
                     root_org_id)
                 logger.debug(
                     "Expire node assets id mapping from memory of org={}, pid={}"
                     "".format(str(org_id), os.getpid()))
         except Exception as e:
             logger.exception(f'subscribe_node_assets_mapping_expire: {e}')
             Node.expire_all_orgs_node_all_asset_ids_mapping_from_memory()
         finally:
             # 请求结束,关闭连接
             close_old_connections()
Esempio n. 9
0
def on_celery_task_post_run(**kwargs):
    close_old_connections()
Esempio n. 10
0
 def disconnect(self, close_code):
     self.disconnected = True
     self.close()
     close_old_connections()