minor backend rework

This commit is contained in:
Jörn-Michael Miehe 2019-06-04 18:01:32 +02:00
parent e963576e46
commit f8b73e38c2
3 changed files with 32 additions and 27 deletions

View file

@ -17,14 +17,12 @@ messages =
class FFTCGDB class FFTCGDB
constructor: (filename, truncate) -> constructor: (filename, truncate) ->
@filename = filename @db = new sqlite3.Database filename, (err) =>
@db = new sqlite3.Database @filename, (err) =>
if err if err
logger.error err.message logger.error err.message
else else
logger.info "OK open '#{@filename}'" logger.info "OK opened '#{filename}'"
@db.run 'PRAGMA foreign_keys = ON;', (err) => @db.run 'PRAGMA foreign_keys = ON;', (err) =>
logger.error err.message if err logger.error err.message if err
@ -80,14 +78,14 @@ class FFTCGDB
logger.info 'OK clear' logger.info 'OK clear'
close: -> close: ->
logger.info 'shutting down' logger.debug 'shutting down'
new Promise (resolve, reject) => new Promise (resolve, reject) =>
@db.close (err) -> @db.close (err) ->
if err if err
logger.error "FAIL '#{err.message}'" logger.error "FAIL '#{err.message}'"
reject null reject null
else else
logger.warn "OK close '#{@filename}'" logger.info "OK closed"
resolve null resolve null
validate: (login, password) -> validate: (login, password) ->
@ -206,13 +204,14 @@ class FFTCGDB
INSERT INTO decks (user, json) INSERT INTO decks (user, json)
VALUES (?, ?) VALUES (?, ?)
''' '''
stmt.run [userID, JSON.stringify deckCards], (err) -> stmt.run [userID, (JSON.stringify deckCards)], (err) ->
stmt.finalize() stmt.finalize()
if err if err
logger.warn "addDeck: FAIL db '#{err.code}' for '#{userID}'" logger.warn "addDeck: FAIL db '#{err.code}' for '#{userID}'"
reject messages.db reject messages.db
else else
# eslint-disable-next-line @fellow/coffee/missing-fat-arrows
logger.debug "addDeck: OK '#{@lastID}'" logger.debug "addDeck: OK '#{@lastID}'"
resolve @lastID resolve @lastID
@ -225,9 +224,11 @@ class FFTCGDB
''' '''
stmt.run [(JSON.stringify deckCards), deckID, userID], (err) -> stmt.run [(JSON.stringify deckCards), deckID, userID], (err) ->
stmt.finalize() stmt.finalize()
isUnchanged =
if err if err
logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'" logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'"
reject messages.db reject messages.db
# eslint-disable-next-line
else if @changes == 0 else if @changes == 0
logger.warn "no changes for input (#{userID}, #{deckID}, #{JSON.stringify deckCards})!" logger.warn "no changes for input (#{userID}, #{deckID}, #{JSON.stringify deckCards})!"
reject messages.db reject messages.db

View file

@ -56,5 +56,10 @@ fastify.listen 3001, '0.0.0.0'
# Handle termination # Handle termination
process.on 'SIGUSR2', -> process.on 'SIGUSR2', ->
logger.info 'shutting down after SIGINT' (require './db').close()
.then ->
logger.info 'shutting down normally after SIGINT'
.catch ->
logger.info 'error shutting down after SIGINT'
.finally ->
process.exit() process.exit()

View file

