feat: 添加调度器单例控制和launch启动集成
- agentmain.py: 添加单例锁机制,防止重复启动 - launch.pyw: 集成调度器启动,支持--no-scheduler参数
This commit is contained in:
25
agentmain.py
25
agentmain.py
@@ -110,8 +110,33 @@ class GeneraticAgent:
|
|||||||
if self.handler is not None: self.handler.code_stop_signal.append(1)
|
if self.handler is not None: self.handler.code_stop_signal.append(1)
|
||||||
|
|
||||||
|
|
||||||
|
def acquire_singleton_lock():
|
||||||
|
"""确保只有一个调度器实例运行"""
|
||||||
|
lock_file = './temp/scheduler.lock'
|
||||||
|
os.makedirs('./temp', exist_ok=True)
|
||||||
|
|
||||||
|
if os.name == 'nt': # Windows
|
||||||
|
import msvcrt
|
||||||
|
try:
|
||||||
|
lock_fd = open(lock_file, 'w')
|
||||||
|
msvcrt.locking(lock_fd.fileno(), msvcrt.LK_NBLCK, 1)
|
||||||
|
return lock_fd
|
||||||
|
except:
|
||||||
|
print('[Scheduler] Another instance is already running')
|
||||||
|
sys.exit(0)
|
||||||
|
else: # Unix/Linux
|
||||||
|
import fcntl
|
||||||
|
try:
|
||||||
|
lock_fd = open(lock_file, 'w')
|
||||||
|
fcntl.flock(lock_fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||||
|
return lock_fd
|
||||||
|
except:
|
||||||
|
print('[Scheduler] Another instance is already running')
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
lock_fd = acquire_singleton_lock() # 获取单例锁
|
||||||
agent = GeneraticAgent()
|
agent = GeneraticAgent()
|
||||||
threading.Thread(target=agent.run, daemon=True).start()
|
threading.Thread(target=agent.run, daemon=True).start()
|
||||||
def drain(dq, tag):
|
def drain(dq, tag):
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ if __name__ == '__main__':
|
|||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('port', nargs='?', default='8501')
|
parser.add_argument('port', nargs='?', default='8501')
|
||||||
parser.add_argument('--no-tg', action='store_true', help='不启动 Telegram Bot')
|
parser.add_argument('--no-tg', action='store_true', help='不启动 Telegram Bot')
|
||||||
|
parser.add_argument('--no-scheduler', action='store_true', help='不启动计划任务调度器')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
port = args.port
|
port = args.port
|
||||||
t = threading.Thread(target=start_streamlit, args=(port,), daemon=True)
|
t = threading.Thread(target=start_streamlit, args=(port,), daemon=True)
|
||||||
@@ -90,6 +91,12 @@ if __name__ == '__main__':
|
|||||||
print('[Launch] Telegram Bot started')
|
print('[Launch] Telegram Bot started')
|
||||||
else: print('[Launch] Telegram Bot disabled (--no-tg)')
|
else: print('[Launch] Telegram Bot disabled (--no-tg)')
|
||||||
|
|
||||||
|
if not args.no_scheduler:
|
||||||
|
scheduler_proc = subprocess.Popen([sys.executable, "agentmain.py"], creationflags=subprocess.CREATE_NO_WINDOW if os.name=='nt' else 0)
|
||||||
|
atexit.register(scheduler_proc.kill)
|
||||||
|
print('[Launch] Task Scheduler started')
|
||||||
|
else: print('[Launch] Task Scheduler disabled (--no-scheduler)')
|
||||||
|
|
||||||
monitor_thread = threading.Thread(target=idle_monitor, daemon=True)
|
monitor_thread = threading.Thread(target=idle_monitor, daemon=True)
|
||||||
monitor_thread.start()
|
monitor_thread.start()
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
|
|||||||
Reference in New Issue
Block a user