vona-module-a-startup 5.0.9 → 5.0.10

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,36 @@
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
+ /** service: end */
11
+ /** service: begin */
12
+ import { Scope } from 'vona-module-a-bean';
13
+ import 'vona';
14
+ import 'vona';
15
+ import 'vona';
16
+ import 'vona';
17
+ export * from "../bean/cacheRedis.startupDebounce.js";
18
+ /** cacheRedis: end */
19
+ /** meta: begin */
20
+ export * from "../bean/meta.redlock.js";
21
+ /** service: end */
22
+ /** config: begin */
23
+ export * from "../config/config.js";
24
+ /** config: end */
25
+ /** monkey: begin */
26
+ export * from "../monkey.js";
27
+ /** meta redlock: end */
28
+ /** service: begin */
29
+ export * from "../service/startup.js";
30
+ let ScopeModuleAStartup = class ScopeModuleAStartup extends BeanScopeBase {
31
+ };
32
+ ScopeModuleAStartup = __decorate([
33
+ Scope()
34
+ ], ScopeModuleAStartup);
35
+ export { ScopeModuleAStartup };
36
+ /** scope: end */
@@ -0,0 +1,2 @@
1
+ export declare const __ThisModule__ = "a-startup";
2
+ export { ScopeModuleAStartup as ScopeModule } from './index.ts';
@@ -0,0 +1,2 @@
1
+ export const __ThisModule__ = 'a-startup';
2
+ export { ScopeModuleAStartup as ScopeModule } from "./index.js";
@@ -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,13 @@
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 CacheRedisStartupDebounce = class CacheRedisStartupDebounce extends BeanCacheRedisBase {
9
+ };
10
+ CacheRedisStartupDebounce = __decorate([
11
+ CacheRedis()
12
+ ], CacheRedisStartupDebounce);
13
+ export { CacheRedisStartupDebounce };
@@ -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,14 @@
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 { Meta } from 'vona-module-a-meta';
8
+ import { BeanRedlockBase } from 'vona-module-a-redlock';
9
+ let MetaRedlock = class MetaRedlock extends BeanRedlockBase {
10
+ };
11
+ MetaRedlock = __decorate([
12
+ Meta()
13
+ ], MetaRedlock);
14
+ export { MetaRedlock };
@@ -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,7 @@
1
+ export function config(_app) {
2
+ return {
3
+ startup: {
4
+ debounce: 10 * 1000,
5
+ },
6
+ };
7
+ }
@@ -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,3 @@
1
+ export * from "./.metadata/index.js";
2
+ export * from "./lib/index.js";
3
+ export * from "./types/index.js";
@@ -0,0 +1 @@
1
+ export * from './startup.ts';
@@ -0,0 +1 @@
1
+ export * from "./startup.js";
@@ -0,0 +1,2 @@
1
+ import type { IDecoratorStartupOptions } from '../types/startup.ts';
2
+ export declare function Startup(options?: IDecoratorStartupOptions): ClassDecorator;
@@ -0,0 +1,4 @@
1
+ import { createBeanDecorator } from 'vona';
2
+ export function Startup(options) {
3
+ return createBeanDecorator('startup', options);
4
+ }
@@ -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
+ }
package/dist/monkey.js ADDED
@@ -0,0 +1,10 @@
1
+ import { BeanSimple } from 'vona';
2
+ import { __ThisModule__ } from "./.metadata/this.js";
3
+ export class Monkey extends BeanSimple {
4
+ async appStart() {
5
+ await this.bean.scope(__ThisModule__).service.startup.appStart();
6
+ }
7
+ async appReady() {
8
+ await this.bean.scope(__ThisModule__).service.startup.appReady();
9
+ }
10
+ }
@@ -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,142 @@
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 path from 'node:path';
8
+ import { isNil } from '@cabloy/utils';
9
+ import fse from 'fs-extra';
10
+ import { BeanBase, cast } from 'vona';
11
+ import { Service } from 'vona-module-a-web';
12
+ let ServiceStartup = class ServiceStartup extends BeanBase {
13
+ async appStart() {
14
+ // clear keys
15
+ await this._clearResources();
16
+ // run startups: not after
17
+ for (const startup of this._startups) {
18
+ const startupOptions = startup.beanOptions.options;
19
+ if (!startupOptions?.instance && startupOptions?.after !== true) {
20
+ await this.runStartup(startup.name, null);
21
+ }
22
+ }
23
+ }
24
+ async appReady() {
25
+ // run startups: after
26
+ for (const startup of this._startups) {
27
+ const startupOptions = startup.beanOptions.options;
28
+ if (!startupOptions?.instance && startupOptions?.after === true) {
29
+ await this.runStartup(startup.name, null);
30
+ }
31
+ }
32
+ // version init : force: should be false
33
+ if (this.app.meta.isTest || this.app.meta.isLocal) {
34
+ const instanceName = '';
35
+ await this.bean.executor.newCtx(async () => {
36
+ await this.$scope.instance.service.instance.instanceStartup(instanceName, { force: false });
37
+ }, {
38
+ dbInfo: true,
39
+ instanceName,
40
+ });
41
+ }
42
+ else {
43
+ // all instances
44
+ const instances = await this.bean.executor.newCtx(async () => {
45
+ return await this.bean.instance.list();
46
+ }, { dbInfo: true });
47
+ for (const instance of instances) {
48
+ const instanceName = instance.name;
49
+ // need not await
50
+ this.bean.executor.newCtx(async () => {
51
+ await this.$scope.instance.service.instance.instanceStartup(instanceName, { force: false });
52
+ }, {
53
+ dbInfo: true,
54
+ instanceName,
55
+ });
56
+ }
57
+ }
58
+ // version test
59
+ if (this.app.meta.isTest) {
60
+ const instanceName = '';
61
+ await this.bean.executor.newCtx(async () => {
62
+ await this.$scope.version.service.version.__instanceTest(instanceName);
63
+ }, {
64
+ dbInfo: true,
65
+ instanceName,
66
+ });
67
+ }
68
+ }
69
+ async runStartup(startupName, instanceName, options) {
70
+ const startup = this.bean.onion.startup.onionsNormal[startupName];
71
+ const startupOptions = startup.beanOptions.options;
72
+ // normal
73
+ if (!startupOptions.debounce) {
74
+ return await this._runStartupInner(startup, instanceName, options);
75
+ }
76
+ // debounce: lock
77
+ return await this.scope.redlock.lockIsolate(`startup.${startupName}`, async () => {
78
+ return await this._runStartupLock(startup, instanceName, options);
79
+ }, {
80
+ instanceName,
81
+ });
82
+ }
83
+ async _runStartupLock(startup, instanceName, options) {
84
+ // ignore debounce for test
85
+ if (!options?.force && !this.app.meta.isTest) {
86
+ const startupOptions = startup.beanOptions.options;
87
+ const cacheKey = `startupDebounce:${startup.name}${!isNil(instanceName) ? `:${this.ctx.instance.id}` : ''}`;
88
+ const debounce = typeof startupOptions.debounce === 'number' ? startupOptions.debounce : this.scope.config.startup.debounce;
89
+ const flag = await this.scope.cacheRedis.startupDebounce.getset(true, cacheKey, { ttl: debounce });
90
+ if (flag)
91
+ return;
92
+ }
93
+ // perform
94
+ await this._runStartupInner(startup, instanceName, options);
95
+ }
96
+ async _runStartupInner(startup, instanceName, options) {
97
+ this.$logger.silly(`startup${startup.beanOptions?.options?.instance ? ' instance' : ''}: ${startup.name}, pid: ${process.pid}`);
98
+ const startupOptions = startup.beanOptions.options;
99
+ // execute
100
+ return await this.bean.executor.newCtx(async () => {
101
+ const bean = cast(this.bean._getBean(startup.beanOptions.beanFullName));
102
+ await bean.execute(options);
103
+ }, {
104
+ dbInfo: true,
105
+ instanceName,
106
+ transaction: startupOptions.transaction,
107
+ });
108
+ }
109
+ async runStartupInstance(instanceName, options) {
110
+ // run startups: not after
111
+ for (const startup of this._startups) {
112
+ const startupOptions = startup.beanOptions.options;
113
+ if (startupOptions?.instance && startupOptions?.after !== true) {
114
+ await this.runStartup(startup.name, instanceName, options);
115
+ }
116
+ }
117
+ // set flag
118
+ this.app.meta.appReadyInstances[instanceName] = true;
119
+ // run startups: after
120
+ for (const startup of this._startups) {
121
+ const startupOptions = startup.beanOptions.options;
122
+ if (startupOptions?.instance && startupOptions?.after === true) {
123
+ await this.runStartup(startup.name, instanceName, options);
124
+ }
125
+ }
126
+ }
127
+ get _startups() {
128
+ return this.bean.onion.startup.getOnionsEnabled();
129
+ }
130
+ async _clearResources() {
131
+ if (!this.app.meta.isTest)
132
+ return;
133
+ // redis
134
+ await this.$scope.redis.service.redisClient.clearAllData();
135
+ // .app/public
136
+ await fse.remove(path.join(this.app.config.server.publicDir, '1'));
137
+ }
138
+ };
139
+ ServiceStartup = __decorate([
140
+ Service()
141
+ ], ServiceStartup);
142
+ export { ServiceStartup };
@@ -0,0 +1 @@
1
+ export * from './startup.ts';
@@ -0,0 +1 @@
1
+ export * from "./startup.js";
@@ -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
+ }
@@ -0,0 +1 @@
1
+ export {};
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.10",
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 && tsc -p tsconfig.build.json"
42
46
  }
43
47
  }