underpost 2.8.641 → 2.8.651

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ import UnderpostDeploy from '../cli/deploy.js';
2
+ import UnderpostMonitor from '../cli/monitor.js';
3
+ import fs from 'fs-extra';
4
+ import { awaitDeployMonitor } from './conf.js';
5
+ import { actionInitLog, loggerFactory } from './logger.js';
6
+ import { shellCd, shellExec } from './process.js';
7
+ import UnderpostRootEnv from '../cli/env.js';
8
+
9
+ const logger = loggerFactory(import.meta);
10
+
11
+ class UnderpostStartUp {
12
+ static API = {
13
+ logRuntimeRouter: () => {
14
+ const displayLog = {};
15
+
16
+ for (const host of Object.keys(UnderpostDeploy.NETWORK))
17
+ for (const path of Object.keys(UnderpostDeploy.NETWORK[host]))
18
+ displayLog[UnderpostDeploy.NETWORK[host][path].publicHost] = UnderpostDeploy.NETWORK[host][path].local;
19
+
20
+ logger.info('Runtime network', displayLog);
21
+ },
22
+ listenServerFactory: (logic = async () => {}) => {
23
+ return {
24
+ listen: async (...args) => {
25
+ const msDelta = 1000;
26
+ const msMax = 30 * 24 * 60 * 60 * 1000; // ~ 1 month
27
+ let msCount = 0;
28
+ setInterval(() => {
29
+ msCount += msDelta;
30
+ if (msCount >= msMax) {
31
+ const message = 'Listen server factory timeout';
32
+ logger.error(message);
33
+ throw new Error(message);
34
+ }
35
+ }, msDelta);
36
+ return logic ? await logic(...args) : undefined, args[1]();
37
+ },
38
+ };
39
+ },
40
+ listenPortController: async (server, port, metadata) =>
41
+ new Promise((resolve) => {
42
+ try {
43
+ if (port === ':') {
44
+ server.listen(port, actionInitLog);
45
+ return resolve(true);
46
+ }
47
+
48
+ const { host, path, client, runtime, meta } = metadata;
49
+ const error = [];
50
+ if (port === undefined) error.push(`port`);
51
+ if (host === undefined) error.push(`host`);
52
+ if (path === undefined) error.push(`path`);
53
+ if (client === undefined) error.push(`client`);
54
+ if (runtime === undefined) error.push(`runtime`);
55
+ if (meta === undefined) error.push(`meta`);
56
+ if (error.length > 0) throw new Error('Listen port controller requires values: ' + error.join(', '));
57
+
58
+ server.listen(port, () => {
59
+ if (!UnderpostDeploy.NETWORK[host]) UnderpostDeploy.NETWORK[host] = {};
60
+ UnderpostDeploy.NETWORK[host][path] = {
61
+ meta,
62
+ client,
63
+ runtime,
64
+ port,
65
+ publicHost:
66
+ port === 80
67
+ ? `http://${host}${path}`
68
+ : port === 443
69
+ ? `https://${host}${path}`
70
+ : `http://${host}:${port}${path}`,
71
+ local: `http://localhost:${port}${path}`,
72
+ apis: metadata.apis,
73
+ };
74
+
75
+ return resolve(true);
76
+ });
77
+ } catch (error) {
78
+ logger.error(error, { metadata, port, stack: error.stack });
79
+ resolve(false);
80
+ }
81
+ }),
82
+
83
+ async callback(deployId = 'default', env = 'development', options = { build: false, run: false }) {
84
+ if (options.build === true) await UnderpostStartUp.API.build(deployId, env);
85
+ if (options.run === true) await UnderpostStartUp.API.run(deployId, env);
86
+ },
87
+ async build(deployId = 'default', env = 'development') {
88
+ const buildBasePath = `/home/dd`;
89
+ const repoName = `engine-${deployId.split('-')[1]}`;
90
+ shellExec(`cd ${buildBasePath} && underpost clone underpostnet/${repoName}`);
91
+ shellExec(`cd ${buildBasePath} && sudo mv ./${repoName} ./engine`);
92
+ shellExec(`cd ${buildBasePath}/engine && underpost clone underpostnet/${repoName}-private`);
93
+ shellExec(`cd ${buildBasePath}/engine && sudo mv ./${repoName}-private ./engine-private`);
94
+ shellCd(`${buildBasePath}/engine`);
95
+ shellExec(`npm install`);
96
+ shellExec(`node bin/deploy conf ${deployId} ${env}`);
97
+ if (fs.existsSync('./engine-private/itc-scripts')) {
98
+ const itcScripts = await fs.readdir('./engine-private/itc-scripts');
99
+ for (const itcScript of itcScripts)
100
+ if (itcScript.match(deployId)) shellExec(`node ./engine-private/itc-scripts/${itcScript}`);
101
+ }
102
+ shellExec(`node bin/deploy build-full-client ${deployId}`);
103
+ },
104
+ async run(deployId = 'default', env = 'development') {
105
+ const runCmd = env === 'production' ? 'run prod-img' : 'run dev-img';
106
+ if (fs.existsSync(`./engine-private/replica`)) {
107
+ const replicas = await fs.readdir(`./engine-private/replica`);
108
+ for (const replica of replicas) {
109
+ if (!replica.match(deployId)) continue;
110
+ shellExec(`node bin/deploy conf ${replica} ${env}`);
111
+ shellExec(`npm ${runCmd} deploy deploy-id:${replica}`, { async: true });
112
+ await awaitDeployMonitor(true);
113
+ }
114
+ }
115
+ shellExec(`node bin/deploy conf ${deployId} ${env}`);
116
+ shellExec(`npm ${runCmd} deploy deploy-id:${deployId}`, { async: true });
117
+ await awaitDeployMonitor(true);
118
+ UnderpostRootEnv.API.set('container-status', `${deployId}-${env}-running-deployment`);
119
+ },
120
+ };
121
+ }
122
+
123
+ export default UnderpostStartUp;
@@ -5,12 +5,24 @@ import { loggerFactory } from './logger.js';
5
5
 
