Merge pull request #18 from titanscouting/daemon-fix

Fix messaging in daemon

Former-commit-id: 6a4d13a33f
This commit is contained in:
Arthur Lu 2021-10-16 16:42:47 -07:00 committed by GitHub
commit 49ee02d6fa
5 changed files with 29 additions and 36 deletions

View File

@ -151,18 +151,16 @@ __all__ = [
# imports: # imports:
import asyncio
import json import json
import math import math
from multiprocessing import Pool, freeze_support from multiprocessing import Pool, freeze_support
import os import os
import pymongo import pymongo
import sys import sys
import threading
import time import time
import traceback import traceback
import warnings import warnings
import websockets import zmq
from interface import splash, log, ERR, INF, stdout, stderr from interface import splash, log, ERR, INF, stdout, stderr
from data import get_previous_time, pull_new_tba_matches, set_current_time, load_match, push_match, load_pit, push_pit, get_database_config, set_database_config, check_new_database_matches from data import get_previous_time, pull_new_tba_matches, set_current_time, load_match, push_match, load_pit, push_pit, get_database_config, set_database_config, check_new_database_matches
@ -544,40 +542,23 @@ def start(pid_path, verbose = False, profile = False, debug = False):
f = open('errorlog.log', 'w+') f = open('errorlog.log', 'w+')
with daemon.DaemonContext( with daemon.DaemonContext(
working_directory=os.getcwd(), working_directory = os.getcwd(),
pidfile=pidfile.TimeoutPIDLockFile(pid_path), pidfile = pidfile.TimeoutPIDLockFile(pid_path),
stderr=f stderr = f
): ):
async def handler(client, path): context = zmq.Context()
clients.append(client) socket = context.socket(zmq.PUB)
while True: socket.bind("tcp://*:5678")
try:
pong_waiter = await client.ping()
await pong_waiter
time.sleep(3)
except Exception as e:
clients.remove(client)
break
async def send_one(client, data): socket.send(b'status')
await client.send(data)
def send(target, level, message, code = 0): def send(target, level, message, code = 0):
message_clients = clients.copy() socket.send(bytes("status: " + message, 'utf-8'))
for client in message_clients:
try:
asyncio.run(send_one(client, message))
except:
pass
clients = []
start_server = websockets.serve(handler, "0.0.0.0", 5678)
asyncio.get_event_loop().run_until_complete(start_server)
threading.Thread(target = asyncio.get_event_loop().run_forever).start()
exit_code = main(send) exit_code = main(send)
socket.close()
f.close()
sys.exit(exit_code) sys.exit(exit_code)
def stop(pid_path): def stop(pid_path):

View File

@ -10,8 +10,6 @@ a = Analysis(['superscript.py'],
"dnspython", "dnspython",
"sklearn.utils._weight_vector", "sklearn.utils._weight_vector",
"requests", "requests",
"websockets.legacy",
"websockets.legacy.server",
], ],
hookspath=[], hookspath=[],
runtime_hooks=[], runtime_hooks=[],

View File

@ -15,5 +15,5 @@ pandas
kivy==2.0.0rc2 kivy==2.0.0rc2
websockets pyzmq
python-daemon python-daemon

14
test/client.py Normal file
View File

@ -0,0 +1,14 @@
import signal
import zmq
signal.signal(signal.SIGINT, signal.SIG_DFL)
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://localhost:5678')
socket.setsockopt(zmq.SUBSCRIBE, b'status')
while True:
message = socket.recv_multipart()
print(f'Received: {message}')