add wallet cleanup functionality

mm-logging
lza_menace 4 years ago
parent 46189e716d
commit 9c51cabdfa

@ -84,6 +84,7 @@ def connect():
current_user.wallet_connected = docker.container_exists(wallet)
current_user.wallet_port = port
current_user.wallet_container = wallet
current_user.wallet_start = datetime.utcnow()
db.session.commit()
return 'ok'

@ -3,6 +3,8 @@ from docker.errors import NotFound, NullResource, APIError
from socket import socket
from os.path import expanduser
from secrets import token_urlsafe
from datetime import datetime, timedelta
from time import sleep
from wowstash import config
from wowstash.models import User
from wowstash.factory import db
@ -140,9 +142,20 @@ class Docker(object):
def cleanup(self):
users = User.query.all()
for u in users:
if u.wallet_container:
if not self.container_exists(u.wallet_container):
u.clear_wallet_data()
# Delete inactive wallet sessions
if u.wallet_start:
session_lifetime = getattr(config, 'PERMANENT_SESSION_LIFETIME', 3600)
expiration_time = u.wallet_start + timedelta(seconds=session_lifetime)
now = datetime.utcnow()
time_diff = expiration_time - now
if time_diff.total_seconds() <= 0:
print(f'[+] Found expired container for {u}. killing it')
self.stop_container(u.wallet_container)
sleep(2)
# Remove wallet db data if not running but it's in db
if u.wallet_container and not self.container_exists(u.wallet_container):
print(f'[+] Found stale data for {u}')
u.clear_wallet_data()
docker = Docker()

@ -19,6 +19,7 @@ class User(db.Model):
wallet_connected = db.Column(db.Boolean, default=False)
wallet_port = db.Column(db.Integer, nullable=True)
wallet_container = db.Column(db.String(30), nullable=True)
wallet_start = db.Column(db.DateTime, nullable=True)
@property
def is_authenticated(self):
@ -43,6 +44,7 @@ class User(db.Model):
self.wallet_connected = False
self.wallet_port = None
self.wallet_container = None
self.wallet_start = None
if reset_password:
self.wallet_password = None
if reset_wallet: