Skip to content

Commit

Permalink
061 - PM2 프로세스간 통신
Browse files Browse the repository at this point in the history
  • Loading branch information
moosin76 committed Dec 8, 2021
1 parent d2f9277 commit cb57411
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 16 deletions.
3 changes: 1 addition & 2 deletions ecosystem.config.js
Expand Up @@ -3,8 +3,7 @@ module.exports = {
{
name : 'ezhome',
script : './server/server.js',
instances : 0,
scale : 2,
instances : 2,
exec_mode : 'cluster',
wait_ready : true,
listen_timeout : 50000,
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
"passport-kakao": "^1.0.1",
"passport-local": "^1.0.0",
"passport-naver": "^1.0.6",
"pm2": "^5.1.2",
"qs": "^6.10.1",
"rand-token": "^1.0.1",
"roboto-fontface": "*",
Expand Down
34 changes: 23 additions & 11 deletions server/api/_model/configModel.js
Expand Up @@ -8,27 +8,39 @@ const configModel = {
const sql = sqlHelper.SelectSimple(TABLE.CONFIG, null, ['cf_key', 'cf_val', 'cf_client', 'cf_type']);
const [rows] = await db.execute(sql.query, sql.values);
global.$config = {
server : {},
client : {}
server: {},
client: {}
};
for(const row of rows) {
this.setConfigItem(row);
for (const row of rows) {
this.setConfigItem(row, true);
}
},
setConfigItem(item) {
configModel.clearConfigItem(item.cf_key);
if(item.cf_type == 'Json') {
setConfigItem(item, isLoading = false) {
configModel.clearConfigItem(item.cf_key, isLoading);
if (item.cf_type == 'Json') {
item.cf_val = JSON.parse(item.cf_val);
}
if(item.cf_client) {
$config.client[item.cf_key] = item.cf_val;
if (item.cf_client) {
$config.client[item.cf_key] = item.cf_val;
} else {
$config.server[item.cf_key] = item.cf_val;
}
if (!isLoading) {
process.send({
type: "config:update",
data: item,
})
}
},
clearConfigItem(cf_key) {
clearConfigItem(cf_key, isLoading = false) {
delete $config.server[cf_key];
delete $config.client[cf_key];
if (!isLoading) {
process.send({
type: "config:remove",
data: cf_key,
})
}
},
async duplicateCheck({ field, value }) {
const sql = sqlHelper.SelectSimple(
Expand Down Expand Up @@ -57,7 +69,7 @@ const configModel = {
} else {
return $config.client;
};

},
async saveConfig(req) {
const data = req.body;
Expand Down
18 changes: 18 additions & 0 deletions server/plugins/pm2Bus.js
@@ -0,0 +1,18 @@
const pm2 = require('pm2');

pm2.launchBus(function (err, pm2_bus) {
// pm2_bus.on('process:msg', function(packet){
// console.log(packet);
// })
pm2_bus.on('config:update', function ({ data }) {
if (data.cf_client) {
$config.client[data.cf_key] = data.cf_val;
} else {
$config.server[data.cf_key] = data.cf_val;
}
});
pm2_bus.on('config:remove', function ({ data }) {
delete $config.client[data];
delete $config.server[data];
});
})
9 changes: 7 additions & 2 deletions server/server.js
Expand Up @@ -3,6 +3,7 @@ const express = require('express');
const http = require('http');
const path = require('path');
const fs = require('fs');
require('./plugins/pm2Bus');

(async function () {
// 앱 초기화
Expand All @@ -12,7 +13,11 @@ const fs = require('fs');

const configModel = require('./api/_model/configModel');
await configModel.load();


setInterval(() => {
console.log($config.client.test1, $config.server.test1)
}, 5000);

let isDiableKeepAlive = false;
app.use((req, res, next) => {
if (isDiableKeepAlive) {
Expand Down Expand Up @@ -82,7 +87,7 @@ const fs = require('fs');
metas: `<!-- inject more metas -->`,
token: req.cookies.token || null,
member: req.user || null,
config : $config.client,
config: $config.client,
};

const stream = renderer.renderToStream(ctx);
Expand Down
1 change: 0 additions & 1 deletion src/entry-server.js
Expand Up @@ -3,7 +3,6 @@ import { createApp } from "./main";
export default (ctx) => {
return new Promise(async (resolve, reject) => {
const { app, router, store } = createApp(ctx);
console.log(ctx);
await store.dispatch('appInit', ctx);

router.push(ctx.url);
Expand Down

0 comments on commit cb57411

Please sign in to comment.