@eggjs/tegg-background-task 4.0.0-beta.6 → 4.0.0-beta.8

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.d.ts CHANGED
@@ -1,2 +1,16 @@
1
- import { BackgroundTaskHelper } from "./BackgroundTaskHelper.js";
1
+ import { EggObjectLifecycle } from "@eggjs/tegg-types";
2
+ import { EggAppConfig, EggLogger } from "egg";
3
+
4
+ //#region src/BackgroundTaskHelper.d.ts
5
+ declare class BackgroundTaskHelper implements EggObjectLifecycle {
6
+ logger: EggLogger;
7
+ timeout: number;
8
+ config: EggAppConfig;
9
+ private backgroundTasks;
10
+ init(): Promise<void>;
11
+ run(fn: () => Promise<void>): void;
12
+ doPreDestroy(): Promise<void>;
13
+ private sleep;
14
+ }
15
+ //#endregion
2
16
  export { BackgroundTaskHelper };
package/dist/index.js CHANGED
@@ -1,3 +1,91 @@
1
- import { BackgroundTaskHelper } from "./BackgroundTaskHelper.js";
1
+ import assert from "node:assert";
2
+ import { ContextProto, Inject } from "@eggjs/core-decorator";
3
+ import { AccessLevel } from "@eggjs/tegg-types";
4
+ import { ContextHandler, EggContextLifecycleUtil } from "@eggjs/tegg-runtime";
2
5
 
6
+ //#region \0@oxc-project+runtime@0.93.0/helpers/decorateMetadata.js
7
+ function __decorateMetadata(k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ }
10
+
11
+ //#endregion
12
+ //#region \0@oxc-project+runtime@0.93.0/helpers/decorate.js
13
+ function __decorate(decorators, target, key, desc) {
14
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16
+ 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;
17
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
18
+ }
19
+
20
+ //#endregion
21
+ //#region src/BackgroundTaskHelper.ts
22
+ let BackgroundTaskHelper = class BackgroundTaskHelper$1 {
23
+ logger;
24
+ timeout = 5e3;
25
+ config;
26
+ backgroundTasks = [];
27
+ async init() {
28
+ const ctx = ContextHandler.getContext();
29
+ assert(ctx, "background task helper must be init in context");
30
+ EggContextLifecycleUtil.registerObjectLifecycle(ctx, { preDestroy: async () => {
31
+ await this.doPreDestroy();
32
+ } });
33
+ if (this.config.backgroundTask?.timeout) this.timeout = this.config.backgroundTask.timeout;
34
+ }
35
+ run(fn) {
36
+ const backgroundTask = new Promise((resolve) => {
37
+ try {
38
+ fn().then(resolve).catch((e) => {
39
+ e.message = "[BackgroundTaskHelper] background throw error:" + e.message;
40
+ this.logger.error(e);
41
+ resolve();
42
+ });
43
+ } catch (e) {
44
+ e.message = "[BackgroundTaskHelper] create background throw error:" + e.message;
45
+ this.logger.error(e);
46
+ resolve();
47
+ }
48
+ });
49
+ this.backgroundTasks.push(backgroundTask);
50
+ }
51
+ async doPreDestroy() {
52
+ if (!this.backgroundTasks.length) return;
53
+ const backgroundTasks = this.backgroundTasks.slice();
54
+ if (this.timeout <= 0 || this.timeout === Infinity) await Promise.all(backgroundTasks);
55
+ else {
56
+ const { promise: timeout, resolve } = this.sleep();
57
+ await Promise.race([timeout, Promise.all(backgroundTasks)]);
58
+ resolve();
59
+ }
60
+ if (this.backgroundTasks.length !== backgroundTasks.length) {
61
+ this.backgroundTasks = this.backgroundTasks.slice(backgroundTasks.length);
62
+ return this.doPreDestroy();
63
+ }
64
+ }
65
+ sleep() {
66
+ let timer;
67
+ let promiseResolve;
68
+ const now = Date.now();
69
+ const p = new Promise((r) => {
70
+ promiseResolve = r;
71
+ timer = setTimeout(() => {
72
+ this.logger.error(`[BackgroundTaskHelper] task is timeout actual is ${Date.now() - now} expect is ${this.timeout}`);
73
+ r();
74
+ }, this.timeout);
75
+ });
76
+ function resolve() {
77
+ clearTimeout(timer);
78
+ promiseResolve();
79
+ }
80
+ return {
81
+ promise: p,
82
+ resolve
83
+ };
84
+ }
85
+ };
86
+ __decorate([Inject(), __decorateMetadata("design:type", Object)], BackgroundTaskHelper.prototype, "logger", void 0);
87
+ __decorate([Inject(), __decorateMetadata("design:type", Object)], BackgroundTaskHelper.prototype, "config", void 0);
88
+ BackgroundTaskHelper = __decorate([ContextProto({ accessLevel: AccessLevel.PUBLIC })], BackgroundTaskHelper);
89
+
90
+ //#endregion
3
91
  export { BackgroundTaskHelper };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@eggjs/tegg-background-task",
3
3
  "description": "background util for tegg",
