pwi-plata-type 0.3.11 → 0.3.14

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/libs/cluster.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PlataRequire, PlataDirs, PlataFiles } from './tools'
1
+ import { PlataRequire, PlataDirs, PlataFiles, PlataResultado } from './tools'
2
2
  import { cpus } from 'node:os'
3
3
  import * as typeCluster from 'node:cluster'
4
4
 
@@ -86,7 +86,7 @@ export class PlataClusterManager {
86
86
  }
87
87
 
88
88
  if (this.Cluster.eventNames().indexOf('message') === -1) {
89
- this.Cluster.on('message', this._clusterMsgServer)
89
+ this.Cluster.on('message', this._clusterMsgServer(this))
90
90
  }
91
91
  }
92
92
  }
@@ -169,16 +169,51 @@ export class PlataClusterManager {
169
169
  }
170
170
  }
171
171
 
172
- private async _clusterMsgServer(_worker: typeCluster.Worker, msg: PlataClusterMsg) {
173
- if (msg.name !== '_plata') {
174
- // TODO :::
175
- } else {
176
- switch(msg.data.action) {
177
- case 'KILL':
178
- console.log('Solicitado kill do app')
179
- process.exit(0)
180
- ;
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
+ }
181
197
  }
182
198
  }
183
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
184
219
  }
package/libs/routes.ts CHANGED
@@ -86,12 +86,13 @@ export type _Router = Router & PlataRouterExtras
86
86
  export type PlataRouterBuilder = (r: PlataRouter) => PlataRouter | Promise<PlataRouter>
87
87
 
88
88
  export type PlataRequest = express.Request & {
89
- user: any, extras: any
89
+ user: any,
90
+ extras: any,
91
+ _onResponse: PlataOnResponse[]
90
92
  }
91
93
 
92
94
  export type PlataOnResponse = (req: PlataRequest, response?: any, error?: PlataError, nodeError?: any) => Promise<any>
93
95
  export interface PlataResponse extends express.Response {
94
- _onResponse: PlataOnResponse[]
95
96
  _oldJson(body?: any): this
96
97
  _onResponseEvent(req: PlataRequest, response?: any, error?: PlataError, nodeError?: any): void
97
98
  error(error: PlataError, nodeError?: any): this
@@ -149,19 +150,21 @@ export function PlataRoute(r: PlataRequestHandler): express.RequestHandler {
149
150
 
150
151
  const aRes = res as any
151
152
 
152
- if (aRes._onResponse === undefined) {
153
- aRes._onResponse = []
153
+ if (aReq._onResponse === undefined) {
154
+ aReq._onResponse = []
154
155
  }
155
156
 
156
157
  if (aRes.addOnResponse === undefined) {
157
- aRes.addOnResponse = aRes._onResponse.push
158
+ aRes.addOnResponse = (a) => {
159
+ aReq._onResponse.push(a)
160
+ }
158
161
  }
159
162
 
160
163
  if (aRes._oldJson === undefined) {
161
164
  aRes._oldJson = aRes.json
162
165
 
163
166
  aRes._onResponseEvent = (...a) => {
164
- aRes._onResponse.forEach(r => {
167
+ aReq._onResponse.forEach(r => {
165
168
  try {
166
169
  r(...a)
167
170
  } catch (e) {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.3.11",
3
+ "version": "0.3.14",
4
4
  "main": "index.ts",
5
5
  "bin": {
6
6
  "plata": "bin/plata.ts",