import time import logging from django.core.management.base import BaseCommand from system.services.vdes_data_receive import VDESSocketMonitor logger = logging.getLogger(__name__) class Command(BaseCommand): help = '控制 VDES 监控服务' def add_arguments(self, parser): parser.add_argument( 'action', choices=['start', 'stop', 'restart'], help='操作: start, stop 或 restart' ) def handle(self, *args, **options): monitor = VDESSocketMonitor() action = options['action'] if action == 'start': monitor.start() self.stdout.write(self.style.SUCCESS('VDES 监控服务已启动')) elif action == 'stop': monitor.stop() self.stdout.write(self.style.SUCCESS('VDES 监控服务已停止')) elif action == 'restart': monitor.stop() time.sleep(1) monitor.start() self.stdout.write(self.style.SUCCESS('VDES 监控服务已重启')) # 确保程序不会立即退出 if action == 'start' or action == 'restart': try: # 保持命令运行,直到用户中断 while True: time.sleep(1) except KeyboardInterrupt: monitor.stop() self.stdout.write(self.style.SUCCESS('\nVDES 监控服务已停止'))