pwi-plata-type 0.3.17 → 0.4.0
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/__BUILD__/bin/extras/create-api-cli.js +96 -0
- package/__BUILD__/bin/extras/create-api-cli.js.map +1 -0
- package/__BUILD__/bin/extras/create-lib-cli.js +62 -0
- package/__BUILD__/bin/extras/create-lib-cli.js.map +1 -0
- package/__BUILD__/bin/extras/plata-create-tools.js +94 -0
- package/__BUILD__/bin/extras/plata-create-tools.js.map +1 -0
- package/__BUILD__/bin/plata-api.js +138 -0
- package/__BUILD__/bin/plata-api.js.map +1 -0
- package/__BUILD__/bin/plata-create.js +24 -0
- package/__BUILD__/bin/plata-create.js.map +1 -0
- package/__BUILD__/bin/plata-runtime.js +156 -0
- package/__BUILD__/bin/plata-runtime.js.map +1 -0
- package/__BUILD__/bin/plata-swagger.js +82 -0
- package/__BUILD__/bin/plata-swagger.js.map +1 -0
- package/__BUILD__/bin/runtime/_setupRuntime.js +43 -0
- package/__BUILD__/bin/runtime/_setupRuntime.js.map +1 -0
- package/__BUILD__/bin/runtime/class.js +18 -0
- package/__BUILD__/bin/runtime/class.js.map +1 -0
- package/__BUILD__/bin/runtime/dirs.js +16 -0
- package/__BUILD__/bin/runtime/dirs.js.map +1 -0
- package/__BUILD__/bin/runtime/promises.js +15 -0
- package/__BUILD__/bin/runtime/promises.js.map +1 -0
- package/__BUILD__/bin/runtime/require.js +88 -0
- package/__BUILD__/bin/runtime/require.js.map +1 -0
- package/__BUILD__/index.js +42 -0
- package/__BUILD__/index.js.map +1 -0
- package/__BUILD__/libs/axios.js +42 -0
- package/__BUILD__/libs/axios.js.map +1 -0
- package/__BUILD__/libs/cluster.js +160 -0
- package/__BUILD__/libs/cluster.js.map +1 -0
- package/__BUILD__/libs/fs.js +206 -0
- package/__BUILD__/libs/fs.js.map +1 -0
- package/__BUILD__/libs/model-validations.js +356 -0
- package/__BUILD__/libs/model-validations.js.map +1 -0
- package/__BUILD__/libs/model.js +185 -0
- package/__BUILD__/libs/model.js.map +1 -0
- package/__BUILD__/libs/router.js +127 -0
- package/__BUILD__/libs/router.js.map +1 -0
- package/__BUILD__/libs/swagger.js +64 -0
- package/__BUILD__/libs/swagger.js.map +1 -0
- package/__BUILD__/libs/tasks.js +134 -0
- package/__BUILD__/libs/tasks.js.map +1 -0
- package/__BUILD__/libs/tools.js +231 -0
- package/__BUILD__/libs/tools.js.map +1 -0
- package/__BUILD__/scripts/post-install/api.js +25 -0
- package/__BUILD__/scripts/post-install/api.js.map +1 -0
- package/__BUILD__/scripts/post-install/lib.js +25 -0
- package/__BUILD__/scripts/post-install/lib.js.map +1 -0
- package/__BUILD__/scripts/post-install.js +65 -0
- package/__BUILD__/scripts/post-install.js.map +1 -0
- package/__BUILD__/scripts/pre-test.js +25 -0
- package/__BUILD__/scripts/pre-test.js.map +1 -0
- package/package.json +18 -16
- package/src/@types/cluster.actions.d.ts +7 -0
- package/src/@types/cluster.d.ts +33 -0
- package/src/@types/create-cli.d.ts +9 -0
- package/src/@types/exported.d.ts +24 -0
- package/src/@types/global.d.ts +69 -0
- package/src/@types/model.d.ts +69 -0
- package/src/@types/router.d.ts +75 -0
- package/src/@types/swagger.d.ts +23 -0
- package/{bin/extras/plata-create-api.ts → src/bin/extras/create-api-cli.ts} +5 -6
- package/{bin/extras/plata-create-lib.ts → src/bin/extras/create-lib-cli.ts} +3 -4
- package/{bin → src/bin}/extras/plata-create-tools.ts +4 -4
- package/src/bin/plata-api.ts +172 -0
- package/src/bin/plata-create.ts +21 -0
- package/src/bin/plata-runtime.ts +188 -0
- package/src/bin/plata-swagger.ts +100 -0
- package/src/bin/runtime/_setupRuntime.ts +49 -0
- package/src/bin/runtime/_types.d.ts +4 -0
- package/src/bin/runtime/class.ts +21 -0
- package/src/bin/runtime/dirs.ts +26 -0
- package/src/bin/runtime/promises.ts +15 -0
- package/src/bin/runtime/require.ts +104 -0
- package/{index.ts → src/index.ts} +2 -4
- package/{libs → src/libs}/axios.ts +0 -0
- package/src/libs/cluster.ts +189 -0
- package/src/libs/fs.ts +382 -0
- package/{libs/model.ts → src/libs/model-validations.ts} +28 -262
- package/src/libs/model.ts +233 -0
- package/src/libs/router.ts +147 -0
- package/src/libs/swagger.ts +81 -0
- package/{libs → src/libs}/tasks.ts +0 -0
- package/{libs → src/libs}/tools.ts +3 -18
- package/src/scripts/post-install/api.ts +32 -0
- package/src/scripts/post-install/lib.ts +30 -0
- package/src/scripts/post-install.ts +65 -0
- package/src/scripts/pre-test.ts +34 -0
- package/src/tests/assets/config/test.txt +1 -0
- package/src/tests/config.test.ts +13 -0
- package/src/tests/model.test.ts +115 -0
- package/templates/create-cli/cluster.ts +7 -9
- package/templates/create-cli/config.ts +2 -8
- package/templates/create-cli/lib.ts +7 -17
- package/templates/create-cli/model.ts +10 -21
- package/templates/create-cli/route.ts +1 -3
- package/templates/postinstall/api/configs/env.ts +2 -8
- package/templates/postinstall/api/tsconfig.json +20 -37
- package/templates/postinstall/lib/postinstall-tools.mjs +1 -1
- package/templates/postinstall/lib/postinstall.js +1 -0
- package/templates/postinstall/lib/tsconfig.json +21 -38
- package/tsconfig.json +25 -37
- package/Dockerfile +0 -13
- package/bin/extras/plata-build-tool.ts +0 -151
- package/bin/plata-build.ts +0 -144
- package/bin/plata-create.ts +0 -28
- package/bin/plata-reload.ts +0 -13
- package/bin/plata-swagger-gen.ts +0 -183
- package/bin/plata.ts +0 -122
- package/libs/cluster.ts +0 -219
- package/libs/env.ts +0 -28
- package/libs/routes.ts +0 -275
- package/libs/swagger.ts +0 -79
- package/postinstall-tools.mjs +0 -115
- package/postinstall.mjs +0 -42
- package/templates/postinstall/api/_install.mjs +0 -34
- package/templates/postinstall/lib/_install.mjs +0 -30
package/bin/plata-swagger-gen.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import { buildExpressRouter } from '../libs/routes'
|
|
4
|
-
import { PlataResultado, PlataDirs, PlataFiles, PlataError } from '../libs/tools'
|
|
5
|
-
|
|
6
|
-
interface dumpParameter {
|
|
7
|
-
name: string,
|
|
8
|
-
required: boolean,
|
|
9
|
-
in: 'path',
|
|
10
|
-
type: 'string'
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
interface dumpRoute {
|
|
14
|
-
httpRoute: string,
|
|
15
|
-
metodo: string,
|
|
16
|
-
params: dumpParameter[]
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
interface swaggerFiles {
|
|
20
|
-
swaggerHeader: any,
|
|
21
|
-
rota: {
|
|
22
|
-
header: any,
|
|
23
|
-
body: any,
|
|
24
|
-
response: any,
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async function toProjectSwagger(file: string, content: any): Promise<PlataError | null> {
|
|
29
|
-
try {
|
|
30
|
-
const p = path.resolve(file)
|
|
31
|
-
|
|
32
|
-
const r = await PlataFiles.createFileIfNotExists(p, JSON.stringify(content, null, 4))
|
|
33
|
-
|
|
34
|
-
return r.errorID !== undefined ? r : null
|
|
35
|
-
|
|
36
|
-
} catch (e) {
|
|
37
|
-
return {
|
|
38
|
-
errorID: '',
|
|
39
|
-
msg: `Erro ao salvar o arquivo ${file}`,
|
|
40
|
-
error: e
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function fromTemplateJson(file: string): PlataResultado<any> {
|
|
46
|
-
try {
|
|
47
|
-
const p = path.resolve(file)
|
|
48
|
-
|
|
49
|
-
return JSON.parse(fs.readFileSync(p).toString())
|
|
50
|
-
} catch (e) {
|
|
51
|
-
return {
|
|
52
|
-
errorID: 'PBSW0001',
|
|
53
|
-
msg: `Erro ao ler o templete da plata ${file}`,
|
|
54
|
-
error: e
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function loadSwaggerFiles(): swaggerFiles {
|
|
60
|
-
const f = {
|
|
61
|
-
rota: {}
|
|
62
|
-
}
|
|
63
|
-
const files = f as swaggerFiles
|
|
64
|
-
|
|
65
|
-
const header = fromTemplateJson(
|
|
66
|
-
PlataDirs.getPlataTempleteDir('swagger-cli', 'header.json')
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
if (header.errorID !== undefined) {
|
|
70
|
-
throw header
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
files.swaggerHeader = header
|
|
74
|
-
|
|
75
|
-
const rotaHeader = fromTemplateJson(
|
|
76
|
-
PlataDirs.getPlataTempleteDir('swagger-cli', 'rota', 'header.json')
|
|
77
|
-
)
|
|
78
|
-
|
|
79
|
-
if (rotaHeader.errorID !== undefined) {
|
|
80
|
-
throw rotaHeader
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
files.rota.header = rotaHeader
|
|
84
|
-
|
|
85
|
-
const rotaBody = fromTemplateJson(
|
|
86
|
-
PlataDirs.getPlataTempleteDir('swagger-cli', 'rota', 'body.json')
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
if (rotaBody.errorID !== undefined) {
|
|
90
|
-
throw rotaBody
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
files.rota.body = rotaBody
|
|
94
|
-
|
|
95
|
-
const rotaReturn = fromTemplateJson(
|
|
96
|
-
PlataDirs.getPlataTempleteDir('swagger-cli', 'rota', 'response.json')
|
|
97
|
-
)
|
|
98
|
-
|
|
99
|
-
if (rotaReturn.errorID !== undefined) {
|
|
100
|
-
throw rotaReturn
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
files.rota.response = rotaReturn
|
|
104
|
-
|
|
105
|
-
return files
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function dumpRotas(): Promise<dumpRoute[]> {
|
|
109
|
-
const rotas = await buildExpressRouter()
|
|
110
|
-
const rotasMetodos: dumpRoute[] = []
|
|
111
|
-
const regex = /:(.*?)\??\//g
|
|
112
|
-
|
|
113
|
-
if (rotas._routes === undefined) return []
|
|
114
|
-
|
|
115
|
-
for (const r of rotas._routes) {
|
|
116
|
-
if (r.swaggerHide) {
|
|
117
|
-
continue
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
r.path = path.normalize(`${r.path ?? '/'}/`).replace(/\\/g, '/')
|
|
121
|
-
|
|
122
|
-
const routeParams: dumpParameter[] = []
|
|
123
|
-
const urlParms = r.path.match(regex) ?? []
|
|
124
|
-
|
|
125
|
-
for (const p of urlParms) {
|
|
126
|
-
routeParams.push({
|
|
127
|
-
name: p.replace(regex, '$1'),
|
|
128
|
-
required: p[p.length - 2] !== '?',
|
|
129
|
-
in: 'path',
|
|
130
|
-
type: 'string'
|
|
131
|
-
})
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
rotasMetodos.push({
|
|
135
|
-
httpRoute: r.path.replace(regex, '{$1}/'),
|
|
136
|
-
metodo: r.method,
|
|
137
|
-
params: routeParams,
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return rotasMetodos
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const main = async () => {
|
|
145
|
-
// TODO :: Colocar isso em um function que inicializa a process._plata ou n usa isso mais
|
|
146
|
-
(process as any)._plata = new Object(null)
|
|
147
|
-
|
|
148
|
-
const rotas = await dumpRotas()
|
|
149
|
-
const files = loadSwaggerFiles()
|
|
150
|
-
|
|
151
|
-
{
|
|
152
|
-
const pathSwaggerRotas = PlataDirs.getProjectDirSwaggerRotas()
|
|
153
|
-
|
|
154
|
-
for (const r of rotas) {
|
|
155
|
-
// https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
|
|
156
|
-
const f = JSON.parse(JSON.stringify(files))
|
|
157
|
-
|
|
158
|
-
f.rota.header.parameters.push(...r.params)
|
|
159
|
-
|
|
160
|
-
for (const json in f.rota) {
|
|
161
|
-
const result = await toProjectSwagger(
|
|
162
|
-
path.join(pathSwaggerRotas, r.httpRoute, r.metodo.toLocaleLowerCase(), `${json}.json`),
|
|
163
|
-
f.rota[json]
|
|
164
|
-
)
|
|
165
|
-
|
|
166
|
-
if (result !== null) throw result
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
{
|
|
172
|
-
const result = await toProjectSwagger(
|
|
173
|
-
path.join( PlataDirs.getProjectDirSwagger(), 'header.json'),
|
|
174
|
-
files.swaggerHeader
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
if (result !== null) throw result
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
console.log('swagger do projeto atualizado com novas rotas')
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
main()
|
package/bin/plata.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { PlataClusterManager, PlataClusterMsg } from "../libs/cluster"
|
|
2
|
-
import { loadEnvProjectFile } from "../libs/env"
|
|
3
|
-
import { loadTasks } from "../libs/tasks"
|
|
4
|
-
import { buildExpressRouter } from "../libs/routes"
|
|
5
|
-
import express, { Express } from "express"
|
|
6
|
-
import { createServer } from 'node:https'
|
|
7
|
-
import fs from 'node:fs'
|
|
8
|
-
import { PlataDirs } from "../libs/tools"
|
|
9
|
-
import path from 'node:path'
|
|
10
|
-
|
|
11
|
-
const main = async () => {
|
|
12
|
-
const clusterLib = new PlataClusterManager();
|
|
13
|
-
|
|
14
|
-
if (clusterLib.isPrimary()) {
|
|
15
|
-
{
|
|
16
|
-
console.log('Carregando .env')
|
|
17
|
-
const err = await loadEnvProjectFile()
|
|
18
|
-
|
|
19
|
-
if (err !== null) {
|
|
20
|
-
console.log(err)
|
|
21
|
-
process.exit(1)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
await loadTasks()
|
|
26
|
-
|
|
27
|
-
console.log(process.env.ENV)
|
|
28
|
-
|
|
29
|
-
clusterLib.run({
|
|
30
|
-
name: '_plata_express',
|
|
31
|
-
onStart: async () => { /*****/ },
|
|
32
|
-
env: process.env.ENV ?? 'prod'
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
clusterLib.loadClusters()
|
|
36
|
-
} else {
|
|
37
|
-
await loadEnvProjectFile()
|
|
38
|
-
|
|
39
|
-
process.on('message', async (msg: PlataClusterMsg) => {
|
|
40
|
-
process.emit(msg.id as any, msg as any)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
if (process.env._plata_name === '_plata_express') {
|
|
44
|
-
process.on('exit', () => {
|
|
45
|
-
if (process.send === undefined) {
|
|
46
|
-
console.log('Erro ao iniciar o cluster verifique a versão do node')
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
process.send({
|
|
51
|
-
id: 0,
|
|
52
|
-
name: '_plata',
|
|
53
|
-
data: {
|
|
54
|
-
action: 'KILL'
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
const port = process.env.PORT ?? 3050
|
|
60
|
-
const app: Express = express()
|
|
61
|
-
|
|
62
|
-
app.disable('x-powered-by')
|
|
63
|
-
|
|
64
|
-
const apiRoots = process.env._PLATA_API_ROOT ?? '/api'
|
|
65
|
-
|
|
66
|
-
app.use(apiRoots.split(','), await buildExpressRouter())
|
|
67
|
-
|
|
68
|
-
app.use((err, _req, res, _next) => {
|
|
69
|
-
return res.status(500).json({
|
|
70
|
-
errorId: 'B00000',
|
|
71
|
-
error: 'Erro interno da aplicação',
|
|
72
|
-
msg: err.message
|
|
73
|
-
})
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
app.all('*', (req, res) => {
|
|
77
|
-
return res.status(404).json({
|
|
78
|
-
errorId: 'B00001',
|
|
79
|
-
error: 'Rota não encontrada',
|
|
80
|
-
msg: req.path
|
|
81
|
-
})
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
if (!process.env._PLATA_API_SSL_KEY || !process.env._PLATA_API_SSL_CERT) {
|
|
85
|
-
console.log(`Nenhum certificado configurado na env ${process.env.ENV}:`)
|
|
86
|
-
console.log(`_PLATA_API_SSL_KEY: ${process.env._PLATA_API_SSL_KEY}`)
|
|
87
|
-
console.log(`_PLATA_API_SSL_CERT: ${process.env._PLATA_API_SSL_CERT}`)
|
|
88
|
-
|
|
89
|
-
app.listen(port, () => {
|
|
90
|
-
console.log(`Iniciando Worker Express na porta ${port}`)
|
|
91
|
-
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
92
|
-
})
|
|
93
|
-
} else {
|
|
94
|
-
console.log('Iniciando a Plata com ssl usando os arquivos:')
|
|
95
|
-
console.log(`key: ${process.env._PLATA_API_SSL_KEY}`)
|
|
96
|
-
console.log(`cert: ${process.env._PLATA_API_SSL_CERT}`)
|
|
97
|
-
|
|
98
|
-
const https = createServer({
|
|
99
|
-
cert: fs.readFileSync(path.normalize(path.join(
|
|
100
|
-
PlataDirs.getProjectDir(),
|
|
101
|
-
process.env._PLATA_API_SSL_CERT
|
|
102
|
-
))),
|
|
103
|
-
key: fs.readFileSync(path.normalize(path.join(
|
|
104
|
-
PlataDirs.getProjectDir(),
|
|
105
|
-
process.env._PLATA_API_SSL_KEY
|
|
106
|
-
))),
|
|
107
|
-
passphrase: process.env._PLATA_API_SSL_PASS
|
|
108
|
-
}, app)
|
|
109
|
-
|
|
110
|
-
https.listen(port, () => {
|
|
111
|
-
console.log(`Iniciando Worker Express na porta ${port} com SSL`)
|
|
112
|
-
setTimeout(() => process.removeAllListeners('exit'), 500)
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
} else {
|
|
117
|
-
await clusterLib.loadWorker()
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
main()
|
package/libs/cluster.ts
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
import { PlataRequire, PlataDirs, PlataFiles, PlataResultado } from './tools'
|
|
2
|
-
import { cpus } from 'node:os'
|
|
3
|
-
import * as typeCluster from 'node:cluster'
|
|
4
|
-
|
|
5
|
-
export type PlataWorker = typeCluster.Worker
|
|
6
|
-
|
|
7
|
-
export interface PlataClusterWorkerConfig {
|
|
8
|
-
name: string,
|
|
9
|
-
env: string,
|
|
10
|
-
onStart: () => Promise<void>
|
|
11
|
-
workers?: number,
|
|
12
|
-
workersEnv?: any,
|
|
13
|
-
onExit?: (worker: PlataWorker, code: number, signal: string) => void,
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface PlataCluster {
|
|
17
|
-
pids: number[],
|
|
18
|
-
config: PlataClusterWorkerConfig
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface PlataClusterMsg {
|
|
22
|
-
name: string,
|
|
23
|
-
id: string,
|
|
24
|
-
data: any
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface PlataClusterProcess {
|
|
28
|
-
clusters: PlataCluster[]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface _PlataClusterProcess extends NodeJS.Process {
|
|
32
|
-
_plata?: PlataClusterProcess
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
export function getClusterNodeProcess(): PlataClusterProcess {
|
|
37
|
-
const p = process as _PlataClusterProcess
|
|
38
|
-
|
|
39
|
-
if (p._plata === undefined) {
|
|
40
|
-
p._plata = {
|
|
41
|
-
clusters: []
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (p._plata.clusters === undefined) {
|
|
46
|
-
p._plata.clusters = []
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return p._plata
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export class PlataClusterManager {
|
|
53
|
-
private readonly Cluster = typeCluster.default as unknown as typeCluster.Cluster
|
|
54
|
-
private readonly Process = getClusterNodeProcess()
|
|
55
|
-
|
|
56
|
-
constructor() {
|
|
57
|
-
// Registra o evento que lida com a morte de workers
|
|
58
|
-
if (this.Cluster.isPrimary) {
|
|
59
|
-
if (this.Cluster.eventNames().indexOf('exit') === -1) {
|
|
60
|
-
this.Cluster.on('exit', (worker, code, signal) => {
|
|
61
|
-
if (worker.process.pid === undefined) return
|
|
62
|
-
|
|
63
|
-
const clusterIndex = this.Process.clusters.findIndex(cluster =>
|
|
64
|
-
cluster.pids.indexOf(worker.process.pid ?? 0) !== -1
|
|
65
|
-
)
|
|
66
|
-
|
|
67
|
-
if(clusterIndex !== -1) {
|
|
68
|
-
const cluster = this.Process.clusters[clusterIndex]
|
|
69
|
-
|
|
70
|
-
console.log(`O Worker ${worker.process.pid} do cluster ${cluster.config.name} morreu com o codigo ${code} ${signal ?? ''}`)
|
|
71
|
-
console.log(`Iniciando um novo worker no cluster ${cluster.config.name}`)
|
|
72
|
-
|
|
73
|
-
this.Cluster.emit(`_plata.cluster.worker.exit-${cluster.config.name}`, worker, code, signal)
|
|
74
|
-
|
|
75
|
-
const w = this.Cluster.fork({
|
|
76
|
-
...cluster.config.workersEnv,
|
|
77
|
-
ENV: cluster.config.env,
|
|
78
|
-
_plata_name: cluster.config.name
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
const pidIndex = cluster.pids.indexOf(worker.process.pid)
|
|
82
|
-
|
|
83
|
-
cluster.pids[pidIndex] = w.process.pid ?? 0
|
|
84
|
-
}
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (this.Cluster.eventNames().indexOf('message') === -1) {
|
|
89
|
-
this.Cluster.on('message', this._clusterMsgServer(this))
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
public async run(config: PlataClusterWorkerConfig): Promise<void> {
|
|
95
|
-
if (config.workers === undefined) {
|
|
96
|
-
if (config.env === 'prod') {
|
|
97
|
-
config.workers = cpus().length
|
|
98
|
-
} else {
|
|
99
|
-
config.workers = 1
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
console.log(`Iniciando ${config.workers} workers para o cluster ${config.name}`)
|
|
104
|
-
|
|
105
|
-
const cluster: PlataCluster = {
|
|
106
|
-
pids: [],
|
|
107
|
-
config
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
for (let i = 0; i < config.workers; i++) {
|
|
111
|
-
const w = this.Cluster.fork({
|
|
112
|
-
...config.workersEnv,
|
|
113
|
-
ENV: cluster.config.env,
|
|
114
|
-
_plata_name: cluster.config.name
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
cluster.pids.push(w.process.pid ?? 0)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (config.onExit !== undefined)
|
|
121
|
-
this.Cluster.on(`_plata.cluster.worker.exit-${cluster.config.name}`, config.onExit)
|
|
122
|
-
;
|
|
123
|
-
|
|
124
|
-
this.Process.clusters.push(cluster)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public isPrimary(): boolean {
|
|
128
|
-
return this.Cluster.isPrimary
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
public async loadClusters(): Promise<void[]> {
|
|
132
|
-
const required = await PlataRequire.requireFolderAsync<PlataClusterWorkerConfig>(PlataDirs.getProjectDirClusters())
|
|
133
|
-
|
|
134
|
-
if (required.errorID !== undefined) {
|
|
135
|
-
console.log(required)
|
|
136
|
-
process.exit(1)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
const promisesClusters: Promise<void>[] = []
|
|
140
|
-
|
|
141
|
-
for (const r of required) {
|
|
142
|
-
if (r.errorID !== undefined) {
|
|
143
|
-
console.log(required)
|
|
144
|
-
process.exit(1)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
promisesClusters.push(this.run(r.exports))
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return Promise.all(promisesClusters)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public async loadWorker() {
|
|
155
|
-
const file: string | null = await PlataFiles.findFile(
|
|
156
|
-
[
|
|
157
|
-
PlataDirs.getProjectDirClusters()
|
|
158
|
-
],
|
|
159
|
-
`${process.env["_plata_name"]}`
|
|
160
|
-
).catch(err => {
|
|
161
|
-
console.error(err)
|
|
162
|
-
return null
|
|
163
|
-
})
|
|
164
|
-
|
|
165
|
-
if (file !== null) {
|
|
166
|
-
const config: PlataClusterWorkerConfig = (await import(file.replace(/\\/g, '/'))).default
|
|
167
|
-
|
|
168
|
-
await config.onStart()
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private async sendAllWorkers(msg: PlataClusterMsg) {
|
|
173
|
-
if (this.Cluster.workers === undefined) return
|
|
174
|
-
|
|
175
|
-
for (const worker of Object.values(this.Cluster.workers)) {
|
|
176
|
-
if (worker === undefined) continue
|
|
177
|
-
|
|
178
|
-
worker.send(msg)
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private _clusterMsgServer(t: PlataClusterManager) {
|
|
183
|
-
return async (_worker: typeCluster.Worker, msg: PlataClusterMsg) => {
|
|
184
|
-
if (msg.name !== '_plata') {
|
|
185
|
-
// TODO :::
|
|
186
|
-
} else {
|
|
187
|
-
switch(msg.data.action) {
|
|
188
|
-
case 'KILL':
|
|
189
|
-
console.log('Solicitado kill do app')
|
|
190
|
-
process.exit(0)
|
|
191
|
-
;
|
|
192
|
-
case 'ECHO':
|
|
193
|
-
t.sendAllWorkers(msg.data.msg)
|
|
194
|
-
return
|
|
195
|
-
;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export async function send(msg: PlataClusterMsg): Promise<PlataResultado<boolean>> {
|
|
203
|
-
if (process.send === undefined) {
|
|
204
|
-
return {
|
|
205
|
-
errorID: 'PBLCLWS0001',
|
|
206
|
-
msg: `Erro interno no cluster da plataforma`
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
process.send({
|
|
211
|
-
name: '_plata',
|
|
212
|
-
data: {
|
|
213
|
-
action: 'ECHO',
|
|
214
|
-
msg
|
|
215
|
-
}
|
|
216
|
-
} as PlataClusterMsg)
|
|
217
|
-
|
|
218
|
-
return true
|
|
219
|
-
}
|
package/libs/env.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { PlataDirs, PlataFiles } from "./tools"
|
|
2
|
-
|
|
3
|
-
export interface EnvConfig {
|
|
4
|
-
readonly env: 'prod' | 'debug' | 'homolog' | string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export async function loadEnvProjectFile() {
|
|
8
|
-
const config = process.env['ENV'] === undefined ?
|
|
9
|
-
(await import(PlataDirs.getProjectConfigFileDir('env'))).default as EnvConfig
|
|
10
|
-
: { env: process.env['ENV'] }
|
|
11
|
-
|
|
12
|
-
return PlataFiles.readFileAsync(
|
|
13
|
-
PlataDirs.getProjectEnvFileDir(`${config.env}.env`),
|
|
14
|
-
async (line) => {
|
|
15
|
-
if (line[0] === '#') return
|
|
16
|
-
|
|
17
|
-
if (line !== '') {
|
|
18
|
-
const env = line.split('=')
|
|
19
|
-
|
|
20
|
-
if (env.length >= 2) {
|
|
21
|
-
const [ key, ...value ] = env
|
|
22
|
-
|
|
23
|
-
process.env[key.toUpperCase()] = value.join('')
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
)
|
|
28
|
-
}
|