diff --git a/backend/db.coffee b/backend/db.coffee index 9e8cf76..53d0259 100644 --- a/backend/db.coffee +++ b/backend/db.coffee @@ -17,14 +17,12 @@ messages = class FFTCGDB constructor: (filename, truncate) -> - @filename = filename - - @db = new sqlite3.Database @filename, (err) => + @db = new sqlite3.Database filename, (err) => if err logger.error err.message else - logger.info "OK open '#{@filename}'" + logger.info "OK opened '#{filename}'" @db.run 'PRAGMA foreign_keys = ON;', (err) => logger.error err.message if err @@ -80,14 +78,14 @@ class FFTCGDB logger.info 'OK clear' close: -> - logger.info 'shutting down' + logger.debug 'shutting down' new Promise (resolve, reject) => @db.close (err) -> if err logger.error "FAIL '#{err.message}'" reject null else - logger.warn "OK close '#{@filename}'" + logger.info "OK closed" resolve null validate: (login, password) -> @@ -206,13 +204,14 @@ class FFTCGDB INSERT INTO decks (user, json) VALUES (?, ?) ''' - stmt.run [userID, JSON.stringify deckCards], (err) -> + stmt.run [userID, (JSON.stringify deckCards)], (err) -> stmt.finalize() if err logger.warn "addDeck: FAIL db '#{err.code}' for '#{userID}'" reject messages.db else + # eslint-disable-next-line @fellow/coffee/missing-fat-arrows logger.debug "addDeck: OK '#{@lastID}'" resolve @lastID @@ -225,9 +224,11 @@ class FFTCGDB ''' stmt.run [(JSON.stringify deckCards), deckID, userID], (err) -> stmt.finalize() + isUnchanged = if err logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'" reject messages.db + # eslint-disable-next-line else if @changes == 0 logger.warn "no changes for input (#{userID}, #{deckID}, #{JSON.stringify deckCards})!" reject messages.db diff --git a/backend/server.coffee b/backend/server.coffee index e2f10d2..e330ed6 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -56,5 +56,10 @@ fastify.listen 3001, '0.0.0.0' # Handle termination process.on 'SIGUSR2', -> - logger.info 'shutting down after SIGINT' - process.exit() + (require './db').close() + .then -> + logger.info 'shutting down normally after SIGINT' + .catch -> + logger.info 'error shutting down after SIGINT' + .finally -> + process.exit() diff --git a/backend/session.coffee b/backend/session.coffee index 9a70a5c..d77bec3 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -13,11 +13,11 @@ EXPIRY = class FFTCGSESSION constructor: -> - @db = redis.createClient + @redis = redis.createClient host: 'redis' port: 6379 - @db.on 'error', (err) -> + @redis.on 'error', (err) -> logger.error err.message sessionKey: (digest) -> "session.#{digest}" @@ -31,7 +31,7 @@ class FFTCGSESSION digest = hmac.digest 'base64' # 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" # return cookie data resolve @@ -42,7 +42,7 @@ class FFTCGSESSION destroy: (digest) -> new Promise (resolve, reject) => # delete hash immediately - @db.del (@sessionKey digest), (err, res) => + @redis.del (@sessionKey digest), (err, res) => if res == 0 reject null else @@ -52,12 +52,12 @@ class FFTCGSESSION check: (digest) -> new Promise (resolve, reject) => # 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 reject null else - @db.get (@sessionKey digest), (err, res) => + @redis.get (@sessionKey digest), (err, res) => logger.debug "OK '#{@sessionKey digest}' resumed" resolve res @@ -69,20 +69,20 @@ class FFTCGSESSION digest = hmac.digest 'base64' # insert game key - @db.hsetnx (@gameKey digest), 'owner', userid, (err, res) => + @redis.hsetnx (@gameKey digest), 'owner', userid, (err, res) => if res == 0 - @db.del (@gameKey digest) + @redis.del (@gameKey digest) reject null else - @db.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => + @redis.expire (@gameKey digest), EXPIRY.game * 86400, (err, res) => if res == 0 - @db.del (@gameKey digest) + @redis.del (@gameKey digest) reject null else # add game to active set - @db.sadd (@gameKey 'active'), (@gameKey digest), (err, res) => + @redis.sadd (@gameKey 'active'), (@gameKey digest), (err, res) => # return game ID logger.info "OK '#{@gameKey digest}' created" resolve digest @@ -97,7 +97,7 @@ class FFTCGSESSION return new Promise (resolve, reject) => # scan "active" gameKey - @db.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) -> + @redis.sscan (@gameKey 'active'), cursor, 'COUNT', '100', (err, res) -> if err reject null @@ -121,7 +121,7 @@ class FFTCGSESSION for key in Array.from set activeGames.push new Promise (resolve) => - @db.hget key, 'owner', (err, res) -> + @redis.hget key, 'owner', (err, res) -> if(err) resolve null @@ -129,20 +129,19 @@ class FFTCGSESSION Promise.all activeGames .then (activeGames) -> - console.log activeGames resolve activeGames joinGame: (digest, userid) -> new Promise (resolve, reject) => # 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 reject null else # insert opponent value - @db.hsetnx (@gameKey digest), 'opponent', userid, (err, res) => + @redis.hsetnx (@gameKey digest), 'opponent', userid, (err, res) => if res == 0 reject null @@ -154,13 +153,13 @@ class FFTCGSESSION updateGame: (digest, state) -> new Promise (resolve, reject) => # 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 reject null else # 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 reject null