Skip to content

Commit

Permalink
Add preferred host zone for re-charge orders
Browse files Browse the repository at this point in the history
  • Loading branch information
eloravpn committed Nov 15, 2024
1 parent eee7b4c commit 91a9d78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
)
GLOBAL_TRAFFIC_RATIO = config("GLOBAL_TRAFFIC_RATIO", cast=float, default=1.0)

ENABLE_ORDER_JOBS = config("ENABLE_ORDER_JOBS", cast=bool, default=False)
ENABLE_ORDER_JOBS = config("ENABLE_ORDER_JOBS", cast=bool, default=True)

PROCESS_PAID_ORDERS_INTERVAL = config(
"PROCESS_PAID_ORDERS_INTERVAL", cast=int, default=60
Expand Down
21 changes: 16 additions & 5 deletions src/jobs/order_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@


def _get_random_available_host_zone(
db: Session, host_zones: List[HostZone]
db: Session, host_zones: List[HostZone], preferred_host_zone: HostZone
) -> HostZone:
random.shuffle(host_zones)

for db_host_zone in host_zones:
if (
preferred_host_zone is not None
and preferred_host_zone.id == db_host_zone.id
):
logger.warn(f"Preferred host zone {preferred_host_zone.name} selected.")
return db_host_zone

for db_host_zone in host_zones:
db_accounts, count = get_accounts(
db=db, filter_enable=True, enable=True, host_zone_id=db_host_zone.id
Expand All @@ -51,22 +59,25 @@ def process_paid_orders():
if db_order.service_id:

db_service = db_order.service
db_account = db_order.account
db_user = db_order.user

if db_service.host_zones is None:
logger.error(f"Host zone is empty in service {db_service.name}")

db_host_zone = _get_random_available_host_zone(
db=db, host_zones=db_service.host_zones
db=db,
host_zones=db_service.host_zones,
preferred_host_zone=(
None if db_account is None else db_account.host_zone
),
)
if db_host_zone is None:
logger.error(
f"All host zones in service {db_service.name} are Full!"
)
continue

db_account = db_order.account
db_user = db_order.user

today = datetime.now()
expired_at = today + timedelta(days=db_order.duration)

Expand Down
6 changes: 1 addition & 5 deletions src/notification/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ def create_notification(
status=notification.status,
engine=notification.engine,
type=notification.type,
keyboard=(
json.loads(notification.keyboard)
if notification.keyboard
else None
),
keyboard=(json.loads(notification.keyboard) if notification.keyboard else None),
photo_url=notification.photo_url,
)

Expand Down

0 comments on commit 91a9d78

Please sign in to comment.