vona-module-a-worker 5.0.10 → 5.0.11

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/dist/index.js CHANGED
@@ -1 +1,131 @@
1
- export * from "./.metadata/index.js";
1
+ import { BeanInfo, BeanBase, uuidv4, closeApp, BeanSimple, BeanScopeBase } from 'vona';
2
+ import { Bean, Scope } from 'vona-module-a-bean';
3
+ import cluster from 'node:cluster';
4
+ import { isNil } from '@cabloy/utils';
5
+ import { Broadcast, BeanBroadcastBase } from 'vona-module-a-broadcast';
6
+ import { CacheRedis, BeanCacheRedisBase } from 'vona-module-a-cache';
7
+
8
+ var _dec$4, _dec2$4, _class$4;
9
+ const SymbolWorkerId = Symbol('SymbolWorkerId');
10
+ let BeanWorker = (_dec$4 = Bean(), _dec2$4 = BeanInfo({
11
+ module: "a-worker"
12
+ }), _dec$4(_class$4 = _dec2$4(_class$4 = class BeanWorker extends BeanBase {
13
+ get id() {
14
+ if (this.app[SymbolWorkerId] === undefined) {
15
+ this.app[SymbolWorkerId] = uuidv4();
16
+ }
17
+ return this.app[SymbolWorkerId];
18
+ }
19
+ async setAlive(id) {
20
+ const aliveTimeout = this.scope.config.worker.alive.timeout;
21
+ const aliveTimeoutMore = this.scope.config.worker.alive.timeoutMore;
22
+ await this.scope.cacheRedis.workerAlive.set(true, id ?? this.id, {
23
+ ttl: aliveTimeout + aliveTimeoutMore
24
+ });
25
+ }
26
+ async delAlive(id) {
27
+ await this.scope.cacheRedis.workerAlive.del(id ?? this.id);
28
+ }
29
+ async getAlive(id) {
30
+ if (isNil(id) || id === this.id) return true;
31
+ return await this.scope.cacheRedis.workerAlive.get(id ?? this.id);
32
+ }
33
+ async exit(code) {
34
+ await this.app.meta.close();
35
+ process.exit(code);
36
+ }
37
+ async exitAll(code) {
38
+ this.scope.broadcast.exitAll.emit({
39
+ code
40
+ });
41
+ }
42
+ async reload() {
43
+ if (!cluster.worker) {
44
+ return;
45
+ // maybe throw uncaughtException
46
+ // throw new Error('Only take affect in cluster');
47
+ }
48
+ await closeApp(false);
49
+ cluster.worker.send('reload-worker');
50
+ }
51
+ async reloadAll() {
52
+ if (!cluster.worker) {
53
+ return;
54
+ // maybe throw uncaughtException
55
+ // throw new Error('Only take affect in cluster');
56
+ }
57
+ this.scope.broadcast.reloadAll.emit();
58
+ }
59
+ }) || _class$4) || _class$4);
60
+
61
+ var _dec$3, _dec2$3, _class$3;
62
+ let BroadcastExitAll = (_dec$3 = Broadcast(), _dec2$3 = BeanInfo({
63
+ module: "a-worker"
64
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class BroadcastExitAll extends BeanBroadcastBase {
65
+ async execute(data, _isEmitter) {
66
+ await this.bean.worker.exit(data.code);
67
+ }
68
+ }) || _class$3) || _class$3);
69
+
70
+ var _dec$2, _dec2$2, _class$2;
71
+ let BroadcastReloadAll = (_dec$2 = Broadcast(), _dec2$2 = BeanInfo({
72
+ module: "a-worker"
73
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class BroadcastReloadAll extends BeanBroadcastBase {
74
+ async execute(_data, _isEmitter) {
75
+ await this.bean.worker.reload();
76
+ }
77
+ }) || _class$2) || _class$2);
78
+
79
+ var _dec$1, _dec2$1, _class$1;
80
+ let CacheRedisWorkerAlive = (_dec$1 = CacheRedis(), _dec2$1 = BeanInfo({
81
+ module: "a-worker"
82
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class CacheRedisWorkerAlive extends BeanCacheRedisBase {}) || _class$1) || _class$1);
83
+
84
+ function config(app) {
85
+ return {
86
+ worker: {
87
+ alive: {
88
+ timeout: app.meta.isProd ? 3000 : 1000,
89
+ timeoutMore: app.meta.isProd ? 3000 : 1000
90
+ }
91
+ }
92
+ };
93
+ }
94
+
95
+ const __ThisModule__ = 'a-worker';
96
+
97
+ class Monkey extends BeanSimple {
98
+ constructor(...args) {
99
+ super(...args);
100
+ this._intervalAlive = null;
101
+ }
102
+ async moduleLoading(_module) {}
103
+ async moduleLoaded(module) {
104
+ if (module.info.relativeName !== __ThisModule__) return;
105
+ const scope = this.bean.scope(__ThisModule__);
106
+ const aliveTimeout = scope.config.worker.alive.timeout;
107
+ // interval
108
+ this._intervalAlive = setInterval(async () => {
109
+ await this.app.bean.worker.setAlive();
110
+ }, aliveTimeout);
111
+ // alive
112
+ await this.app.bean.worker.setAlive();
113
+ }
114
+ async configLoaded(_module, _config) {}
115
+ async appClosed() {
116
+ await this.bean.worker.delAlive();
117
+ if (this._intervalAlive) {
118
+ clearInterval(this._intervalAlive);
119
+ }
120
+ this._intervalAlive = null;
121
+ }
122
+ }
123
+
124
+ var _dec, _dec2, _class;
125
+ let ScopeModuleAWorker = (_dec = Scope(), _dec2 = BeanInfo({
126
+ module: "a-worker"
127
+ }), _dec(_class = _dec2(_class = class ScopeModuleAWorker extends BeanScopeBase {}) || _class) || _class);
128
+
129
+ /** scope: end */
130
+
131
+ export { BeanWorker, BroadcastExitAll, BroadcastReloadAll, CacheRedisWorkerAlive, Monkey, ScopeModuleAWorker, config };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-worker",
3
3
  "type": "module",