6
6
  const logger = loggerFactory(import.meta);
7
7
 
8
+ const ValkeyInstances = {};
9
+
8
10
  let valkeyEnabled = true;
9
11
 
10
12
  const disableValkeyErrorMessage = 'valkey is not enabled';
11
13
 
12
14
  const isValkeyEnable = () => valkeyEnabled;
13
15
 
16
+ const createValkeyConnection = async (
17
+ instance = { host: '', port: 0 },
18
+ valkeyServerConnectionOptions = { host: '', port: 0 },
19
+ ) => {
20
+ ValkeyInstances[`${instance.host}${instance.path}`] = await ValkeyAPI.valkeyClientFactory(
21
+ valkeyServerConnectionOptions,
22
+ );
23
+ return ValkeyInstances[`${instance.host}${instance.path}`];
24
+ };
25
+
14
26
  const selectDtoFactory = (payload, select) => {
15
27
  const result = {};
16
28
  for (const key of Object.keys(select)) {
@@ -19,10 +31,12 @@ const selectDtoFactory = (payload, select) => {
19
31
  return result;
20
32
  };
21
33
 
22
- const valkeyClientFactory = async () => {
34
+ const valkeyClientFactory = async (options) => {
23
35
  const valkey = new Valkey({
24
36
  // port: 6379,
25
37
  // host: 'service-valkey.default.svc.cluster.local',
38
+ port: options?.port ? options.port : undefined,
39
+ host: options?.port ? options.host : undefined,
26
40
  retryStrategy: (attempt) => {
27
41
  if (attempt === 1) {
28
42
  valkey.disconnect();
@@ -46,12 +60,12 @@ const valkeyClientFactory = async () => {
46
60
  return valkey;
47
61
  };
48
62
 
49
- const getValkeyObject = async (key = '') => {
63
+ const getValkeyObject = async (options = { host: '', port: 0 }, key = '') => {
50
64
  if (!valkeyEnabled) {
51
65
  logger.warn(disableValkeyErrorMessage + ' get', key);
52
66
  return null;
53
67
  }
54
- const object = await valkey.get(key);
68
+ const object = await ValkeyInstances[`${options.host}${options.path}`].get(key);
55
69
  try {
56
70
  return JSON.parse(object);
57
71
  } catch (error) {
@@ -60,19 +74,19 @@ const getValkeyObject = async (key = '') => {
60
74
  }
61
75
  };
62
76
 
63
- const setValkeyObject = async (key = '', payload = {}) => {
77
+ const setValkeyObject = async (options = { host: '', port: 0 }, key = '', payload = {}) => {
64
78
  if (!valkeyEnabled) throw new Error(disableValkeyErrorMessage);
65
- return await valkey.set(key, JSON.stringify(payload));
79
+ return await ValkeyInstances[`${options.host}${options.path}`].set(key, JSON.stringify(payload));
66
80
  };
67
81
 
68
- const updateValkeyObject = async (key = '', payload = {}) => {
82
+ const updateValkeyObject = async (options = { host: '', port: 0 }, key = '', payload = {}) => {
69
83
  if (!valkeyEnabled) throw new Error(disableValkeyErrorMessage);
70
- const object = await getValkeyObject(key, valkey);
84
+ const object = await getValkeyObject(key);
71
85
  object.updatedAt = new Date().toISOString();
72
- return await valkey.set(key, JSON.stringify({ ...object, ...payload }));
86
+ return await ValkeyInstances[`${options.host}${options.path}`].set(key, JSON.stringify({ ...object, ...payload }));
73
87
  };
74
88
 
75
- const valkeyObjectFactory = async (module = '', options = { host: 'localhost', object: {} }) => {
89
+ const valkeyObjectFactory = async (options = { host: 'localhost', object: {} }, module = '') => {
76
90
  if (!valkeyEnabled) throw new Error(disableValkeyErrorMessage);
77
91
  const idoDate = new Date().toISOString();
78
92
  options.object = options.object || {};
@@ -112,10 +126,9 @@ const ValkeyAPI = {
112
126
  setValkeyObject,
113
127
  valkeyObjectFactory,
114
128
  updateValkeyObject,
129
+ createValkeyConnection,
115
130
  };
116
131
 
117
- const valkey = await ValkeyAPI.valkeyClientFactory();
118
-
119
132
  export {
120
133
  valkeyClientFactory,
121
134
  selectDtoFactory,
@@ -124,5 +137,6 @@ export {
124
137
  valkeyObjectFactory,
125
138
  updateValkeyObject,
126
139
  isValkeyEnable,
140
+ createValkeyConnection,
127
141
  ValkeyAPI,
128
142
  };