1
0
mirror of https://github.com/krateng/maloja.git synced 2023-08-10 21:12:55 +03:00

Added regular database sync

This commit is contained in:
Krateng 2018-11-28 13:02:43 +01:00
parent 2221365536
commit 538a5bcba9
2 changed files with 19 additions and 5 deletions

View File

@ -12,6 +12,8 @@ SCROBBLES = [] # Format: tuple(track_ref,timestamp,saved)
ARTISTS = [] # Format: artist
TRACKS = [] # Format: tuple(frozenset(artist_ref,...),title)
lastsync = 0
# by id
#def getScrobbleObject(o):
@ -124,17 +126,21 @@ def post_scrobble():
createScrobble(artists,title,time)
if (time - lastsync) > 3600:
sync()
return ""
@route("/flush")
@route("/sync")
def abouttoshutdown():
flush()
sync()
print("Database saved to disk.")
#sys.exit()
# Starts the server
def runserver(DATABASE_PORT):
global lastsync
lastsync = time = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
#reload()
#buildh()
build_db()
@ -272,7 +278,7 @@ def reload():
DATABASE.append({"artists":artists,"title":title,"time":time,"saved":True})
# Saves all cached entries to disk
def flush():
def sync():
for idx in range(len(SCROBBLES)):
if not SCROBBLES[idx][2]:
@ -290,6 +296,9 @@ def flush():
SCROBBLES[idx] = (SCROBBLES[idx][0],SCROBBLES[idx][1],True)
global lastsync
lastsync = time = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
# Queries the database
def db_query(artist=None,track=None,since=0,to=9999999999):

View File

@ -5,6 +5,7 @@ import waitress
import urllib.request
import urllib.parse
import sys
import signal
MAIN_PORT = 12345
@ -35,7 +36,10 @@ def database(pth):
@route("/exit")
def shutdown():
urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/flush")
graceful_exit()
def graceful_exit(sig=None,frame=None):
urllib.request.urlopen("http://localhost:" + str(DATABASE_PORT) + "/sync")
print("Server shutting down...")
sys.exit()
@ -46,6 +50,7 @@ def static(pth):
return static_file(pth,root="")
signal.signal(signal.SIGINT, graceful_exit)
## start database server
_thread.start_new_thread(SourceFileLoader("database","database.py").load_module().runserver,(DATABASE_PORT,))