diff --git a/api/management/commands/follow_invoices.py b/api/management/commands/follow_invoices.py index 48a5581c..46eecf50 100644 --- a/api/management/commands/follow_invoices.py +++ b/api/management/commands/follow_invoices.py @@ -139,13 +139,14 @@ class Command(BaseCommand): queryset = LNPayment.objects.filter( type=LNPayment.Types.NORM, status=LNPayment.Status.FLIGHT, + in_flight=False, routing_attempts=0, ) queryset_retries = LNPayment.objects.filter( type=LNPayment.Types.NORM, status__in=[LNPayment.Status.VALIDI, LNPayment.Status.FAILRO], - routing_attempts__lt=5, + in_flight=False, last_routing_time__lt=( timezone.now() - timedelta(minutes=int(config("RETRY_TIME")))), ) @@ -153,9 +154,9 @@ class Command(BaseCommand): queryset = queryset.union(queryset_retries) for lnpayment in queryset: - success, _ = follow_send_payment.delay(lnpayment.payment_hash) + success, _ = follow_send_payment(lnpayment.payment_hash) - # If failed, reset mision control. (This won't scale well, just a temporary fix) + # If failed, reset mission control. (This won't scale well, just a temporary fix) if not success: LNNode.resetmc() diff --git a/api/models.py b/api/models.py index 82f854cc..35064586 100644 --- a/api/models.py +++ b/api/models.py @@ -125,7 +125,7 @@ class LNPayment(models.Model): last_routing_time = models.DateTimeField(null=True, default=None, blank=True) - + in_flight = models.BooleanField(default=False, null=False, blank=False) # involved parties sender = models.ForeignKey(User, related_name="sender", diff --git a/api/tasks.py b/api/tasks.py index a44f57d3..8438f13e 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -95,11 +95,13 @@ def follow_send_payment(hash): ]): if response.status == 0: # Status 0 'UNKNOWN' # Not sure when this status happens - pass + lnpayment.in_flight = False + lnpayment.save() if response.status == 1: # Status 1 'IN_FLIGHT' print("IN_FLIGHT") lnpayment.status = LNPayment.Status.FLIGHT + lnpayment.in_flight = True lnpayment.save() order.status = Order.Status.PAY order.save() @@ -109,6 +111,7 @@ def follow_send_payment(hash): lnpayment.status = LNPayment.Status.FAILRO lnpayment.last_routing_time = timezone.now() lnpayment.routing_attempts += 1 + lnpayment.in_flight = False lnpayment.save() order.status = Order.Status.FAI order.expires_at = timezone.now() + timedelta( @@ -116,7 +119,8 @@ def follow_send_payment(hash): order.save() context = { "routing_failed": - LNNode.payment_failure_context[response.failure_reason] + LNNode.payment_failure_context[response.failure_reason], + "IN_FLIGHT":False, } print(context) return False, context @@ -130,13 +134,14 @@ def follow_send_payment(hash): order.expires_at = timezone.now() + timedelta( seconds=order.t_to_expire(Order.Status.SUC)) order.save() - return True, None + return True, {"IN_FLIGHT":False} except Exception as e: if "invoice expired" in str(e): print("INVOICE EXPIRED") lnpayment.status = LNPayment.Status.EXPIRE lnpayment.last_routing_time = timezone.now() + lnpayment.in_flight = False lnpayment.save() order.status = Order.Status.FAI order.expires_at = timezone.now() + timedelta(