@ -13,11 +13,11 @@ EXPIRY =
class FFTCGSESSION class FFTCGSESSION
constructor: -> constructor: ->
@db = redis.createClient @redis = redis.createClient
host: 'redis' host: 'redis'
port: 6379 port: 6379
@db.on 'error', (err) -> @redis.on 'error', (err) ->
logger.error err.message logger.error err.message
sessionKey: (digest) -> "session.#{digest}" sessionKey: (digest) -> "session.#{digest}"
@ -31,7 +31,7 @@ class FFTCGSESSION
digest = hmac.digest 'base64' digest = hmac.digest 'base64'
# push (hash, userid) into DB for the configured timespan # push (hash, userid) into DB for the configured timespan
@db.setex (@sessionKey digest), EXPIRY.login * 86400, userid, => @redis.setex (@sessionKey digest), EXPIRY.login * 86400, userid, =>
logger.info "OK '#{@sessionKey digest}' created" logger.info "OK '#{@sessionKey digest}' created"
# return cookie data # return cookie data
resolve resolve
@ -42,7 +42,7 @@ class FFTCGSESSION
destroy: (digest) -> destroy: (digest) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# delete hash immediately # delete hash immediately
@db.del (@sessionKey digest), (err, res) => @redis.del (@sessionKey digest), (err, res) =>
if res == 0 if res == 0
reject null reject null
else else
@ -52,12 +52,12 @@ class FFTCGSESSION
check: (digest) -> check: (digest) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# refresh expiry timer on digest # refresh expiry timer on digest
@db.expire (@sessionKey digest), EXPIRY.login * 86400, (err, res) => @redis.expire (@sessionKey digest), EXPIRY.login * 86400, (err, res) =>
if res == 0 if res == 0
reject null reject null
else else
@db.get (@sessionKey digest), (err, res) => @redis.get (@sessionKey digest), (err, res) =>
logger.debug "OK '#{@sessionKey digest}' resumed" logger.debug "OK '#{@sessionKey digest}' resumed"
resolve res resolve res
@ -69,20 +69,20 @@ class FFTCGSESSION
digest = hmac.digest 'base64' digest = hmac.digest 'base64'
# insert game key # insert game key
@db.hsetnx (@gameKey digest), 'owner', userid, (err, res) => @redis.hsetnx (@gameKey digest), 'owner', userid, (err, res) =>
if res == 0 if res == 0
@db.del (@gameKey digest) @redis.del (@gameKey digest)
reject null reject null
else else
@db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => @redis.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) =>
if res == 0 if res == 0
@db.del (@gameKey digest) @redis.del (@gameKey digest)
reject null reject null
else else
# add game to active set # add game to active set
@db.sadd (@gameKey 'active'), (@gameKey digest), (err, res) => @redis.sadd (@gameKey 'active'), (@gameKey digest), (err, res) =>
# return game ID # return game ID
logger.info "OK '#{@gameKey digest}' created" logger.info "OK '#{@gameKey digest}' created"
resolve digest resolve digest
@ -97,7 +97,7 @@ class FFTCGSESSION
return new Promise (resolve, reject) => return new Promise (resolve, reject) =>
# scan "active" gameKey # scan "active" gameKey
@db.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) -> @redis.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) ->
if err if err
reject null reject null
@ -121,7 +121,7 @@ class FFTCGSESSION
for key in Array.from set for key in Array.from set
activeGames.push new Promise (resolve) => activeGames.push new Promise (resolve) =>
@db.hget key, 'owner', (err, res) -> @redis.hget key, 'owner', (err, res) ->
if(err) if(err)
resolve null resolve null
@ -129,20 +129,19 @@ class FFTCGSESSION
Promise.all activeGames Promise.all activeGames
.then (activeGames) -> .then (activeGames) ->
console.log activeGames
resolve activeGames resolve activeGames
joinGame: (digest, userid) -> joinGame: (digest, userid) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# refresh expiry timer on digest # refresh expiry timer on digest
@db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => @redis.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) =>
if res == 0 if res == 0
reject null reject null
else else
# insert opponent value # insert opponent value
@db.hsetnx (@gameKey digest), 'opponent', userid, (err, res) => @redis.hsetnx (@gameKey digest), 'opponent', userid, (err, res) =>
if res == 0 if res == 0
reject null reject null
@ -154,13 +153,13 @@ class FFTCGSESSION
updateGame: (digest, state) -> updateGame: (digest, state) ->
new Promise (resolve, reject) => new Promise (resolve, reject) =>
# refresh expiry timer on digest # refresh expiry timer on digest
@db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => @redis.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) =>
if res == 0 if res == 0
reject null reject null
else else
# update state value # update state value
@db.hset (@gameKey digest), 'state', (JSON.stringify state), (err, res) => @redis.hset (@gameKey digest), 'state', (JSON.stringify state), (err, res) =>
if res == 0 if res == 0
reject null reject null