From 14b3258eec37d36bfece69d16a7e2171568b1bc6 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Thu, 14 Mar 2024 19:20:36 +0100 Subject: [PATCH] agent-spawner: base64 encoding one more time The base64 encoding is worth doing on the dispatcher side, and store strings to Redis, instead of decoding and encoding it once more in worker (see #155). Fixes: #155, #156 --- resalloc_agent_spawner/dispatcher.py | 13 +++++++++---- resalloc_agent_spawner/worker.py | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/resalloc_agent_spawner/dispatcher.py b/resalloc_agent_spawner/dispatcher.py index 529032e..a5d9fd2 100644 --- a/resalloc_agent_spawner/dispatcher.py +++ b/resalloc_agent_spawner/dispatcher.py @@ -2,6 +2,7 @@ Agent spawner daemon. Part of the resalloc project. """ +import base64 import sys import logging from copr_common.dispatcher import Dispatcher @@ -84,7 +85,11 @@ def get_ticket_data(self, ticket_id): if not ticket.collect(): # not yet resolved or failed return None - return ticket.output + + # The ticket.output is bytes(), and we don't plan to touch the data at + # all, we are just going to "forward" the data base64 encoded to our + # hook scripts. Encode it now, and never change again. + return base64.b64encode(ticket.output.data).decode("ascii") def try_to_stop(self, group_id, to_stop): """ @@ -174,10 +179,10 @@ def start_preparing(self): resource that we can prepare (in the background). Switch the state. """ for ticket_id in self.get_tickets("NEW"): - ticket = self.resalloc.getTicket(ticket_id) - if not ticket.collect(): + data = self.get_ticket_data(ticket_id) + if data is None: continue - self.set_ticket_attribute(ticket_id, "data", ticket.output) + self.set_ticket_attribute(ticket_id, "data", data) self.set_ticket_attribute(ticket_id, "state", "PREPARING") def detect_failed_tickets(self): diff --git a/resalloc_agent_spawner/worker.py b/resalloc_agent_spawner/worker.py index 3ed9c8d..5a547db 100644 --- a/resalloc_agent_spawner/worker.py +++ b/resalloc_agent_spawner/worker.py @@ -2,8 +2,6 @@ Handle certain tasks by a background daemon process. """ -import base64 - from copr_common.background_worker import BackgroundWorker from resalloc_agent_spawner.helpers import ( @@ -37,7 +35,9 @@ def handle_ticket(self, ticket_id): # We know there's self._redis initialized by parent class so we don't # create yet another connection. redis_dict = self._redis.hgetall(redis_key) - ticket_data = base64.b64encode(redis_dict["data"]) + + # dispatcher already converted data to b64-encoded strings + ticket_data = redis_dict["data"] if redis_dict["state"] == "PREPARING": if self.cmd_take(redis_dict["group_id"], ticket_data):