mirror of
https://github.com/krateng/maloja.git
synced 2025-08-05 08:40:00 -04:00
Added regular database sync
This commit is contained in:
parent
2221365536
commit
538a5bcba9
17
database.py
17
database.py
@ -12,6 +12,8 @@ SCROBBLES = [] # Format: tuple(track_ref,timestamp,saved)
|
|||||||
ARTISTS = [] # Format: artist
|
ARTISTS = [] # Format: artist
|
||||||
TRACKS = [] # Format: tuple(frozenset(artist_ref,...),title)
|
TRACKS = [] # Format: tuple(frozenset(artist_ref,...),title)
|
||||||
|
|
||||||
|
lastsync = 0
|
||||||
|
|
||||||
|
|
||||||
# by id
|
# by id
|
||||||
#def getScrobbleObject(o):
|
#def getScrobbleObject(o):
|
||||||
@ -124,17 +126,21 @@ def post_scrobble():
|
|||||||
|
|
||||||
createScrobble(artists,title,time)
|
createScrobble(artists,title,time)
|
||||||
|
|
||||||
|
if (time - lastsync) > 3600:
|
||||||
|
sync()
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@route("/flush")
|
@route("/sync")
|
||||||
def abouttoshutdown():
|
def abouttoshutdown():
|
||||||
flush()
|
sync()
|
||||||
print("Database saved to disk.")
|
print("Database saved to disk.")
|
||||||
#sys.exit()
|
#sys.exit()
|
||||||
|
|
||||||
# Starts the server
|
# Starts the server
|
||||||
def runserver(DATABASE_PORT):
|
def runserver(DATABASE_PORT):
|
||||||
|
global lastsync
|
||||||
|
lastsync = time = int(datetime.datetime.now(tz=datetime.timezone.utc).timestamp())
|
||||||
#reload()
|
#reload()
|
||||||
#buildh()
|
#buildh()
|
||||||
build_db()
|
build_db()
|
||||||
@ -272,7 +278,7 @@ def reload():
|
|||||||
DATABASE.append({"artists":artists,"title":title,"time":time,"saved":True})
|
DATABASE.append({"artists":artists,"title":title,"time":time,"saved":True})
|
||||||
|
|
||||||
# Saves all cached entries to disk
|
# Saves all cached entries to disk
|
||||||
def flush():
|
def sync():
|
||||||
for idx in range(len(SCROBBLES)):
|
for idx in range(len(SCROBBLES)):
|
||||||
if not SCROBBLES[idx][2]:
|
if not SCROBBLES[idx][2]:
|
||||||
|
|
||||||
@ -290,6 +296,9 @@ def flush():
|
|||||||
|
|
||||||
SCROBBLES[idx] = (SCROBBLES[idx][0],SCROBBLES[idx][1],True)
|
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
|
# Queries the database
|
||||||
def db_query(artist=None,track=None,since=0,to=9999999999):
|
def db_query(artist=None,track=None,since=0,to=9999999999):
|
||||||
|
@ -5,6 +5,7 @@ import waitress
|
|||||||
import urllib.request
|
import urllib.request
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import sys
|
import sys
|
||||||
|
import signal
|
||||||
|
|
||||||
|
|
||||||
MAIN_PORT = 12345
|
MAIN_PORT = 12345
|
||||||
@ -35,7 +36,10 @@ def database(pth):
|
|||||||
|
|
||||||
@route("/exit")
|
@route("/exit")
|
||||||
def shutdown():
|
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...")
|
print("Server shutting down...")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
@ -46,6 +50,7 @@ def static(pth):
|
|||||||
return static_file(pth,root="")
|
return static_file(pth,root="")
|
||||||
|
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, graceful_exit)
|
||||||
|
|
||||||
## start database server
|
## start database server
|
||||||
_thread.start_new_thread(SourceFileLoader("database","database.py").load_module().runserver,(DATABASE_PORT,))
|
_thread.start_new_thread(SourceFileLoader("database","database.py").load_module().runserver,(DATABASE_PORT,))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user