pyinfra/mini_queue/utils/rabbitmq.py
Julius Unverfehrt 4edf04ab24 Pull request #1: Add storage handle
Merge in RR/mini_queue from add-storage-handle to master

Squashed commit of the following:

commit 03e542d2a65802c28735873fae184209f0c83553
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 11:55:34 2022 +0100

    Quickfix typo

commit b4d538e9445187435d87c5cf8ce1f4e448021129
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 11:41:42 2022 +0100

    added prefetch count and make channel function

commit d46d1375e387d36641c06b062a8ccc54f114ef4c
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 11:20:39 2022 +0100

    black on M.s request

commit bc47b20312a978f19b08531804bf42b00f0a88f0
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 11:19:57 2022 +0100

    changed response

commit 9a475ecd8df9ca007e5f7fe146483b6403eccc3b
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 10:15:08 2022 +0100

    .

commit 108bc3ea90d867575db8c1b1503c9df859222485
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 09:56:56 2022 +0100

    quickrestore

commit ae04d17d8d041f612d86117e8e96c96ddffcbde3
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 09:37:30 2022 +0100

    refactor

commit 68051a72eb93868eba8adba234258b9e5373ecaa
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 08:50:59 2022 +0100

    added answer file template for rancher

commit 09ef45ead51c07732a20133acad0b8b2ae7d0a61
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 08:26:05 2022 +0100

    Quickfix inconsistency

commit d925b0f3f91f29403c88fb6149566ec966af2973
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Wed Feb 16 08:20:40 2022 +0100

    Quick refactor

commit 48795455cde8d97ed98e58c3004a87a26f331352
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Tue Feb 15 17:46:45 2022 +0100

    bluckckck

commit 80e58efab0269dc513990f83b14ceb36b3e4dd8e
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Tue Feb 15 17:45:49 2022 +0100

    Quick restatus setting

commit 83f276ee13348a678b7da84e25ca844dd348b4c9
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Tue Feb 15 17:30:16 2022 +0100

    Quickreset to working status

commit d44cdcf922250639a6832cc3e16d0d967d9853fb
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date:   Tue Feb 15 14:44:26 2022 +0100

    added storage handle for minio WIP
2022-02-16 11:57:52 +01:00

67 lines
2.3 KiB
Python

import json
import logging
import tempfile
import time
from operator import itemgetter
import pika
from mini_queue.utils.config import CONFIG
from mini_queue.utils.file import dossier_id_and_file_id_to_compressed_storage_pdf_object_name, download, unzip
from mini_queue.utils.minio import MinioHandle
def make_channel(connection) -> pika.adapters.blocking_connection.BlockingChannel:
channel = connection.channel()
channel.basic_qos(prefetch_count=CONFIG.rabbitmq.prefetch_count)
return channel
def declare_queue(channel, queue: str):
args = {
# "x-message-ttl": CONFIG.rabbitmq.message_ttl,
"x-dead-letter-exchange": "",
"x-dead-letter-routing-key": CONFIG.rabbitmq.queues.dead_letter,
}
return channel.queue_declare(queue=queue, auto_delete=False, arguments=args, durable=True)
def make_callback():
storage_client = MinioHandle()
def callback(channel, method, properties, body):
logging.info(f" [R] Received {body}")
response = json.dumps(process(body))
channel.basic_publish(exchange="", routing_key=CONFIG.rabbitmq.queues.output, body=response)
channel.basic_ack(delivery_tag=method.delivery_tag)
def process(payload):
payload = json.loads(payload)
with tempfile.TemporaryDirectory() as pdf_compressed_dir:
with tempfile.TemporaryDirectory() as pdf_dir:
dossier_id, file_id = itemgetter("dossierId", "fileId")(payload)
time.sleep(0.4)
object_name = dossier_id_and_file_id_to_compressed_storage_pdf_object_name(dossier_id, file_id)
time.sleep(0.6)
downloaded_file_path = download(storage_client, object_name, pdf_compressed_dir)
time.sleep(0.3)
unzipped_file_path = unzip(downloaded_file_path, pdf_dir)
time.sleep(1)
payload["imageMetadata"] = []
return json.dumps(payload)
return callback
def read_connection_params():
credentials = pika.PlainCredentials(CONFIG.rabbitmq.user, CONFIG.rabbitmq.password)
parameters = pika.ConnectionParameters(
host=CONFIG.rabbitmq.host,
port=CONFIG.rabbitmq.port,
heartbeat=CONFIG.rabbitmq.heartbeat,
credentials=credentials,
)
return parameters