4
- "version": "5.0.10",
4
+ "version": "5.0.11",
5
5
  "title": "a-worker",
6
6
  "vonaModule": {
7
7
  "dependencies": {},
@@ -34,6 +34,6 @@
34
34
  },
35
35
  "scripts": {
36
36
  "clean": "rimraf dist tsconfig.build.tsbuildinfo",
37
- "tsc:publish": "npm run clean && tsc -p tsconfig.build.json"
37
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
38
38
  }
39
39
  }
@@ -1,35 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- /** monkey: end */
8
- /** scope: begin */
9
- import { BeanScopeBase } from 'vona';
10
- import { Scope } from 'vona-module-a-bean';
11
- /** bean: begin */
12
- import 'vona';
13
- import 'vona';
14
- import 'vona';
15
- export * from "../bean/bean.worker.js";
16
- /** bean: end */
17
- /** broadcast: begin */
18
- export * from "../bean/broadcast.exitAll.js";
19
- export * from "../bean/broadcast.reloadAll.js";
20
- /** broadcast: end */
21
- /** cacheRedis: begin */
22
- export * from "../bean/cacheRedis.workerAlive.js";
23
- /** cacheRedis: end */
24
- /** config: begin */
25
- export * from "../config/config.js";
26
- /** config: end */
27
- /** monkey: begin */
28
- export * from "../monkey.js";
29
- let ScopeModuleAWorker = class ScopeModuleAWorker extends BeanScopeBase {
30
- };
31
- ScopeModuleAWorker = __decorate([
32
- Scope()
33
- ], ScopeModuleAWorker);
34
- export { ScopeModuleAWorker };
35
- /** scope: end */
@@ -1,2 +0,0 @@
1
- export const __ThisModule__ = 'a-worker';
2
- export { ScopeModuleAWorker as ScopeModule } from "./index.js";
@@ -1,60 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import cluster from 'node:cluster';
8
- import { isNil } from '@cabloy/utils';
9
- import { BeanBase, closeApp, uuidv4 } from 'vona';
10
- import { Bean } from 'vona-module-a-bean';
11
- const SymbolWorkerId = Symbol('SymbolWorkerId');
12
- let BeanWorker = class BeanWorker extends BeanBase {
13
- get id() {
14
- if (this.app[SymbolWorkerId] === undefined) {
15
- this.app[SymbolWorkerId] = uuidv4();
16
- }
17
- return this.app[SymbolWorkerId];
18
- }
19
- async setAlive(id) {
20
- const aliveTimeout = this.scope.config.worker.alive.timeout;
21
- const aliveTimeoutMore = this.scope.config.worker.alive.timeoutMore;
22
- await this.scope.cacheRedis.workerAlive.set(true, id ?? this.id, { ttl: aliveTimeout + aliveTimeoutMore });
23
- }
24
- async delAlive(id) {
25
- await this.scope.cacheRedis.workerAlive.del(id ?? this.id);
26
- }
27
- async getAlive(id) {
28
- if (isNil(id) || id === this.id)
29
- return true;
30
- return await this.scope.cacheRedis.workerAlive.get(id ?? this.id);
31
- }
32
- async exit(code) {
33
- await this.app.meta.close();
34
- process.exit(code);
35
- }
36
- async exitAll(code) {
37
- this.scope.broadcast.exitAll.emit({ code });
38
- }
39
- async reload() {
40
- if (!cluster.worker) {
41
- return;
42
- // maybe throw uncaughtException
43
- // throw new Error('Only take affect in cluster');
44
- }
45
- await closeApp(false);
46
- cluster.worker.send('reload-worker');
47
- }
48
- async reloadAll() {
49
- if (!cluster.worker) {
50
- return;
51
- // maybe throw uncaughtException
52
- // throw new Error('Only take affect in cluster');
53
- }
54
- this.scope.broadcast.reloadAll.emit();
55
- }
56
- };
57
- BeanWorker = __decorate([
58
- Bean()
59
- ], BeanWorker);
60
- export { BeanWorker };
@@ -1,16 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { BeanBroadcastBase, Broadcast } from 'vona-module-a-broadcast';
8
- let BroadcastExitAll = class BroadcastExitAll extends BeanBroadcastBase {
9
- async execute(data, _isEmitter) {
10
- await this.bean.worker.exit(data.code);
11
- }
12
- };
13
- BroadcastExitAll = __decorate([
14
- Broadcast()
15
- ], BroadcastExitAll);
16
- export { BroadcastExitAll };
@@ -1,16 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { BeanBroadcastBase, Broadcast } from 'vona-module-a-broadcast';
8
- let BroadcastReloadAll = class BroadcastReloadAll extends BeanBroadcastBase {
9
- async execute(_data, _isEmitter) {
10
- await this.bean.worker.reload();
11
- }
12
- };
13
- BroadcastReloadAll = __decorate([
14
- Broadcast()
15
- ], BroadcastReloadAll);
16
- export { BroadcastReloadAll };
@@ -1,13 +0,0 @@
1
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
- return c > 3 && r && Object.defineProperty(target, key, r), r;
6
- };
7
- import { BeanCacheRedisBase, CacheRedis } from 'vona-module-a-cache';
8
- let CacheRedisWorkerAlive = class CacheRedisWorkerAlive extends BeanCacheRedisBase {
9
- };
10
- CacheRedisWorkerAlive = __decorate([
11
- CacheRedis()
12
- ], CacheRedisWorkerAlive);
13
- export { CacheRedisWorkerAlive };
@@ -1,10 +0,0 @@
1
- export function config(app) {
2
- return {
3
- worker: {
4
- alive: {
5
- timeout: app.meta.isProd ? 3000 : 1000,
6
- timeoutMore: app.meta.isProd ? 3000 : 1000,
7
- },
8
- },
9
- };
10
- }
package/dist/monkey.js DELETED
@@ -1,26 +0,0 @@
1
- import { BeanSimple } from 'vona';
2
- import { __ThisModule__ } from "./.metadata/this.js";
3
- export class Monkey extends BeanSimple {
4
- _intervalAlive = null;
5
- async moduleLoading(_module) { }
6
- async moduleLoaded(module) {
7
- if (module.info.relativeName !== __ThisModule__)
8
- return;
9
- const scope = this.bean.scope(__ThisModule__);
10
- const aliveTimeout = scope.config.worker.alive.timeout;
11
- // interval
12
- this._intervalAlive = setInterval(async () => {
13
- await this.app.bean.worker.setAlive();
14
- }, aliveTimeout);
15
- // alive
16
- await this.app.bean.worker.setAlive();
17
- }
18
- async configLoaded(_module, _config) { }
19
- async appClosed() {
20
- await this.bean.worker.delAlive();
21
- if (this._intervalAlive) {
22
- clearInterval(this._intervalAlive);
23
- }
24
- this._intervalAlive = null;
25
- }
26
- }