vona-module-a-startup 5.0.9 → 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.
@@ -0,0 +1,92 @@
1
+ import type { BeanScopeUtil, TypeModuleConfig } from 'vona';
2
+ /** cacheRedis: begin */
3
+ import type { IDecoratorCacheRedisOptions } from 'vona-module-a-cache';
4
+ /** cacheRedis: end */
5
+ /** cacheRedis: begin */
6
+ import type { CacheRedisStartupDebounce } from '../bean/cacheRedis.startupDebounce.ts';
7
+ /** meta: end */
8
+ /** meta redlock: begin */
9
+ import type { MetaRedlock } from '../bean/meta.redlock.ts';
10
+ import type { config } from '../config/config.ts';
11
+ /** service: end */
12
+ /** service: begin */
13
+ import type { ServiceStartup } from '../service/startup.ts';
14
+ /** monkey: end */
15
+ /** scope: begin */
16
+ import { BeanScopeBase } from 'vona';
17
+ import 'vona';
18
+ import 'vona';
19
+ import 'vona';
20
+ import 'vona';
21
+ export * from '../bean/cacheRedis.startupDebounce.ts';
22
+ declare module 'vona-module-a-cache' {
23
+ interface ICacheRedisRecord {
24
+ 'a-startup:startupDebounce': IDecoratorCacheRedisOptions;
25
+ }
26
+ }
27
+ declare module 'vona-module-a-startup' {
28
+ interface CacheRedisStartupDebounce {
29
+ }
30
+ }
31
+ export interface IModuleCacheRedis {
32
+ startupDebounce: CacheRedisStartupDebounce;
33
+ }
34
+ /** cacheRedis: end */
35
+ /** meta: begin */
36
+ export * from '../bean/meta.redlock.ts';
37
+ declare module 'vona' {
38
+ interface IMetaRecord {
39
+ 'a-startup:redlock': never;
40
+ }
41
+ }
42
+ declare module 'vona-module-a-startup' {
43
+ interface MetaRedlock {
44
+ }
45
+ }
46
+ /** service: end */
47
+ /** config: begin */
48
+ export * from '../config/config.ts';
49
+ declare module 'vona-module-a-web' {
50
+ interface IServiceRecord {
51
+ 'a-startup:startup': never;
52
+ }
53
+ }
54
+ declare module 'vona-module-a-startup' {
55
+ interface ServiceStartup {
56
+ }
57
+ }
58
+ export interface IModuleService {
59
+ startup: ServiceStartup;
60
+ }
61
+ declare module 'vona' {
62
+ interface IBeanRecordGeneral {
63
+ 'a-startup.service.startup': ServiceStartup;
64
+ }
65
+ }
66
+ /** config: end */
67
+ /** monkey: begin */
68
+ export * from '../monkey.ts';
69
+ /** meta redlock: end */
70
+ /** service: begin */
71
+ export * from '../service/startup.ts';
72
+ export declare class ScopeModuleAStartup extends BeanScopeBase {
73
+ }
74
+ export interface ScopeModuleAStartup {
75
+ util: BeanScopeUtil;
76
+ config: TypeModuleConfig<typeof config>;
77
+ cacheRedis: IModuleCacheRedis;
78
+ redlock: MetaRedlock;
79
+ service: IModuleService;
80
+ }
81
+ declare module 'vona' {
82
+ interface IBeanScopeRecord {
83
+ 'a-startup': ScopeModuleAStartup;
84
+ }
85
+ interface IBeanScopeContainer {
86
+ startup: ScopeModuleAStartup;
87
+ }
88
+ interface IBeanScopeConfig {
89
+ 'a-startup': ReturnType<typeof config>;
90
+ }
91
+ }
92
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-startup";
2
+ export { ScopeModuleAStartup as ScopeModule } from './index.ts';
@@ -0,0 +1,5 @@
1
+ import { BeanCacheRedisBase } from 'vona-module-a-cache';
2
+ export type TCacheRedisStartupDebounceKey = `startupDebounce:${string}`;
3
+ export type TCacheRedisStartupDebounceData = boolean;
4
+ export declare class CacheRedisStartupDebounce extends BeanCacheRedisBase<TCacheRedisStartupDebounceKey, TCacheRedisStartupDebounceData> {
5
+ }
@@ -0,0 +1,8 @@
1
+ import type { FunctionAsync } from 'vona';
2
+ import type { IRedlockLockIsolateOptions } from 'vona-module-a-redlock';
3
+ import { BeanRedlockBase } from 'vona-module-a-redlock';
4
+ export interface MetaRedlock {
5
+ lockIsolate<RESULT, KEY extends string>(resource: `startup.${KEY}`, fn: FunctionAsync<RESULT>, options?: IRedlockLockIsolateOptions): Promise<RESULT>;
6
+ }
7
+ export declare class MetaRedlock extends BeanRedlockBase {
8
+ }
@@ -0,0 +1,6 @@
1
+ import type { VonaApplication } from 'vona';
2
+ export declare function config(_app: VonaApplication): {
3
+ startup: {
4
+ debounce: number;
5
+ };
6
+ };
@@ -0,0 +1,3 @@
1
+ export * from './.metadata/index.ts';
2
+ export * from './lib/index.ts';
3
+ export * from './types/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,190 @@
1
+ import { BeanInfo, BeanSimple, BeanBase, cast, BeanScopeBase, createBeanDecorator } from 'vona';
2
+ import { Scope } from 'vona-module-a-bean';
3
+ import { CacheRedis, BeanCacheRedisBase } from 'vona-module-a-cache';
4
+ import { Meta } from 'vona-module-a-meta';
5
+ import { BeanRedlockBase } from 'vona-module-a-redlock';
6
+ import path from 'node:path';
7
+ import { isNil } from '@cabloy/utils';
8
+ import fse from 'fs-extra';
9
+ import { Service } from 'vona-module-a-web';
10
+
11
+ var _dec$3, _dec2$3, _class$3;
12
+ let CacheRedisStartupDebounce = (_dec$3 = CacheRedis(), _dec2$3 = BeanInfo({
13
+ module: "a-startup"
14
+ }), _dec$3(_class$3 = _dec2$3(_class$3 = class CacheRedisStartupDebounce extends BeanCacheRedisBase {}) || _class$3) || _class$3);
15
+
16
+ var _dec$2, _dec2$2, _class$2;
17
+ let MetaRedlock = (_dec$2 = Meta(), _dec2$2 = BeanInfo({
18
+ module: "a-startup"
19
+ }), _dec$2(_class$2 = _dec2$2(_class$2 = class MetaRedlock extends BeanRedlockBase {}) || _class$2) || _class$2);
20
+
21
+ function config(_app) {
22
+ return {
23
+ startup: {
24
+ debounce: 10 * 1000
25
+ }
26
+ };
27
+ }
28
+
29
+ const __ThisModule__ = 'a-startup';
30
+
31
+ class Monkey extends BeanSimple {
32
+ async appStart() {
33
+ await this.bean.scope(__ThisModule__).service.startup.appStart();
34
+ }
35
+ async appReady() {
36
+ await this.bean.scope(__ThisModule__).service.startup.appReady();
37
+ }
38
+ }
39
+
40
+ var _dec$1, _dec2$1, _class$1;
41
+ let ServiceStartup = (_dec$1 = Service(), _dec2$1 = BeanInfo({
42
+ module: "a-startup"
43
+ }), _dec$1(_class$1 = _dec2$1(_class$1 = class ServiceStartup extends BeanBase {
44
+ async appStart() {
45
+ // clear keys
46
+ await this._clearResources();
47
+
48
+ // run startups: not after
49
+ for (const startup of this._startups) {
50
+ const startupOptions = startup.beanOptions.options;
51
+ if (!startupOptions?.instance && startupOptions?.after !== true) {
52
+ await this.runStartup(startup.name, null);
53
+ }
54
+ }
55
+ }
56
+ async appReady() {
57
+ // run startups: after
58
+ for (const startup of this._startups) {
59
+ const startupOptions = startup.beanOptions.options;
60
+ if (!startupOptions?.instance && startupOptions?.after === true) {
61
+ await this.runStartup(startup.name, null);
62
+ }
63
+ }
64
+
65
+ // version init : force: should be false
66
+ if (this.app.meta.isTest || this.app.meta.isLocal) {
67
+ const instanceName = '';
68
+ await this.bean.executor.newCtx(async () => {
69
+ await this.$scope.instance.service.instance.instanceStartup(instanceName, {
70
+ force: false
71
+ });
72
+ }, {
73
+ dbInfo: true,
74
+ instanceName
75
+ });
76
+ } else {
77
+ // all instances
78
+ const instances = await this.bean.executor.newCtx(async () => {
79
+ return await this.bean.instance.list();
80
+ }, {
81
+ dbInfo: true
82
+ });
83
+ for (const instance of instances) {
84
+ const instanceName = instance.name;
85
+ // need not await
86
+ this.bean.executor.newCtx(async () => {
87
+ await this.$scope.instance.service.instance.instanceStartup(instanceName, {
88
+ force: false
89
+ });
90
+ }, {
91
+ dbInfo: true,
92
+ instanceName
93
+ });
94
+ }
95
+ }
96
+
97
+ // version test
98
+ if (this.app.meta.isTest) {
99
+ const instanceName = '';
100
+ await this.bean.executor.newCtx(async () => {
101
+ await this.$scope.version.service.version.__instanceTest(instanceName);
102
+ }, {
103
+ dbInfo: true,
104
+ instanceName
105
+ });
106
+ }
107
+ }
108
+ async runStartup(startupName, instanceName, options) {
109
+ const startup = this.bean.onion.startup.onionsNormal[startupName];
110
+ const startupOptions = startup.beanOptions.options;
111
+ // normal
112
+ if (!startupOptions.debounce) {
113
+ return await this._runStartupInner(startup, instanceName, options);
114
+ }
115
+ // debounce: lock
116
+ return await this.scope.redlock.lockIsolate(`startup.${startupName}`, async () => {
117
+ return await this._runStartupLock(startup, instanceName, options);
118
+ }, {
119
+ instanceName
120
+ });
121
+ }
122
+ async _runStartupLock(startup, instanceName, options) {
123
+ // ignore debounce for test
124
+ if (!options?.force && !this.app.meta.isTest) {
125
+ const startupOptions = startup.beanOptions.options;
126
+ const cacheKey = `startupDebounce:${startup.name}${!isNil(instanceName) ? `:${this.ctx.instance.id}` : ''}`;
127
+ const debounce = typeof startupOptions.debounce === 'number' ? startupOptions.debounce : this.scope.config.startup.debounce;
128
+ const flag = await this.scope.cacheRedis.startupDebounce.getset(true, cacheKey, {
129
+ ttl: debounce
130
+ });
131
+ if (flag) return;
132
+ }
133
+ // perform
134
+ await this._runStartupInner(startup, instanceName, options);
135
+ }
136
+ async _runStartupInner(startup, instanceName, options) {
137
+ this.$logger.silly(`startup${startup.beanOptions?.options?.instance ? ' instance' : ''}: ${startup.name}, pid: ${process.pid}`);
138
+ const startupOptions = startup.beanOptions.options;
139
+ // execute
140
+ return await this.bean.executor.newCtx(async () => {
141
+ const bean = cast(this.bean._getBean(startup.beanOptions.beanFullName));
142
+ await bean.execute(options);
143
+ }, {
144
+ dbInfo: true,
145
+ instanceName,
146
+ transaction: startupOptions.transaction
147
+ });
148
+ }
149
+ async runStartupInstance(instanceName, options) {
150
+ // run startups: not after
151
+ for (const startup of this._startups) {
152
+ const startupOptions = startup.beanOptions.options;
153
+ if (startupOptions?.instance && startupOptions?.after !== true) {
154
+ await this.runStartup(startup.name, instanceName, options);
155
+ }
156
+ }
157
+ // set flag
158
+ this.app.meta.appReadyInstances[instanceName] = true;
159
+ // run startups: after
160
+ for (const startup of this._startups) {
161
+ const startupOptions = startup.beanOptions.options;
162
+ if (startupOptions?.instance && startupOptions?.after === true) {
163
+ await this.runStartup(startup.name, instanceName, options);
164
+ }
165
+ }
166
+ }
167
+ get _startups() {
168
+ return this.bean.onion.startup.getOnionsEnabled();
169
+ }
170
+ async _clearResources() {
171
+ if (!this.app.meta.isTest) return;
172
+ // redis
173
+ await this.$scope.redis.service.redisClient.clearAllData();
174
+ // .app/public
175
+ await fse.remove(path.join(this.app.config.server.publicDir, '1'));
176
+ }
177
+ }) || _class$1) || _class$1);
178
+
179
+ var _dec, _dec2, _class;
180
+ let ScopeModuleAStartup = (_dec = Scope(), _dec2 = BeanInfo({
181
+ module: "a-startup"
182
+ }), _dec(_class = _dec2(_class = class ScopeModuleAStartup extends BeanScopeBase {}) || _class) || _class);
183
+
184
+ /** scope: end */
185
+
186
+ function Startup(options) {
187
+ return createBeanDecorator('startup', options);
188
+ }
189
+
190
+ export { CacheRedisStartupDebounce, MetaRedlock, Monkey, ScopeModuleAStartup, ServiceStartup, Startup, config };
@@ -0,0 +1 @@
1
+ export * from './startup.ts';
@@ -0,0 +1,2 @@
1
+ import type { IDecoratorStartupOptions } from '../types/startup.ts';
2
+ export declare function Startup(options?: IDecoratorStartupOptions): ClassDecorator;
@@ -0,0 +1,6 @@
1
+ import type { IMonkeyAppReady, IMonkeyAppStart } from 'vona';
2
+ import { BeanSimple } from 'vona';
3
+ export declare class Monkey extends BeanSimple implements IMonkeyAppStart, IMonkeyAppReady {
4
+ appStart(): Promise<void>;
5
+ appReady(): Promise<void>;
6
+ }
@@ -0,0 +1,13 @@
1
+ import type { IOnionSlice } from 'vona-module-a-onion';
2
+ import type { IDecoratorStartupOptions, IInstanceStartupOptions } from '../types/startup.ts';
3
+ import { BeanBase } from 'vona';
4
+ export declare class ServiceStartup extends BeanBase {
5
+ appStart(): Promise<void>;
6
+ appReady(): Promise<void>;
7
+ runStartup(startupName: string, instanceName?: string | null, options?: IInstanceStartupOptions): Promise<void>;
8
+ _runStartupLock(startup: IOnionSlice<IDecoratorStartupOptions>, instanceName?: string | null, options?: IInstanceStartupOptions): Promise<void>;
9
+ _runStartupInner(startup: IOnionSlice<IDecoratorStartupOptions>, instanceName?: string | null, options?: IInstanceStartupOptions): Promise<void>;
10
+ runStartupInstance(instanceName: string, options?: IInstanceStartupOptions): Promise<void>;
11
+ private get _startups();
12
+ private _clearResources;
13
+ }
@@ -0,0 +1 @@
1
+ export * from './startup.ts';
@@ -0,0 +1,30 @@
1
+ import type { ConfigInstanceBase, OmitNever } from 'vona';
2
+ import type { IOnionOptionsDeps, IOnionOptionsEnable, ServiceOnion } from 'vona-module-a-onion';
3
+ export interface IStartupRecord {
4
+ }
5
+ export interface IStartupExecute {
6
+ execute: (options?: IInstanceStartupOptions) => Promise<void>;
7
+ }
8
+ export interface IDecoratorStartupOptions extends IOnionOptionsEnable, IOnionOptionsDeps<keyof IStartupRecord> {
9
+ instance?: boolean;
10
+ debounce?: boolean | number;
11
+ transaction?: boolean;
12
+ after?: boolean;
13
+ }
14
+ export interface IInstanceStartupOptions {
15
+ force?: boolean;
16
+ configInstanceBase?: ConfigInstanceBase;
17
+ }
18
+ declare module 'vona-module-a-onion' {
19
+ interface BeanOnion {
20
+ startup: ServiceOnion<IDecoratorStartupOptions, keyof IStartupRecord>;
21
+ }
22
+ }
23
+ declare module 'vona' {
24
+ interface ConfigOnions {
25
+ startup: OmitNever<IStartupRecord>;
26
+ }
27
+ interface IBeanSceneRecord {
28
+ startup: never;
29
+ }
30
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-module-a-startup",
3
3
  "type": "module",
4
- "version": "5.0.9",
4
+ "version": "5.0.11",
5
5
  "title": "a-startup",
6
6
  "vonaModule": {
7
7
  "dependencies": {},
@@ -36,8 +36,12 @@
36
36
  "dist",
37
37
  "static"
38
38
  ],
39
+ "devDependencies": {
40
+ "clean-package": "^2.2.0",
41
+ "rimraf": "^6.0.1"
42
+ },
39
43
  "scripts": {
40
- "clean": "rimraf dist tsconfig.tsbuildinfo",
41
- "tsc:publish": "npm run clean && tsc"
44
+ "clean": "rimraf dist tsconfig.build.tsbuildinfo",
45
+ "tsc:publish": "npm run clean && vona :bin:buildModule && tsc -p tsconfig.build.json"
42
46
  }
43
47
  }