Add background task for expired orders removal

This commit is contained in:
Reckless_Satoshi 2022-01-16 10:32:34 -08:00
parent 2cbc82a535
commit 28bfaee937
No known key found for this signature in database
GPG Key ID: 9C4585B561315571
6 changed files with 75 additions and 20 deletions

View File

@ -45,7 +45,8 @@ class UserProfileAdmin(AdminChangeLinksMixin, admin.ModelAdmin):
@admin.register(Currency) @admin.register(Currency)
class CurrencieAdmin(admin.ModelAdmin): class CurrencieAdmin(admin.ModelAdmin):
list_display = ('currency','exchange_rate','timestamp') list_display = ('id','currency','exchange_rate','timestamp')
list_display_links = ('id','currency')
readonly_fields = ('currency','exchange_rate','timestamp') readonly_fields = ('currency','exchange_rate','timestamp')
@admin.register(MarketTick) @admin.register(MarketTick)

View File

@ -95,13 +95,23 @@ class Logics():
return price, premium return price, premium
def order_expires(order): @classmethod
def order_expires(cls, order):
''' General case when time runs out. Only ''' General case when time runs out. Only
used when the maker does not lock a publishing bond''' used when the maker does not lock a publishing bond'''
order.status = Order.Status.EXP
order.maker = None if order.status == Order.Status.WFB:
order.taker = None order.status = Order.Status.EXP
order.save() order.maker = None
order.taker = None
order.save()
if order.status == Order.Status.PUB:
cls.return_bond(order.maker_bond)
order.status = Order.Status.EXP
order.maker = None
order.taker = None
order.save()
def kick_taker(order): def kick_taker(order):
''' The taker did not lock the taker_bond. Now he has to go''' ''' The taker did not lock the taker_bond. Now he has to go'''

View File

@ -11,6 +11,7 @@ from datetime import timedelta
from django.utils import timezone from django.utils import timezone
from decouple import config from decouple import config
import time
@shared_task(name="users_cleansing") @shared_task(name="users_cleansing")
def users_cleansing(): def users_cleansing():
@ -42,18 +43,51 @@ def users_cleansing():
@shared_task(name="orders_expire") @shared_task(name="orders_expire")
def orders_expire(): def orders_expire(rest_secs):
pass '''
Continuously checks order expiration times for 1 hour.
If order is expires, it handles the actions.
'''
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:
context.append(str(order)+ " was "+ Order.Status(order.status).label)
Logics.order_expires(order)
# 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
@shared_task @shared_task
def follow_lnd_payment(): def follow_lnd_payment():
''' Makes a payment and follows it.
Updates the LNpayment object, and retries
until payment is done'''
pass pass
@shared_task @shared_task
def query_all_lnd_invoices(): def follow_lnd_hold_invoice():
''' Follows and updates LNpayment object
until settled or canceled'''
pass pass
@shared_task(name="cache_market", ignore_result=True) @shared_task(name="cache_external_market_prices", ignore_result=True)
def cache_market(): def cache_market():
exchange_rates = get_exchange_rates(list(Currency.currency_dict.values())) exchange_rates = get_exchange_rates(list(Currency.currency_dict.values()))
results = {} results = {}
@ -65,7 +99,7 @@ def cache_market():
Currency.objects.update_or_create( Currency.objects.update_or_create(
id = int(val), id = int(val),
currency = int(val), currency = int(val),
# if there is a Cached Exchange rate matching that value, it updates it with defaults below # if there is a Cached market prices matching that id, it updates it with defaults below
defaults = { defaults = {
'exchange_rate': rate, 'exchange_rate': rate,
'timestamp': timezone.now(), 'timestamp': timezone.now(),

View File

@ -2,6 +2,8 @@ import React, { Component } from 'react';
import { Paper, Alert, AlertTitle, Button , Grid, Typography, TextField, Select, FormHelperText, MenuItem, FormControl, Radio, FormControlLabel, RadioGroup, Menu} from "@mui/material" import { Paper, Alert, AlertTitle, Button , Grid, Typography, TextField, Select, FormHelperText, MenuItem, FormControl, Radio, FormControlLabel, RadioGroup, Menu} from "@mui/material"
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import getFlags from './getFlags'
function getCookie(name) { function getCookie(name) {
let cookieValue = null; let cookieValue = null;
if (document.cookie && document.cookie !== '') { if (document.cookie && document.cookie !== '') {
@ -190,7 +192,9 @@ export default class MakerPage extends Component {
> >
{ {
Object.entries(this.state.currencies_dict) Object.entries(this.state.currencies_dict)
.map( ([key, value]) => <MenuItem value={parseInt(key)}>{value}</MenuItem> ) .map( ([key, value]) => <MenuItem value={parseInt(key)}>
{getFlags(value) + " " + value}
</MenuItem> )
} }
</Select> </Select>

View File

@ -2,6 +2,7 @@ import React, { Component } from "react";
import { Alert, Paper, CircularProgress, Button , Grid, Typography, List, ListItem, ListItemIcon, ListItemText, ListItemAvatar, Avatar, Divider, Box, LinearProgress} from "@mui/material" import { Alert, Paper, CircularProgress, Button , Grid, Typography, List, ListItem, ListItemIcon, ListItemText, ListItemAvatar, Avatar, Divider, Box, LinearProgress} from "@mui/material"
import Countdown, { zeroPad, calcTimeDelta } from 'react-countdown'; import Countdown, { zeroPad, calcTimeDelta } from 'react-countdown';
import TradeBox from "./TradeBox"; import TradeBox from "./TradeBox";
import getFlags from './getFlags'
// icons // icons
import AccessTimeIcon from '@mui/icons-material/AccessTime'; import AccessTimeIcon from '@mui/icons-material/AccessTime';
@ -281,9 +282,10 @@ export default class OrderPage extends Component {
<ListItem> <ListItem>
<ListItemIcon> <ListItemIcon>
<MoneyIcon/> {getFlags(this.state.currencyCode)}
</ListItemIcon> </ListItemIcon>
<ListItemText primary={parseFloat(parseFloat(this.state.amount).toFixed(4))+" "+this.state.currencyCode} secondary="Amount"/> <ListItemText primary={parseFloat(parseFloat(this.state.amount).toFixed(4))
+" "+this.state.currencyCode} secondary="Amount"/>
</ListItem> </ListItem>
<Divider /> <Divider />
<ListItem> <ListItem>

View File

@ -32,14 +32,18 @@ app.conf.beat_scheduler = 'django_celery_beat.schedulers:DatabaseScheduler'
# Configure the periodic tasks # Configure the periodic tasks
app.conf.beat_schedule = { app.conf.beat_schedule = {
# User cleansing every 6 hours # User cleansing every 6 hours
'users-cleansing': { 'users-cleansing': { # Cleans abandoned users every 6 hours
'task': 'users_cleansing', 'task': 'users_cleansing',
'schedule': timedelta(hours=6), 'schedule': timedelta(hours=6),
}, },
'cache-market-prices': { # Cache market prices every minutes for now.
'cache-market-rates': { 'task': 'cache_external_market_prices',
'task': 'cache_market', 'schedule': timedelta(seconds=60),
'schedule': timedelta(seconds=60), # Cache market prices every minutes for now. },
'orders_expire': { # Continuous order expire removal (1 hour long process, every hour reports results)
'task': 'orders_expire',
'schedule': timedelta(hours=1),
'args': [5], # Rest between checks (secs)
}, },
} }