Skip to content

Commit

Permalink
Fix bug during the order payment retry
Browse files Browse the repository at this point in the history
  • Loading branch information
cmin764 committed Dec 1, 2024
1 parent 823718f commit 28e57b0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
LOG_LEVEL=INFO
DEBUG=false

# Postgres
POSTGRES_SERVER=localhost
Expand Down
1 change: 1 addition & 0 deletions deep_ice/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Settings(BaseSettings):
)

LOG_LEVEL: str = "INFO"
DEBUG: bool = False
PROJECT_NAME: str = "Deep Ice"
API_V1_STR: str = "/v1"

Expand Down
2 changes: 1 addition & 1 deletion deep_ice/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from deep_ice.core.config import settings

async_engine = create_async_engine(
str(settings.SQLALCHEMY_DATABASE_URI), echo=True, future=True
str(settings.SQLALCHEMY_DATABASE_URI), echo=settings.DEBUG, future=True
)


Expand Down
12 changes: 8 additions & 4 deletions deep_ice/services/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def make_payment_task(
msg = f"{method.value} payment for order #{order_id} failed, retrying..."
logger.warning(msg)
sentry_sdk.capture_message(msg, level="warning")
raise Retry(defer=attempts * settings.TASK_BACKOFF_FACTOR)
raise Retry(defer=attempts * settings.TASK_BACKOFF_FACTOR)

async for session in get_async_session():
order_service = OrderService(session, stats_service=stats_service)
Expand Down Expand Up @@ -82,7 +82,9 @@ class PaymentStub(PaymentInterface):

min_delay: int
max_delay: int
allow_failures: bool = False # enable failures or not
# Enable failures (or not) and at what rate.
allow_failures: bool = False
failure_rate: float = 0.2

async def make_payment(
self,
Expand Down Expand Up @@ -124,7 +126,9 @@ async def make_payment(
if self.allow_failures:
# Simulate payment result: 80% chance of success, 20% chance of failure.
payment_result = random.choices(
[PaymentStatus.SUCCESS, PaymentStatus.FAILED], weights=[80, 20], k=1
[PaymentStatus.SUCCESS, PaymentStatus.FAILED],
weights=[1 - self.failure_rate, self.failure_rate],
k=1,
)[0]
else:
payment_result = PaymentStatus.SUCCESS
Expand Down Expand Up @@ -196,4 +200,4 @@ async def set_order_payment_status(self, order_id: int, status: PaymentStatus):
self._session.add(payment)


payment_stub = PaymentStub(1, 3, allow_failures=True)
payment_stub = PaymentStub(1, 3, allow_failures=True, failure_rate=0.2)
2 changes: 1 addition & 1 deletion tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,5 @@ def run_worker(ctx, develop: bool = False):
"""Run a worker for processing the task queue in production or development mode."""
params = ""
if develop:
params = "--watch deep_ice"
params = f"--watch {APP_PACKAGE}"
uv_run(ctx, f"arq {APP_PACKAGE}.TaskQueue {params}", "Task queue worker")

0 comments on commit 28e57b0

Please sign in to comment.