From 6d67fbde84cbfe1495d7a35a5ee43499597eac3b Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 22 Sep 2020 04:07:47 +0700 Subject: [PATCH] NodeJS cluster fix: Only save disk cache to disk when master. fixes #108 --- backend/src/api/disk-cache.ts | 19 +++++++++++-------- backend/src/index.ts | 1 - 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index 30683f277..1e35fdf68 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +import * as cluster from 'cluster'; import memPool from './mempool'; import blocks from './blocks'; @@ -6,15 +7,17 @@ class DiskCache { static FILE_NAME = './cache.json'; constructor() { - process.on('SIGINT', () => { - this.saveCacheToDisk(); - process.exit(2); - }); + if (cluster.isMaster) { + process.on('SIGINT', () => { + this.saveCacheToDisk(); + process.exit(2); + }); - process.on('SIGTERM', () => { - this.saveCacheToDisk(); - process.exit(2); - }); + process.on('SIGTERM', () => { + this.saveCacheToDisk(); + process.exit(2); + }); + } } saveCacheToDisk() { diff --git a/backend/src/index.ts b/backend/src/index.ts index 8a47ba797..e8123e95e 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -7,7 +7,6 @@ import * as http from 'http'; import * as https from 'https'; import * as WebSocket from 'ws'; import * as cluster from 'cluster'; -import * as os from 'os'; import { checkDbConnection } from './database'; import routes from './routes';