Skip to content

Commit

Permalink
client_stub/daemon: Add a way to handle commands from build monitor
Browse files Browse the repository at this point in the history
Signed-off-by: ZorEl212 <[email protected]>
  • Loading branch information
ZorEl212 committed Oct 26, 2024
1 parent 0b069e4 commit 8c05468
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions client_stub/echo_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from models.exception.auth import AuthenticationError
from client_stub.utils import config_loader, config_saver, exception_handler
from engineio.async_drivers import gevent
from models.build import Build

# Global variables
sio = socketio.Client(logger=True, engineio_logger=True)
Expand Down Expand Up @@ -67,16 +68,15 @@ def on_disconnect(self):
@classmethod
def send_build_status(cls, sio, build):
# Update the timestamp to the current time
build['timestamp'] = datetime.datetime.utcnow().isoformat() + 'Z'
data = json.dumps(build)
if sio.connected:
sio.emit('builds_report', data, namespace='/daemon')
sio.emit('builds_report', build, namespace='/daemon')
else:
print("WebSocket connection is closed, unable to send data.")

def listen_for_build_updates(self):
print("Listening for build updates")
def socket_listner():

def socket_listener():
if os.path.exists(self.socket_path):
os.remove(self.socket_path)
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
Expand All @@ -86,18 +86,40 @@ def socket_listner():
conn, _ = sock.accept()
thread = threading.Thread(target=handle_client, args=(conn,))
thread.start()

def handle_client(conn):
with conn:
while True:
data = conn.recv(1024)
if not data:
break
if data:
build = json.loads(data)
self.send_build_status(sio, build)

socket_listner()
try:
command = json.loads(data)
server_info = ast.literal_eval(config.get('SERVER', 'server'))
# Responding back to the client
if command.get('command') == 'add_build':
new_build = Build(serverId=server_info.get('id'), buildNmae=command.get('data').get('name'),
buildDir=command.get('data').get('dir'))
res = sio.call('add_build', {'server_id': server_info.get('id'), 'build': new_build.to_dict()}, namespace=self.namespace)
if res.get('status') == 'success':
response = {'success': True, 'data': new_build.to_dict() }
conn.send(json.dumps(response).encode('utf-8'))
if command.get('command') == 'add_user':
res = sio.call('add_user', {'server_id': server_info.get('id'), 'user_id': command.get('user_id')}, namespace=self.namespace)
if res.get('status') == 'success':
response = {'success': True, 'data': {'user_id': command.get('user_id')}}
conn.send(json.dumps(response).encode('utf-8'))
else:
response = {"status": "received", "build_id": command.get("id")}
build_info = {'server_id': server_info.get('id'), 'build_id': command.get('id'), 'data': command.get('data')}
conn.send(json.dumps({'status:': 'ok'}).encode('utf-8'))
self.send_build_status(sio, build_info)
except json.JSONDecodeError:
print("Invalid data received")
conn.send(b'{"error": "invalid data"}') # Sending error response

socket_listener()


def main():
ws_url = 'http://0.0.0.0:8000'
Expand Down

0 comments on commit 8c05468

Please sign in to comment.