50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
|
|
import time
|
||
|
|
import random
|
||
|
|
from base_subsystem import BaseSubsystem
|
||
|
|
from sqlalchemy.orm import sessionmaker
|
||
|
|
import database_manager
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class Test_SS(BaseSubsystem):
|
||
|
|
|
||
|
|
|
||
|
|
def _startup(self):
|
||
|
|
"""启动时的初始化"""
|
||
|
|
self.name= "测试子系统1"
|
||
|
|
#self.logger.info(f"子系统 '{self.name}' 初始化...")
|
||
|
|
print(f"子系统 '{self.name}' 初始化...")
|
||
|
|
# 订阅感兴趣的消息主题
|
||
|
|
self.message_bus.subscribe(f"{self.name}.command", self._handle_command)
|
||
|
|
|
||
|
|
def _shutdown(self):
|
||
|
|
"""停止时的清理"""
|
||
|
|
self.logger.info(f"子系统 '{self.name}' 清理中...")
|
||
|
|
|
||
|
|
def process(self):
|
||
|
|
"""主要业务逻辑"""
|
||
|
|
# 模拟业务处理
|
||
|
|
time.sleep(3)
|
||
|
|
#self.logger.info(f"子系统 '{self.name}' 工作中...")
|
||
|
|
|
||
|
|
Session=sessionmaker(bind=self.db_manager.engine)
|
||
|
|
session=Session()
|
||
|
|
|
||
|
|
new_test=database_manager.Test(name="测试数据",value=time.time())
|
||
|
|
session.add(new_test)
|
||
|
|
|
||
|
|
session.commit()
|
||
|
|
session.close()
|
||
|
|
|
||
|
|
self.message_bus.publish(f"{self.name}.status",{"time":str(time.time())})
|
||
|
|
|
||
|
|
|
||
|
|
def _handle_command(self, message):
|
||
|
|
"""处理来自其他子系统的命令"""
|
||
|
|
self.logger.info(f"收到命令: {message.payload}")
|
||
|
|
self._stats['processed_messages'] += 1
|
||
|
|
|
||
|
|
|
||
|
|
config=[{'class':Test_SS,'name':"测试子系统1",'params':{},'autostart':True},]
|