From 236c746e1cad53f4cc1d48e889b993b426c37fa8 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Tue, 21 Jun 2022 12:39:51 +0530 Subject: [PATCH] Fix: Suddenly got errors saying that the redis.enqueue job was running in a different loop --- barker/barker/printing/bill.py | 6 ++++-- barker/barker/printing/cashier_report.py | 6 ++++-- barker/barker/printing/discount_report.py | 6 ++++-- barker/barker/printing/kot.py | 6 ++++-- barker/barker/printing/product_sale_report.py | 6 ++++-- barker/barker/printing/sale_report.py | 6 ++++-- 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/barker/barker/printing/bill.py b/barker/barker/printing/bill.py index cc21f2d..cc70d58 100644 --- a/barker/barker/printing/bill.py +++ b/barker/barker/printing/bill.py @@ -62,12 +62,14 @@ def print_bill(voucher_id: uuid.UUID, db: Session): for i in [it for it in items_dict.values() if it.quantity != 0]: get_tax_item(i.tax.id, i.tax.name, i.tax_amount, tax) data = design_bill(voucher, list(items_dict.values()), list(tax.values()), db) - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run( + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, printer.address, printer.cut_code, _queue_name=f"barker:print:{printer.name}" ) ) + loop.close() def get_tax_item( diff --git a/barker/barker/printing/cashier_report.py b/barker/barker/printing/cashier_report.py index 9dc1540..498d179 100644 --- a/barker/barker/printing/cashier_report.py +++ b/barker/barker/printing/cashier_report.py @@ -28,12 +28,14 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run( + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, printer.address, printer.cut_code, _queue_name=f"barker:print:{printer.name}" ) ) + loop.close() def design_cashier_report(report: CashierReport): diff --git a/barker/barker/printing/discount_report.py b/barker/barker/printing/discount_report.py index 07f5dab..cb0a2ee 100644 --- a/barker/barker/printing/discount_report.py +++ b/barker/barker/printing/discount_report.py @@ -25,12 +25,14 @@ def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Sess .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run( + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, printer.address, printer.cut_code, _queue_name=f"barker:print:{printer.name}" ) ) + loop.close() def design_discount_report(report: DiscountReport): diff --git a/barker/barker/printing/kot.py b/barker/barker/printing/kot.py index 7da8de9..db40a57 100644 --- a/barker/barker/printing/kot.py +++ b/barker/barker/printing/kot.py @@ -104,13 +104,14 @@ def print_kot(voucher_id: uuid.UUID, db: Session): if key not in my_hash: my_hash[key] = (printer, []) my_hash[key][1].append(item) - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) for key, value in my_hash.items(): printer_id, copies = key printer, items = value for c in range(int(copies)): data = design_kot(voucher, kot, items, c, db) - asyncio.run( + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, @@ -119,3 +120,4 @@ def print_kot(voucher_id: uuid.UUID, db: Session): _queue_name=f"barker:print:{printer.name}", ) ) + loop.close() diff --git a/barker/barker/printing/product_sale_report.py b/barker/barker/printing/product_sale_report.py index 17f9b70..25571e8 100644 --- a/barker/barker/printing/product_sale_report.py +++ b/barker/barker/printing/product_sale_report.py @@ -26,12 +26,14 @@ def print_product_sale_report(report, device_id: uuid.UUID, db: Session): .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run( + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, printer.address, printer.cut_code, _queue_name=f"barker:print:{printer.name}" ) ) + loop.close() def design_product_sale_report(report): diff --git a/barker/barker/printing/sale_report.py b/barker/barker/printing/sale_report.py index badbfde..e34e842 100644 --- a/barker/barker/printing/sale_report.py +++ b/barker/barker/printing/sale_report.py @@ -28,12 +28,14 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session): .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - redis: ArqRedis = asyncio.run(create_pool(redis_settings)) - asyncio.run( + loop = asyncio.new_event_loop() + redis: ArqRedis = loop.run_until_complete(create_pool(redis_settings)) + loop.run_until_complete( redis.enqueue_job( "sent_to_printer", data, printer.address, printer.cut_code, _queue_name=f"barker:print:{printer.name}" ) ) + loop.close() def design_sale_report(report: SaleReport):