From 18fd86600cd0b7bf6af6b31c07b157a2834c9328 Mon Sep 17 00:00:00 2001 From: jl vaquero Date: Mon, 27 Jan 2020 13:03:22 +0100 Subject: [PATCH] + Add graceful stop to sinergy with process managers (i.e. PM2) + Add npm start script --- memoryStore.js | 9 ++++++--- moonBot.js | 14 ++++++++++++-- moonGame.js | 4 ++++ package.json | 1 + 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/memoryStore.js b/memoryStore.js index a0b6cec..e5fc2d5 100644 --- a/memoryStore.js +++ b/memoryStore.js @@ -1,14 +1,17 @@ const MemoryStore = { games: new Map(), - set: async function (gameId, gameState) { + async set(gameId, gameState) { this.games.set(gameId, gameState); return Promise.resolve(gameState.id); }, - get: async function (gameId) { + async get(gameId) { return Promise.resolve(this.games.get(gameId)); }, - del: async function (gameId) { + async del(gameId) { return Promise.resolve(this.games.delete(gameId)); + }, + async quit() { + return Promise.resolve('OK'); } }; diff --git a/moonBot.js b/moonBot.js index fc35c22..32e34c1 100644 --- a/moonBot.js +++ b/moonBot.js @@ -8,7 +8,9 @@ const { concatMap, map } = require('rxjs/operators'); const { partition } = require('rxjs'); const keyBoards = require('./telegramKeyboard'); const { Rules, GameEventType } = require('./gameRules'); -const Store = require('./memoryStore'); +const Store = require('./memoryStore'); //memory store for testing and develop +//const Store = require('./redisStore'); //redis store recommended for production + const showStateEvent = Symbol.for("SHOW_GAME_STATE"); const token = process.env.MOON_BOT_TOKEN; @@ -368,4 +370,12 @@ Operation Target Cost All 2 register operations store the result in the first register. "or A B" will modify register A. -"mov A B" will copy register B value into register A.\`\`\``; \ No newline at end of file +"mov A B" will copy register B value into register A.\`\`\``; + +process.on('SIGINT', function () { + + eventStream.complete(); + eventStream.unsubscribe(); + Game.Quit(); + +}); \ No newline at end of file diff --git a/moonGame.js b/moonGame.js index 771d538..dc885eb 100644 --- a/moonGame.js +++ b/moonGame.js @@ -154,6 +154,10 @@ function Game(store) { gameState = StateManager.fixError({ gameState, playerId, error }).gameState; return gameState; + }, + + async Quit() { + return store.quit(); } }; diff --git a/package.json b/package.json index 168b91a..453fd17 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "tabletop moon game on telegram", "main": "moonBot.js", "scripts": { + "start": "node moonBot.js", "test": "mocha" }, "repository": {