4
- "version": "4.0.0-beta.6",
4
+ "version": "4.0.0-beta.8",
5
5
  "keywords": [
6
6
  "egg",
7
7
  "typescript",
@@ -32,9 +32,9 @@
32
32
  "author": "killagu <killa123@126.com>",
33
33
  "license": "MIT",
34
34
  "dependencies": {
35
- "@eggjs/core-decorator": "4.0.0-beta.6",
36
- "@eggjs/tegg-types": "4.0.0-beta.6",
37
- "@eggjs/tegg-runtime": "4.0.0-beta.6"
35
+ "@eggjs/core-decorator": "4.0.0-beta.8",
36
+ "@eggjs/tegg-runtime": "4.0.0-beta.8",
37
+ "@eggjs/tegg-types": "4.0.0-beta.8"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "egg": "beta"
@@ -44,7 +44,7 @@
44
44
  "typescript": "^5.9.3",
45
45
  "tsdown": "^0.15.6",
46
46
  "unplugin-unused": "^0.5.3",
47
- "@eggjs/tegg-common-util": "4.0.0-beta.6"
47
+ "@eggjs/tegg-common-util": "4.0.0-beta.8"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"
@@ -1,16 +0,0 @@
1
- import { EggObjectLifecycle } from "@eggjs/tegg-types";
2
- import { EggAppConfig, EggLogger } from "egg";
3
-
4
- //#region src/BackgroundTaskHelper.d.ts
5
- declare class BackgroundTaskHelper implements EggObjectLifecycle {
6
- logger: EggLogger;
7
- timeout: number;
8
- config: EggAppConfig;
9
- private backgroundTasks;
10
- init(): Promise<void>;
11
- run(fn: () => Promise<void>): void;
12
- doPreDestroy(): Promise<void>;
13
- private sleep;
14
- }
15
- //#endregion
16
- export { BackgroundTaskHelper };
@@ -1,78 +0,0 @@
1
- import { __decorateMetadata } from "./_virtual/_@oxc-project_runtime@0.93.0/helpers/decorateMetadata.js";
2
- import { __decorate } from "./_virtual/_@oxc-project_runtime@0.93.0/helpers/decorate.js";
3
- import assert from "node:assert";
4
- import { ContextProto, Inject } from "@eggjs/core-decorator";
5
- import { AccessLevel } from "@eggjs/tegg-types";
6
- import { ContextHandler, EggContextLifecycleUtil } from "@eggjs/tegg-runtime";
7
-
8
- //#region src/BackgroundTaskHelper.ts
9
- let BackgroundTaskHelper = class BackgroundTaskHelper$1 {
10
- logger;
11
- timeout = 5e3;
12
- config;
13
- backgroundTasks = [];
14
- async init() {
15
- const ctx = ContextHandler.getContext();
16
- assert(ctx, "background task helper must be init in context");
17
- EggContextLifecycleUtil.registerObjectLifecycle(ctx, { preDestroy: async () => {
18
- await this.doPreDestroy();
19
- } });
20
- if (this.config.backgroundTask?.timeout) this.timeout = this.config.backgroundTask.timeout;
21
- }
22
- run(fn) {
23
- const backgroundTask = new Promise((resolve) => {
24
- try {
25
- fn().then(resolve).catch((e) => {
26
- e.message = "[BackgroundTaskHelper] background throw error:" + e.message;
27
- this.logger.error(e);
28
- resolve();
29
- });
30
- } catch (e) {
31
- e.message = "[BackgroundTaskHelper] create background throw error:" + e.message;
32
- this.logger.error(e);
33
- resolve();
34
- }
35
- });
36
- this.backgroundTasks.push(backgroundTask);
37
- }
38
- async doPreDestroy() {
39
- if (!this.backgroundTasks.length) return;
40
- const backgroundTasks = this.backgroundTasks.slice();
41
- if (this.timeout <= 0 || this.timeout === Infinity) await Promise.all(backgroundTasks);
42
- else {
43
- const { promise: timeout, resolve } = this.sleep();
44
- await Promise.race([timeout, Promise.all(backgroundTasks)]);
45
- resolve();
46
- }
47
- if (this.backgroundTasks.length !== backgroundTasks.length) {
48
- this.backgroundTasks = this.backgroundTasks.slice(backgroundTasks.length);
49
- return this.doPreDestroy();
50
- }
51
- }
52
- sleep() {
53
- let timer;
54
- let promiseResolve;
55
- const now = Date.now();
56
- const p = new Promise((r) => {
57
- promiseResolve = r;
58
- timer = setTimeout(() => {
59
- this.logger.error(`[BackgroundTaskHelper] task is timeout actual is ${Date.now() - now} expect is ${this.timeout}`);
60
- r();
61
- }, this.timeout);
62
- });
63
- function resolve() {
64
- clearTimeout(timer);
65
- promiseResolve();
66
- }
67
- return {
68
- promise: p,
69
- resolve
70
- };
71
- }
72
- };
73
- __decorate([Inject(), __decorateMetadata("design:type", Object)], BackgroundTaskHelper.prototype, "logger", void 0);
74
- __decorate([Inject(), __decorateMetadata("design:type", Object)], BackgroundTaskHelper.prototype, "config", void 0);
75
- BackgroundTaskHelper = __decorate([ContextProto({ accessLevel: AccessLevel.PUBLIC })], BackgroundTaskHelper);
76
-
77
- //#endregion
78
- export { BackgroundTaskHelper };
@@ -1,10 +0,0 @@
1
- //#region \0@oxc-project+runtime@0.93.0/helpers/decorate.js
2
- function __decorate(decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- 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;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- }
8
-
9
- //#endregion
10
- export { __decorate };
@@ -1,7 +0,0 @@
1
- //#region \0@oxc-project+runtime@0.93.0/helpers/decorateMetadata.js
2
- function __decorateMetadata(k, v) {
3
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
4
- }
5
-
6
- //#endregion
7
- export { __decorateMetadata };