diff --git a/.env b/.env index 9c3ef91..b6b2f32 100644 --- a/.env +++ b/.env @@ -8,8 +8,9 @@ PROJECT_NAME=barker MAX_WORKERS=4 -REDIS_HOST=localhost -REDIS_PORT=6379 +NATS_URL=nats://localhost:4222 +SUBJECT="barker.print" +STREAM="barker_print_stream" # openssl rand -hex 32 SECRET_KEY=611f9393e58f85521d16d9497bfd847ced70f2b99e12a9c6af40b7bd05cc7b1d @@ -24,6 +25,5 @@ TIMEZONE_OFFSET_MINUTES=330 ALEMBIC_LOG_LEVEL=INFO ALEMBIC_SQLALCHEMY_LOG_LEVEL=WARN -WORKER_REDIS_HOST= -WORKER_REDIS_PORT=6379 -WORKER_QUEUE_NAME="" + +FASTAPI_ENV=development diff --git a/.vscode/settings.json b/.vscode/settings.json index 8a907a3..13c0da0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,9 +1,9 @@ { - "python.defaultInterpreterPath": "/home/tanshu/programming/barker/barker/.venv/bin/python", + "python.defaultInterpreterPath": "barker/.venv/bin/python", "python.terminal.useEnvFile": true, - "python.interpreterPath": "/home/tanshu/programming/barker/barker/.venv/bin/python", "python.venvFolders": [ "barker/.venv", + "frank/.venv", ".venv" ], "python.venvPath": "${workspaceFolder}/barker", diff --git a/barker/barker/core/config.py b/barker/barker/core/config.py index 35f3a70..859de9c 100644 --- a/barker/barker/core/config.py +++ b/barker/barker/core/config.py @@ -22,7 +22,8 @@ class Settings(BaseSettings): ALEMBIC_SQLALCHEMY_LOG_LEVEL: str = "WARN" NATS_URL: str = "nats://127.0.0.1:4222" - WORKER_QUEUE_NAME: str = "arq:queue" + SUBJECT: str = "barker.print" + STREAM: str = "barker_print_stream" model_config = SettingsConfigDict(case_sensitive=True, env_file=".env") diff --git a/barker/barker/core/taskiq.py b/barker/barker/core/taskiq.py index 23dce74..099b574 100644 --- a/barker/barker/core/taskiq.py +++ b/barker/barker/core/taskiq.py @@ -19,24 +19,24 @@ class RoutedJetStreamBroker(PullBasedJetStreamBroker): broker = RoutedJetStreamBroker( settings.NATS_URL, - subject="barker.print.>", - stream_name="barker_print_stream", + subject=f"{settings.SUBJECT}.>", + stream_name=f"{settings.STREAM}", stream_config=StreamConfig( - name="barker_print_stream", - subjects=["barker.print.>"], + name=f"{settings.STREAM}", + subjects=[f"{settings.SUBJECT}.>"], ), ) main_loop: asyncio.AbstractEventLoop | None = None -@broker.task(task_name="sent_to_printer") -async def sent_to_printer(data: str, address: str, cut_code: str) -> None: +@broker.task(task_name="nat_print") +async def nat_print(data: str, cut_code: str) -> None: pass -def enqueue_print_job_sync(printer_name: str, data: str, address: str, cut_code: str) -> None: - coro = sent_to_printer.kicker().with_labels(queue=f"barker.print.{printer_name}").kiq(data, address, cut_code) +def enqueue_print_job_sync(printer_name: str, data: str, cut_code: str) -> None: + coro = nat_print.kicker().with_labels(queue=f"{settings.SUBJECT}.{printer_name}").kiq(data, cut_code) if main_loop is not None and main_loop.is_running(): asyncio.run_coroutine_threadsafe(coro, main_loop) diff --git a/barker/barker/printing/bill.py b/barker/barker/printing/bill.py index f5a9572..d578898 100644 --- a/barker/barker/printing/bill.py +++ b/barker/barker/printing/bill.py @@ -49,9 +49,9 @@ def print_bill(voucher_id: uuid.UUID, db: Session) -> None: if len(bills) > 1: total = design_total(voucher, regimes, db) - enqueue_print_job_sync(printer.name, total, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, total, printer.cut_code) for bill in bills: - enqueue_print_job_sync(printer.name, bill, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, bill, printer.cut_code) def get_inventories(voucher: Voucher) -> list[Inventory]: diff --git a/barker/barker/printing/cashier_report.py b/barker/barker/printing/cashier_report.py index 27f6a6f..4bc15e8 100644 --- a/barker/barker/printing/cashier_report.py +++ b/barker/barker/printing/cashier_report.py @@ -25,8 +25,8 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - enqueue_print_job_sync(printer.name, summary, printer.address, printer.cut_code) - enqueue_print_job_sync(printer.name, details, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, summary, printer.cut_code) + enqueue_print_job_sync(printer.name, details, printer.cut_code) def design_cashier_report_summary(report: CashierReport) -> str: diff --git a/barker/barker/printing/discount_report.py b/barker/barker/printing/discount_report.py index 2130299..425873e 100644 --- a/barker/barker/printing/discount_report.py +++ b/barker/barker/printing/discount_report.py @@ -21,7 +21,7 @@ def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Sess .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, data, printer.cut_code) def design_discount_report(report: DiscountReport) -> str: diff --git a/barker/barker/printing/kot.py b/barker/barker/printing/kot.py index daf71c5..7d6272c 100644 --- a/barker/barker/printing/kot.py +++ b/barker/barker/printing/kot.py @@ -83,4 +83,4 @@ def print_kot(voucher_id: uuid.UUID, db: Session) -> None: printer, items = value for c in range(int(copies)): data = design_kot(voucher, kot, items, c) - enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, data, printer.cut_code) diff --git a/barker/barker/printing/product_sale_report.py b/barker/barker/printing/product_sale_report.py index 40b4a2f..aa6f95d 100644 --- a/barker/barker/printing/product_sale_report.py +++ b/barker/barker/printing/product_sale_report.py @@ -23,7 +23,7 @@ def print_product_sale_report(report: ProductSaleReport, device_id: uuid.UUID, d .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, data, printer.cut_code) def design_product_sale_report(report: ProductSaleReport) -> str: diff --git a/barker/barker/printing/sale_report.py b/barker/barker/printing/sale_report.py index b829d9e..0397f61 100644 --- a/barker/barker/printing/sale_report.py +++ b/barker/barker/printing/sale_report.py @@ -24,7 +24,7 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session) -> .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() - enqueue_print_job_sync(printer.name, data, printer.address, printer.cut_code) + enqueue_print_job_sync(printer.name, data, printer.cut_code) def design_sale_report(report: SaleReport) -> str: diff --git a/barker/barker/routers/temporal_product.py b/barker/barker/routers/temporal_product.py index 695c5c0..4bf4978 100644 --- a/barker/barker/routers/temporal_product.py +++ b/barker/barker/routers/temporal_product.py @@ -82,15 +82,6 @@ def check_gaps(data: TemporalProduct) -> None: skus: dict[uuid.UUID, list[SkuModel]] = defaultdict(list) for sku in data.skus: skus[sku.id_].append(sku) - for sku_list in skus.values(): - if ( - data.products[0].valid_from != sku_list[0].valid_from - or data.products[-1].valid_till != sku_list[-1].valid_till - ): - raise HTTPException( - status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, - detail="Product and SKU versions validity do not match", - ) for i, p_item in enumerate(data.products[1:], start=1): if data.products[i - 1].valid_till + timedelta(days=1) != p_item.valid_from: # type: ignore[operator] raise HTTPException(