2022-10-25 18:04:12 +00:00
|
|
|
import time
|
|
|
|
|
2022-10-20 20:53:51 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
2022-10-25 18:04:12 +00:00
|
|
|
from django.utils import timezone
|
2022-01-17 18:11:44 +00:00
|
|
|
|
|
|
|
from api.logics import Logics
|
2022-10-25 18:04:12 +00:00
|
|
|
from api.models import Order
|
2022-01-17 18:11:44 +00:00
|
|
|
|
2022-02-17 19:50:10 +00:00
|
|
|
|
2022-01-17 18:11:44 +00:00
|
|
|
class Command(BaseCommand):
|
2022-02-17 19:50:10 +00:00
|
|
|
help = "Follows all active hold invoices"
|
2022-01-17 18:11:44 +00:00
|
|
|
|
|
|
|
# def add_arguments(self, parser):
|
|
|
|
# parser.add_argument('debug', nargs='+', type=boolean)
|
|
|
|
|
2022-01-20 20:50:25 +00:00
|
|
|
def clean_orders(self, *args, **options):
|
2022-02-17 19:50:10 +00:00
|
|
|
"""Continuously checks order expiration times for 1 hour. If order
|
|
|
|
has expires, it calls the logics module for expiration handling."""
|
2022-01-17 18:11:44 +00:00
|
|
|
|
2022-02-17 19:50:10 +00:00
|
|
|
do_nothing = [
|
|
|
|
Order.Status.UCA,
|
|
|
|
Order.Status.EXP,
|
|
|
|
Order.Status.DIS,
|
|
|
|
Order.Status.CCA,
|
|
|
|
Order.Status.PAY,
|
|
|
|
Order.Status.SUC,
|
|
|
|
Order.Status.FAI,
|
|
|
|
Order.Status.MLD,
|
|
|
|
Order.Status.TLD,
|
|
|
|
Order.Status.WFR,
|
|
|
|
]
|
2022-01-17 18:11:44 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
time.sleep(5)
|
|
|
|
|
|
|
|
queryset = Order.objects.exclude(status__in=do_nothing)
|
2022-02-17 19:50:10 +00:00
|
|
|
queryset = queryset.filter(
|
2022-10-20 09:56:10 +00:00
|
|
|
expires_at__lt=timezone.now()
|
|
|
|
) # expires at lower than now
|
2022-01-17 18:11:44 +00:00
|
|
|
|
|
|
|
debug = {}
|
2022-02-17 19:50:10 +00:00
|
|
|
debug["num_expired_orders"] = len(queryset)
|
|
|
|
debug["expired_orders"] = []
|
2022-02-20 14:38:29 +00:00
|
|
|
debug["failed_order_expiry"] = []
|
|
|
|
debug["reason_failure"] = []
|
2022-01-17 18:11:44 +00:00
|
|
|
|
|
|
|
for idx, order in enumerate(queryset):
|
2022-10-20 09:56:10 +00:00
|
|
|
context = str(order) + " was " + Order.Status(order.status).label
|
2022-01-19 19:37:10 +00:00
|
|
|
try:
|
2022-10-20 09:56:10 +00:00
|
|
|
if Logics.order_expires(order): # Order send to expire here
|
2022-02-17 19:50:10 +00:00
|
|
|
debug["expired_orders"].append({idx: context})
|
|
|
|
|
|
|
|
# It should not happen, but if it cannot locate the hold invoice
|
2022-01-20 17:30:29 +00:00
|
|
|
# it probably was cancelled by another thread, make it expire anyway.
|
2022-01-19 19:37:10 +00:00
|
|
|
except Exception as e:
|
2022-02-20 14:38:29 +00:00
|
|
|
debug["failed_order_expiry"].append({idx: context})
|
|
|
|
debug["reason_failure"].append({idx: str(e)})
|
2022-10-20 09:56:10 +00:00
|
|
|
|
2022-02-17 19:50:10 +00:00
|
|
|
if "unable to locate invoice" in str(e):
|
2022-01-19 19:37:10 +00:00
|
|
|
self.stdout.write(str(e))
|
|
|
|
order.status = Order.Status.EXP
|
2023-05-08 18:10:37 +00:00
|
|
|
order.save(update_fields=["status"])
|
2022-02-17 19:50:10 +00:00
|
|
|
debug["expired_orders"].append({idx: context})
|
2022-01-17 23:11:41 +00:00
|
|
|
|
2022-02-17 19:50:10 +00:00
|
|
|
if debug["num_expired_orders"] > 0:
|
2022-01-17 23:11:41 +00:00
|
|
|
self.stdout.write(str(timezone.now()))
|
|
|
|
self.stdout.write(str(debug))
|
2022-02-17 19:50:10 +00:00
|
|
|
|
2022-01-20 20:50:25 +00:00
|
|
|
def handle(self, *args, **options):
|
2023-05-08 18:10:37 +00:00
|
|
|
"""Never mind database locked error, keep going, print them out.
|
|
|
|
Not an issue with PostgresQL"""
|
2022-01-20 20:50:25 +00:00
|
|
|
try:
|
|
|
|
self.clean_orders()
|
|
|
|
except Exception as e:
|
2022-02-17 19:50:10 +00:00
|
|
|
if "database is locked" in str(e):
|
|
|
|
self.stdout.write("database is locked")
|
|
|
|
|
2022-01-24 17:54:44 +00:00
|
|
|
self.stdout.write(str(e))
|