def mainloop(self): previous = None while True: screen = self.renderer.loop(self.input) screen, next_interaction = self.current.loop(screen) if self.current.active: self.inactive_start = pygame.time.get_ticks() delta = pygame.time.get_ticks() - self.inactive_start if delta > 5000: next_interaction = 'Sad' if self.input.too_close(): next_interaction = "Scared" if pygame.time.get_ticks() - self.refresh > 10000: self.refresh = pygame.time.get_ticks() self.input.ignore_list = [] if next_interaction is not None: telemetry.log('Switching state: ' + next_interaction) if next_interaction == 'Scared': self.current = self.sequences[next_interaction](self.input, previous) else: self.current = self.sequences[next_interaction](self.input) if next_interaction != 'Scared': previous = next_interaction #screen = rendering.input_debug_shim(screen, self.input) screen = rendering.display_pointer_circles(screen, self.input) self.renderer.display() self.keep_alive()
def main(): def on_message_receive_callback(channel, method, properties, body): log("Mensaje recibido.") message = json.loads(body) operation = message["operation"] image_processor.run(operation) log("Todas las tareas asociadas a la operación '{}' han finalizado.". format(operation)) log("Inicializando conexión a RabbitMQ en '{}:{}'...".format( RABBITMQ_HOST, RABBITMQ_PORT)) connection = BlockingConnection( ConnectionParameters(host=RABBITMQ_HOST, port=RABBITMQ_PORT, credentials=PlainCredentials( RABBITMQ_USER, RABBITMQ_PASS), virtual_host="/")) channel = connection.channel() channel.exchange_declare(exchange="XPyxel-Nodes", exchange_type="direct", durable=True) channel.queue_declare(queue=WORKER_ID, durable=True) channel.basic_consume(queue=WORKER_ID, on_message_callback=on_message_receive_callback, auto_ack=True) log("Pyxel [{}] ha iniciado correctamente y está esperando peticiones. Presione [Ctrl]+C para salir..." .format(WORKER_ID)) channel.start_consuming()
def execute_task(task: {}): log("Procesando tarea '{}'...".format(task["id"])) filename, filtered_image = process_image(task) image_id = store_image(filtered_image, filename) update_assigned_task(task["id"], str(image_id)) log("Tarea finalizada: '{}'.".format(task["id"]))
def process_image(task: {}) -> (str, Image.Image): log("Aplicando filtro '{}'...".format(task["filter"])) obj = mongo_storage.get_fs_object(task["file_id"]) original_image = image_utils.read_image(obj) filtered_image = filter_image(original_image, task["filter"]) return ("{}-{}.jpg".format(obj.filename.replace(".jpg", ""), task["filter"]), filtered_image)
def __init__(self, sequences, start, input): self.sequences = sequences self.input = input self.current = self.sequences[start](self.input) self.renderer = rendering.Renderer() self.timer = pygame.time.Clock() telemetry.log('Starting.') self.inactive_start = 0 self.refresh = 0
def on_message_receive_callback(channel, method, properties, body): log("Mensaje recibido.") message = json.loads(body) operation = message["operation"] image_processor.run(operation) log("Todas las tareas asociadas a la operación '{}' han finalizado.". format(operation))
def run(operation_id: str): log("Inicializando ejecución de tareas para la operación '{}'...".format( operation_id)) tasks = retrieve_assigned_tasks(operation_id) log("Se han recuperado las tareas asignadas. Inicializando procesamiento de tareas..." ) for task in tasks: execute_task(task)
def update_assigned_task(task_id: str, output_file_id: str): log("Registrando resultados de la tarea...") db = get_db_connection() cur = db.cursor() cur.execute( "UPDATE public.operation_task SET finished_at = NOW(), output_file = %s WHERE id = %s;", (output_file_id, task_id)) db.commit() cur.close() db.close()
def retrieve_assigned_tasks(operation_id: str) -> []: log("Recuperando tareas asignadas para la operación actual...") tasks = [] db = get_db_connection() cur = db.cursor() cur.execute( "SELECT id, file_id, filter FROM public.operation_task WHERE operation = %s AND assigned_worker = %s;", (operation_id, os.environ["PYXEL_WORKER_ID"])) rows = cur.fetchall() for row in rows: tasks.append({ "id": row[0], "file_id": row[1], "filter": row[2].replace("\"", "") }) cur.close() db.close() return tasks
def store_image(filtered_image: Image.Image, filename: str) -> ObjectId: log("Almacenando imagen con filtro aplicado...") filtered_image_id = mongo_storage.save_image(filtered_image, filename) return filtered_image_id
def test_default(self, probe): telemetry.add_handler(probe) telemetry.log(REASON) assert probe.calls == 1
log("Todas las tareas asociadas a la operación '{}' han finalizado.". format(operation)) log("Inicializando conexión a RabbitMQ en '{}:{}'...".format( RABBITMQ_HOST, RABBITMQ_PORT)) connection = BlockingConnection( ConnectionParameters(host=RABBITMQ_HOST, port=RABBITMQ_PORT, credentials=PlainCredentials( RABBITMQ_USER, RABBITMQ_PASS), virtual_host="/")) channel = connection.channel() channel.exchange_declare(exchange="XPyxel-Nodes", exchange_type="direct", durable=True) channel.queue_declare(queue=WORKER_ID, durable=True) channel.basic_consume(queue=WORKER_ID, on_message_callback=on_message_receive_callback, auto_ack=True) log("Pyxel [{}] ha iniciado correctamente y está esperando peticiones. Presione [Ctrl]+C para salir..." .format(WORKER_ID)) channel.start_consuming() log("Inicializando Pyxel. Nodo: {}".format(WORKER_ID)) main()