pwi-plata-type 0.4.166 → 0.4.167
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.
- package/README.md +2 -2
- package/__BUILD__/libs/sql.js +4 -1
- package/__BUILD__/libs/sql.js.map +1 -1
- package/package.json +53 -53
- package/src/@types/build.d.ts +26 -26
- package/src/@types/cluster.actions.d.ts +27 -27
- package/src/@types/cluster.d.ts +55 -55
- package/src/@types/create-cli.d.ts +8 -8
- package/src/@types/exported.d.ts +31 -31
- package/src/@types/fs.d.ts +6 -6
- package/src/@types/global.d.ts +113 -113
- package/src/@types/knex.d.ts +13 -13
- package/src/@types/model.d.ts +143 -143
- package/src/@types/router.d.ts +107 -107
- package/src/@types/sql.d.ts +75 -75
- package/src/@types/swagger.d.ts +22 -22
- package/src/bin/extras/create-api-cli.ts +218 -218
- package/src/bin/extras/create-cluster-cli.ts +160 -160
- package/src/bin/extras/create-lib-cli.ts +84 -84
- package/src/bin/extras/plata-create-tools.ts +97 -97
- package/src/bin/plata-api.ts +189 -189
- package/src/bin/plata-build.ts +17 -17
- package/src/bin/plata-cluster.ts +42 -42
- package/src/bin/plata-cmd.ts +18 -18
- package/src/bin/plata-create.ts +49 -49
- package/src/bin/plata-runtime.ts +131 -131
- package/src/bin/plata-swagger.ts +99 -99
- package/src/bin/runtime/_setupRuntime.ts +48 -48
- package/src/bin/runtime/_types.d.ts +7 -7
- package/src/bin/runtime/class.ts +20 -20
- package/src/bin/runtime/dirs.ts +25 -25
- package/src/bin/runtime/effect.ts +60 -60
- package/src/bin/runtime/fs.ts +10 -10
- package/src/bin/runtime/promises.ts +18 -18
- package/src/bin/runtime/require.ts +113 -113
- package/src/bin/runtime/sql.ts +12 -12
- package/src/bin/runtime/switch.ts +60 -60
- package/src/index.ts +19 -19
- package/src/libs/axios.ts +51 -51
- package/src/libs/build.ts +162 -162
- package/src/libs/cluster/cluster-controller.ts +93 -93
- package/src/libs/cluster.ts +330 -330
- package/src/libs/custom.ts +15 -15
- package/src/libs/env.ts +10 -10
- package/src/libs/fs.ts +399 -399
- package/src/libs/hot-reload.ts +63 -63
- package/src/libs/models/index.ts +4 -4
- package/src/libs/models/model-template-combine.ts +93 -93
- package/src/libs/models/model-template-env.ts +58 -58
- package/src/libs/models/model-template-union.ts +83 -83
- package/src/libs/models/model-template.ts +107 -107
- package/src/libs/models/model-validations.ts +443 -443
- package/src/libs/models/tools.ts +194 -194
- package/src/libs/router.ts +185 -185
- package/src/libs/sql.ts +792 -788
- package/src/libs/swagger.ts +81 -81
- package/src/libs/tools.ts +240 -240
- package/src/scripts/post-install/api.ts +32 -32
- package/src/scripts/post-install/cluster.ts +28 -28
- package/src/scripts/post-install/lib.ts +25 -25
- package/src/scripts/post-install.ts +64 -64
- package/src/scripts/pre-test.ts +33 -33
- package/src/tests/assets/sql/example.sql +76 -76
- package/src/tests/config.test.ts +12 -12
- package/src/tests/effect.test.ts +62 -62
- package/src/tests/model.env.test.ts +66 -66
- package/src/tests/model.test.ts +141 -141
- package/src/tests/model.union.test.ts +160 -160
- package/src/tests/router.test.ts +44 -44
- package/src/tests/sql.test.ts +502 -502
- package/src/tests/switch.test.ts +76 -76
- package/templates/create-cli/class.ts +13 -13
- package/templates/create-cli/cluster.ts +14 -14
- package/templates/create-cli/cmd.ts +3 -3
- package/templates/create-cli/config.ts +2 -2
- package/templates/create-cli/lib.ts +3 -3
- package/templates/create-cli/model.ts +14 -14
- package/templates/create-cli/route.ts +8 -8
- package/templates/create-cli/task.ts +13 -13
- package/templates/postinstall/api/.vscode/launch.json +20 -20
- package/templates/postinstall/api/Dockerfile +24 -24
- package/templates/postinstall/api/configs/env.ts +2 -2
- package/templates/postinstall/api/envs/debug.env +15 -15
- package/templates/postinstall/api/envs/homolog.env +15 -15
- package/templates/postinstall/api/envs/prod.env +15 -15
- package/templates/postinstall/api/tsconfig.json +24 -24
- package/templates/postinstall/cluster/.vscode/launch.json +20 -20
- package/templates/postinstall/cluster/Dockerfile +24 -24
- package/templates/postinstall/cluster/configs/env.ts +2 -2
- package/templates/postinstall/cluster/envs/debug.env +10 -10
- package/templates/postinstall/cluster/envs/homolog.env +10 -10
- package/templates/postinstall/cluster/envs/prod.env +10 -10
- package/templates/postinstall/cluster/tsconfig.json +24 -24
- package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
- package/templates/postinstall/lib/postinstall.js +33 -33
- package/templates/postinstall/lib/tsconfig.json +22 -22
- package/templates/swagger-cli/header.json +21 -21
- package/templates/swagger-cli/route/body.json +14 -14
- package/templates/swagger-cli/route/header.json +18 -18
- package/templates/swagger-cli/route/response.json +53 -53
- package/tsconfig.json +28 -28
package/src/libs/build.ts
CHANGED
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { glob } from "glob"
|
|
3
|
-
import moment from 'moment'
|
|
4
|
-
|
|
5
|
-
export namespace PlataBuild {
|
|
6
|
-
export const getConfigDefault: () => Build.Config = () => ({
|
|
7
|
-
env: 'prod',
|
|
8
|
-
copyFolderToRoot: [],
|
|
9
|
-
mapFiles: false,
|
|
10
|
-
otimizar: true,
|
|
11
|
-
plugins: [],
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
export async function getBuildConfig(): PlataPromise<Build.Config> {
|
|
15
|
-
const config = await Plata.RequireAsync<{ default: unknown }>("@@/config.build")
|
|
16
|
-
|
|
17
|
-
if (config.errorID !== undefined) {
|
|
18
|
-
return PlataBuild.getConfigDefault()
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const loadedConfig = await Plata.switchUnion((typeof config.exports.default) as 'function' | 'object', {
|
|
22
|
-
_default: async (v) => Plata.BuildPlataError({
|
|
23
|
-
errorID: 'PLGETBLCONF001',
|
|
24
|
-
msg: `Tipo de config não suportada ${v}`
|
|
25
|
-
}),
|
|
26
|
-
|
|
27
|
-
function: async () => (config.exports.default as () => unknown)(),
|
|
28
|
-
object: async () => config.exports.default as unknown
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
return loadedConfig
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getScriptsByPlataType() {
|
|
35
|
-
switch (Plata.ProjectJson['plata_type']) {
|
|
36
|
-
case 'api': {
|
|
37
|
-
return {
|
|
38
|
-
start: 'plata-runtime --skip-recompile + plata-api'
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
case 'cluster': {
|
|
43
|
-
return {
|
|
44
|
-
start: 'plata-runtime --skip-recompile + plata-cluster'
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
default: {
|
|
49
|
-
return {}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export async function compile(params: Build.CompilerConfigs) {
|
|
55
|
-
const tsupRequire = await Plata.RequireAsync<typeof import('tsup')>('tsup')
|
|
56
|
-
|
|
57
|
-
if (tsupRequire.errorID !== undefined) {
|
|
58
|
-
return tsupRequire
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const files = [
|
|
62
|
-
...glob.sync('*/**/*.+(ts|env|json)'),
|
|
63
|
-
...glob.sync('config.build.ts'),
|
|
64
|
-
].filter(f => {
|
|
65
|
-
if (f.startsWith('node_modules'))
|
|
66
|
-
return false
|
|
67
|
-
;
|
|
68
|
-
|
|
69
|
-
if (f.startsWith('__'))
|
|
70
|
-
return false
|
|
71
|
-
;
|
|
72
|
-
|
|
73
|
-
if (f.endsWith('.d.ts'))
|
|
74
|
-
return false
|
|
75
|
-
;
|
|
76
|
-
|
|
77
|
-
if (params.packageJsonSetup)
|
|
78
|
-
if (f === "config.build.ts")
|
|
79
|
-
return false
|
|
80
|
-
;
|
|
81
|
-
;
|
|
82
|
-
|
|
83
|
-
return true
|
|
84
|
-
})
|
|
85
|
-
;
|
|
86
|
-
|
|
87
|
-
const { exports: tsup } = tsupRequire
|
|
88
|
-
const outDir = path.join(params.basePath, '__BUILD__')
|
|
89
|
-
|
|
90
|
-
await tsup.build({
|
|
91
|
-
outDir,
|
|
92
|
-
tsconfig: path.join(Plata.ProjectDir, 'tsconfig.json'),
|
|
93
|
-
clean: true,
|
|
94
|
-
entry: files,
|
|
95
|
-
skipNodeModulesBundle: true,
|
|
96
|
-
bundle: false,
|
|
97
|
-
platform: 'node',
|
|
98
|
-
target: 'esnext',
|
|
99
|
-
minify: params.otimizar === true,
|
|
100
|
-
minifyIdentifiers: params.otimizar === true,
|
|
101
|
-
minifySyntax: params.otimizar === true,
|
|
102
|
-
minifyWhitespace: params.otimizar === true,
|
|
103
|
-
sourcemap: params.mapFiles === true,
|
|
104
|
-
silent: true,
|
|
105
|
-
loader: {
|
|
106
|
-
'.env': 'copy',
|
|
107
|
-
'.json': 'copy',
|
|
108
|
-
}
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
console.log('✅ Codigo Compilado')
|
|
112
|
-
|
|
113
|
-
const tools = new Plata.files.CliTools(Plata.ProjectDir, params.basePath)
|
|
114
|
-
|
|
115
|
-
if (params.copyFolderToRoot !== undefined) {
|
|
116
|
-
for (let index = 0; index < params.copyFolderToRoot.length; index++) {
|
|
117
|
-
const folder = params.copyFolderToRoot[index];
|
|
118
|
-
|
|
119
|
-
await tools.copyFolderToDest(folder)
|
|
120
|
-
console.log(`✅ Pasta ${folder} copiada`)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (params.plugins?.length) {
|
|
125
|
-
for (let i = 0; i < params.plugins.length; i++) {
|
|
126
|
-
const plugin = params.plugins[i]
|
|
127
|
-
|
|
128
|
-
if (plugin.posBuild !== undefined) {
|
|
129
|
-
console.log(`♻️ Rodando o plugin: ${plugin.name}`)
|
|
130
|
-
await plugin.posBuild(params, outDir)
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
console.log(`✅ Todos os plugins rodados`)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (params.packageJsonSetup === true) {
|
|
138
|
-
await tools.createFileToDest('package.json', JSON.stringify({
|
|
139
|
-
name: Plata.ProjectJson.name,
|
|
140
|
-
version: moment().format('YYYYMMDD'),
|
|
141
|
-
description: "",
|
|
142
|
-
scripts: {
|
|
143
|
-
...Plata.ProjectJson['my-scripts'],
|
|
144
|
-
...PlataBuild.getScriptsByPlataType(),
|
|
145
|
-
},
|
|
146
|
-
keywords: [],
|
|
147
|
-
author: "",
|
|
148
|
-
plata_skip_recompile: true,
|
|
149
|
-
plata_type: Plata.ProjectJson['plata_type'],
|
|
150
|
-
plata_name: Plata.ProjectJson['plata_name'],
|
|
151
|
-
plata_no_setup: true,
|
|
152
|
-
plata_build_env: params.env ?? 'prod',
|
|
153
|
-
dependencies: {
|
|
154
|
-
...Plata.ProjectJson.dependencies
|
|
155
|
-
}
|
|
156
|
-
}, null, 4))
|
|
157
|
-
|
|
158
|
-
console.log(`🚀 Compilação finalizada`)
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return true
|
|
162
|
-
}
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import { glob } from "glob"
|
|
3
|
+
import moment from 'moment'
|
|
4
|
+
|
|
5
|
+
export namespace PlataBuild {
|
|
6
|
+
export const getConfigDefault: () => Build.Config = () => ({
|
|
7
|
+
env: 'prod',
|
|
8
|
+
copyFolderToRoot: [],
|
|
9
|
+
mapFiles: false,
|
|
10
|
+
otimizar: true,
|
|
11
|
+
plugins: [],
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
export async function getBuildConfig(): PlataPromise<Build.Config> {
|
|
15
|
+
const config = await Plata.RequireAsync<{ default: unknown }>("@@/config.build")
|
|
16
|
+
|
|
17
|
+
if (config.errorID !== undefined) {
|
|
18
|
+
return PlataBuild.getConfigDefault()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const loadedConfig = await Plata.switchUnion((typeof config.exports.default) as 'function' | 'object', {
|
|
22
|
+
_default: async (v) => Plata.BuildPlataError({
|
|
23
|
+
errorID: 'PLGETBLCONF001',
|
|
24
|
+
msg: `Tipo de config não suportada ${v}`
|
|
25
|
+
}),
|
|
26
|
+
|
|
27
|
+
function: async () => (config.exports.default as () => unknown)(),
|
|
28
|
+
object: async () => config.exports.default as unknown
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return loadedConfig
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getScriptsByPlataType() {
|
|
35
|
+
switch (Plata.ProjectJson['plata_type']) {
|
|
36
|
+
case 'api': {
|
|
37
|
+
return {
|
|
38
|
+
start: 'plata-runtime --skip-recompile + plata-api'
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
case 'cluster': {
|
|
43
|
+
return {
|
|
44
|
+
start: 'plata-runtime --skip-recompile + plata-cluster'
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
default: {
|
|
49
|
+
return {}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function compile(params: Build.CompilerConfigs) {
|
|
55
|
+
const tsupRequire = await Plata.RequireAsync<typeof import('tsup')>('tsup')
|
|
56
|
+
|
|
57
|
+
if (tsupRequire.errorID !== undefined) {
|
|
58
|
+
return tsupRequire
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const files = [
|
|
62
|
+
...glob.sync('*/**/*.+(ts|env|json)'),
|
|
63
|
+
...glob.sync('config.build.ts'),
|
|
64
|
+
].filter(f => {
|
|
65
|
+
if (f.startsWith('node_modules'))
|
|
66
|
+
return false
|
|
67
|
+
;
|
|
68
|
+
|
|
69
|
+
if (f.startsWith('__'))
|
|
70
|
+
return false
|
|
71
|
+
;
|
|
72
|
+
|
|
73
|
+
if (f.endsWith('.d.ts'))
|
|
74
|
+
return false
|
|
75
|
+
;
|
|
76
|
+
|
|
77
|
+
if (params.packageJsonSetup)
|
|
78
|
+
if (f === "config.build.ts")
|
|
79
|
+
return false
|
|
80
|
+
;
|
|
81
|
+
;
|
|
82
|
+
|
|
83
|
+
return true
|
|
84
|
+
})
|
|
85
|
+
;
|
|
86
|
+
|
|
87
|
+
const { exports: tsup } = tsupRequire
|
|
88
|
+
const outDir = path.join(params.basePath, '__BUILD__')
|
|
89
|
+
|
|
90
|
+
await tsup.build({
|
|
91
|
+
outDir,
|
|
92
|
+
tsconfig: path.join(Plata.ProjectDir, 'tsconfig.json'),
|
|
93
|
+
clean: true,
|
|
94
|
+
entry: files,
|
|
95
|
+
skipNodeModulesBundle: true,
|
|
96
|
+
bundle: false,
|
|
97
|
+
platform: 'node',
|
|
98
|
+
target: 'esnext',
|
|
99
|
+
minify: params.otimizar === true,
|
|
100
|
+
minifyIdentifiers: params.otimizar === true,
|
|
101
|
+
minifySyntax: params.otimizar === true,
|
|
102
|
+
minifyWhitespace: params.otimizar === true,
|
|
103
|
+
sourcemap: params.mapFiles === true,
|
|
104
|
+
silent: true,
|
|
105
|
+
loader: {
|
|
106
|
+
'.env': 'copy',
|
|
107
|
+
'.json': 'copy',
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
console.log('✅ Codigo Compilado')
|
|
112
|
+
|
|
113
|
+
const tools = new Plata.files.CliTools(Plata.ProjectDir, params.basePath)
|
|
114
|
+
|
|
115
|
+
if (params.copyFolderToRoot !== undefined) {
|
|
116
|
+
for (let index = 0; index < params.copyFolderToRoot.length; index++) {
|
|
117
|
+
const folder = params.copyFolderToRoot[index];
|
|
118
|
+
|
|
119
|
+
await tools.copyFolderToDest(folder)
|
|
120
|
+
console.log(`✅ Pasta ${folder} copiada`)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (params.plugins?.length) {
|
|
125
|
+
for (let i = 0; i < params.plugins.length; i++) {
|
|
126
|
+
const plugin = params.plugins[i]
|
|
127
|
+
|
|
128
|
+
if (plugin.posBuild !== undefined) {
|
|
129
|
+
console.log(`♻️ Rodando o plugin: ${plugin.name}`)
|
|
130
|
+
await plugin.posBuild(params, outDir)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
console.log(`✅ Todos os plugins rodados`)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (params.packageJsonSetup === true) {
|
|
138
|
+
await tools.createFileToDest('package.json', JSON.stringify({
|
|
139
|
+
name: Plata.ProjectJson.name,
|
|
140
|
+
version: moment().format('YYYYMMDD'),
|
|
141
|
+
description: "",
|
|
142
|
+
scripts: {
|
|
143
|
+
...Plata.ProjectJson['my-scripts'],
|
|
144
|
+
...PlataBuild.getScriptsByPlataType(),
|
|
145
|
+
},
|
|
146
|
+
keywords: [],
|
|
147
|
+
author: "",
|
|
148
|
+
plata_skip_recompile: true,
|
|
149
|
+
plata_type: Plata.ProjectJson['plata_type'],
|
|
150
|
+
plata_name: Plata.ProjectJson['plata_name'],
|
|
151
|
+
plata_no_setup: true,
|
|
152
|
+
plata_build_env: params.env ?? 'prod',
|
|
153
|
+
dependencies: {
|
|
154
|
+
...Plata.ProjectJson.dependencies
|
|
155
|
+
}
|
|
156
|
+
}, null, 4))
|
|
157
|
+
|
|
158
|
+
console.log(`🚀 Compilação finalizada`)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return true
|
|
162
|
+
}
|
|
163
163
|
}
|
|
@@ -1,94 +1,94 @@
|
|
|
1
|
-
import { PlataClusterManager } from "../cluster"
|
|
2
|
-
|
|
3
|
-
export class PlataClusterController {
|
|
4
|
-
public readonly clusterManager: PlataClusterManager
|
|
5
|
-
public readonly clusterName: string
|
|
6
|
-
|
|
7
|
-
private msgId = 0
|
|
8
|
-
private workersPids: Set<number> = new Set()
|
|
9
|
-
|
|
10
|
-
constructor(clusterManager: PlataClusterManager, name: string) {
|
|
11
|
-
this.clusterManager = clusterManager
|
|
12
|
-
this.clusterName = name
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public async loadWorkers(): PlataPromise<true> {
|
|
16
|
-
const result: PlataResult<number[]> = await new Promise(resolve => {
|
|
17
|
-
const id = `${process.pid}_${this.msgId++}_GETPID`
|
|
18
|
-
|
|
19
|
-
const cleanUp = () => {
|
|
20
|
-
process.removeListener('message', onResponse)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const onResponse = (event: Cluster.Actions) => {
|
|
24
|
-
if (event.action !== `GET-PIDS-CLUSTER-RESULT-${id}`)
|
|
25
|
-
return
|
|
26
|
-
;
|
|
27
|
-
|
|
28
|
-
cleanUp()
|
|
29
|
-
|
|
30
|
-
const e: Extract<Cluster.Actions, { action: `GET-PIDS-CLUSTER-RESULT-${string}` }> = event as any
|
|
31
|
-
|
|
32
|
-
return resolve(e.msg)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
process.on('message', onResponse)
|
|
36
|
-
this.clusterManager.send({
|
|
37
|
-
action: 'GET-PIDS-CLUSTER',
|
|
38
|
-
msg: {
|
|
39
|
-
id,
|
|
40
|
-
name: this.clusterName
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
if (result.errorID !== undefined) {
|
|
46
|
-
return result
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const pids = new Set(result)
|
|
50
|
-
|
|
51
|
-
pids.delete(process.pid)
|
|
52
|
-
|
|
53
|
-
this.workersPids = pids
|
|
54
|
-
|
|
55
|
-
return true
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public getWorkersPids(): number[] {
|
|
59
|
-
return [...this.workersPids]
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public setOnDeadWorker(handle: (newPid: number, oldPid: number) => Promise<void>) {
|
|
63
|
-
const handleMessage = ((event: Cluster.Actions) => {
|
|
64
|
-
if (event.action !== 'DEAD-WORKER-NOTIFY') return
|
|
65
|
-
if (event.msg.clusterName !== this.clusterName) return
|
|
66
|
-
|
|
67
|
-
this.workersPids.delete(event.msg.pidDead)
|
|
68
|
-
this.workersPids.add(event.msg.newPid)
|
|
69
|
-
|
|
70
|
-
return handle(event.msg.newPid, event.msg.pidDead)
|
|
71
|
-
}).bind(this)
|
|
72
|
-
|
|
73
|
-
process.on('message', handleMessage)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public async sendMsgToPID(pid: number, event: Cluster.UserActions): PlataPromise<boolean> {
|
|
77
|
-
if (!this.workersPids.has(pid)) return {
|
|
78
|
-
errorID: 'PLCLUCONTSNDPID001',
|
|
79
|
-
msg: `PID ${pid} não existe no cluster`
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return this.clusterManager.sendToWorkerPid(pid, event)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public async setOnMessage(handle: (msg: Cluster.UserActions) => Promise<void>) {
|
|
86
|
-
const handleMessage = ((msg) => {
|
|
87
|
-
if (msg.action === undefined) return
|
|
88
|
-
|
|
89
|
-
return (handle as any)(msg)
|
|
90
|
-
}).bind(this)
|
|
91
|
-
|
|
92
|
-
process.on('message', handleMessage)
|
|
93
|
-
}
|
|
1
|
+
import { PlataClusterManager } from "../cluster"
|
|
2
|
+
|
|
3
|
+
export class PlataClusterController {
|
|
4
|
+
public readonly clusterManager: PlataClusterManager
|
|
5
|
+
public readonly clusterName: string
|
|
6
|
+
|
|
7
|
+
private msgId = 0
|
|
8
|
+
private workersPids: Set<number> = new Set()
|
|
9
|
+
|
|
10
|
+
constructor(clusterManager: PlataClusterManager, name: string) {
|
|
11
|
+
this.clusterManager = clusterManager
|
|
12
|
+
this.clusterName = name
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async loadWorkers(): PlataPromise<true> {
|
|
16
|
+
const result: PlataResult<number[]> = await new Promise(resolve => {
|
|
17
|
+
const id = `${process.pid}_${this.msgId++}_GETPID`
|
|
18
|
+
|
|
19
|
+
const cleanUp = () => {
|
|
20
|
+
process.removeListener('message', onResponse)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const onResponse = (event: Cluster.Actions) => {
|
|
24
|
+
if (event.action !== `GET-PIDS-CLUSTER-RESULT-${id}`)
|
|
25
|
+
return
|
|
26
|
+
;
|
|
27
|
+
|
|
28
|
+
cleanUp()
|
|
29
|
+
|
|
30
|
+
const e: Extract<Cluster.Actions, { action: `GET-PIDS-CLUSTER-RESULT-${string}` }> = event as any
|
|
31
|
+
|
|
32
|
+
return resolve(e.msg)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
process.on('message', onResponse)
|
|
36
|
+
this.clusterManager.send({
|
|
37
|
+
action: 'GET-PIDS-CLUSTER',
|
|
38
|
+
msg: {
|
|
39
|
+
id,
|
|
40
|
+
name: this.clusterName
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
if (result.errorID !== undefined) {
|
|
46
|
+
return result
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const pids = new Set(result)
|
|
50
|
+
|
|
51
|
+
pids.delete(process.pid)
|
|
52
|
+
|
|
53
|
+
this.workersPids = pids
|
|
54
|
+
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public getWorkersPids(): number[] {
|
|
59
|
+
return [...this.workersPids]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public setOnDeadWorker(handle: (newPid: number, oldPid: number) => Promise<void>) {
|
|
63
|
+
const handleMessage = ((event: Cluster.Actions) => {
|
|
64
|
+
if (event.action !== 'DEAD-WORKER-NOTIFY') return
|
|
65
|
+
if (event.msg.clusterName !== this.clusterName) return
|
|
66
|
+
|
|
67
|
+
this.workersPids.delete(event.msg.pidDead)
|
|
68
|
+
this.workersPids.add(event.msg.newPid)
|
|
69
|
+
|
|
70
|
+
return handle(event.msg.newPid, event.msg.pidDead)
|
|
71
|
+
}).bind(this)
|
|
72
|
+
|
|
73
|
+
process.on('message', handleMessage)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public async sendMsgToPID(pid: number, event: Cluster.UserActions): PlataPromise<boolean> {
|
|
77
|
+
if (!this.workersPids.has(pid)) return {
|
|
78
|
+
errorID: 'PLCLUCONTSNDPID001',
|
|
79
|
+
msg: `PID ${pid} não existe no cluster`
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return this.clusterManager.sendToWorkerPid(pid, event)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public async setOnMessage(handle: (msg: Cluster.UserActions) => Promise<void>) {
|
|
86
|
+
const handleMessage = ((msg) => {
|
|
87
|
+
if (msg.action === undefined) return
|
|
88
|
+
|
|
89
|
+
return (handle as any)(msg)
|
|
90
|
+
}).bind(this)
|
|
91
|
+
|
|
92
|
+
process.on('message', handleMessage)
|
|
93
|
+
}
|
|
94
94
|
}
|