2022-01-16 15:31:25 +03:00
|
|
|
from celery import shared_task
|
|
|
|
|
|
|
|
from .lightning.node import LNNode
|
|
|
|
from django.contrib.auth.models import User
|
2022-01-16 19:06:53 +03:00
|
|
|
from .models import LNPayment, Order, Currency
|
2022-01-16 15:31:25 +03:00
|
|
|
from .logics import Logics
|
2022-01-16 18:18:23 +03:00
|
|
|
from .utils import get_exchange_rates
|
2022-01-16 15:31:25 +03:00
|
|
|
|
2022-01-16 18:18:23 +03:00
|
|
|
from django.db.models import Q
|
2022-01-16 15:31:25 +03:00
|
|
|
from datetime import timedelta
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2022-01-16 21:32:34 +03:00
|
|
|
import time
|
2022-01-16 15:31:25 +03:00
|
|
|
|
|
|
|
@shared_task(name="users_cleansing")
|
|
|
|
def users_cleansing():
|
|
|
|
'''
|
|
|
|
Deletes users never used 12 hours after creation
|
|
|
|
'''
|
|
|
|
# Users who's last login has not been in the last 12 hours
|
|
|
|
active_time_range = (timezone.now() - timedelta(hours=12), timezone.now())
|
|
|
|
queryset = User.objects.filter(~Q(last_login__range=active_time_range))
|
2022-01-16 22:16:11 +03:00
|
|
|
queryset = queryset(is_staff=False) # Do not delete staff users
|
2022-01-16 15:31:25 +03:00
|
|
|
|
|
|
|
# And do not have an active trade or any pass finished trade.
|
|
|
|
deleted_users = []
|
|
|
|
for user in queryset:
|
|
|
|
if not user.profile.total_contracts == 0:
|
|
|
|
continue
|
|
|
|
valid, _ = Logics.validate_already_maker_or_taker(user)
|
|
|
|
if valid:
|
|
|
|
deleted_users.append(str(user))
|
|
|
|
user.delete()
|
|
|
|
|
|
|
|
results = {
|
|
|
|
'num_deleted': len(deleted_users),
|
|
|
|
'deleted_users': deleted_users,
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2022-01-16 18:18:23 +03:00
|
|
|
@shared_task(name="orders_expire")
|
2022-01-16 21:32:34 +03:00
|
|
|
def orders_expire(rest_secs):
|
|
|
|
'''
|
2022-01-17 00:54:42 +03:00
|
|
|
Continuously checks order expiration times for 1 hour. If order
|
|
|
|
has expires, it calls the logics module for expiration handling.
|
2022-01-16 21:32:34 +03:00
|
|
|
'''
|
|
|
|
now = timezone.now()
|
|
|
|
end_time = now + timedelta(hours=1)
|
|
|
|
context = []
|
|
|
|
|
|
|
|
while now < end_time:
|
|
|
|
queryset = Order.objects.exclude(status=Order.Status.EXP).exclude(status=Order.Status.UCA).exclude(status= Order.Status.CCA)
|
|
|
|
queryset = queryset.filter(expires_at__lt=now) # expires at lower than now
|
|
|
|
|
|
|
|
for order in queryset:
|
2022-01-17 00:54:42 +03:00
|
|
|
if Logics.order_expires(order): # Order send to expire here
|
|
|
|
context.append(str(order)+ " was "+ Order.Status(order.status).label)
|
2022-01-16 21:32:34 +03:00
|
|
|
|
|
|
|
# Allow for some thread rest.
|
|
|
|
time.sleep(rest_secs)
|
|
|
|
|
|
|
|
# Update 'now' for a new loop
|
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
results = {
|
|
|
|
'num_expired': len(context),
|
|
|
|
'expired_orders_context': context,
|
|
|
|
'rest_param': rest_secs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
2022-01-16 15:31:25 +03:00
|
|
|
|
|
|
|
@shared_task
|
|
|
|
def follow_lnd_payment():
|
2022-01-16 21:32:34 +03:00
|
|
|
''' Makes a payment and follows it.
|
|
|
|
Updates the LNpayment object, and retries
|
|
|
|
until payment is done'''
|
2022-01-17 00:54:42 +03:00
|
|
|
|
2022-01-16 15:31:25 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
@shared_task
|
2022-01-16 21:32:34 +03:00
|
|
|
def follow_lnd_hold_invoice():
|
|
|
|
''' Follows and updates LNpayment object
|
|
|
|
until settled or canceled'''
|
2022-01-17 00:54:42 +03:00
|
|
|
|
2022-01-16 15:31:25 +03:00
|
|
|
pass
|
|
|
|
|
2022-01-16 21:32:34 +03:00
|
|
|
@shared_task(name="cache_external_market_prices", ignore_result=True)
|
2022-01-16 15:31:25 +03:00
|
|
|
def cache_market():
|
2022-01-16 19:06:53 +03:00
|
|
|
exchange_rates = get_exchange_rates(list(Currency.currency_dict.values()))
|
2022-01-16 18:18:23 +03:00
|
|
|
results = {}
|
2022-01-16 19:06:53 +03:00
|
|
|
for val in Currency.currency_dict:
|
2022-01-16 18:18:23 +03:00
|
|
|
rate = exchange_rates[int(val)-1] # currecies are indexed starting at 1 (USD)
|
2022-01-16 19:06:53 +03:00
|
|
|
results[val] = {Currency.currency_dict[val], rate}
|
2022-01-16 22:16:11 +03:00
|
|
|
if str(rate) == 'nan': continue # Do not update if no new rate was found
|
2022-01-16 18:18:23 +03:00
|
|
|
|
|
|
|
# Create / Update database cached prices
|
2022-01-16 19:06:53 +03:00
|
|
|
Currency.objects.update_or_create(
|
|
|
|
id = int(val),
|
2022-01-16 18:18:23 +03:00
|
|
|
currency = int(val),
|
2022-01-16 21:32:34 +03:00
|
|
|
# if there is a Cached market prices matching that id, it updates it with defaults below
|
2022-01-16 18:18:23 +03:00
|
|
|
defaults = {
|
2022-01-16 22:16:11 +03:00
|
|
|
'exchange_rate': float(rate),
|
2022-01-16 18:18:23 +03:00
|
|
|
'timestamp': timezone.now(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return results
|