wok-server 0.4.10 → 0.4.12

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.
@@ -171,9 +171,6 @@ class BaseMysqlManager {
171
171
  * @returns 更新是否成功
172
172
  */
173
173
  updateOne(table, criteria, updater) {
174
- if (!Object.keys(criteria).length) {
175
- throw new exception_1.MysqlException('criteria cannot be empty !');
176
- }
177
174
  if (!Object.keys(updater).length) {
178
175
  throw new exception_1.MysqlException('updater cannot be empty !');
179
176
  }
@@ -8,9 +8,10 @@ const task_1 = require("./task");
8
8
  * @param hours 时
9
9
  * @param minutes 分
10
10
  * @param task 要执行的任务
11
+ * @param timeout 任务超时时间,单位毫秒
11
12
  * @returns
12
13
  */
13
- function scheduleDailyTask(hours, minutes, task) {
14
+ function scheduleDailyTask(hours, minutes, task, timeout) {
14
15
  // 校验
15
16
  (0, validation_1.validate)({ hours, minutes }, {
16
17
  hours: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(23)],
@@ -18,7 +19,7 @@ function scheduleDailyTask(hours, minutes, task) {
18
19
  });
19
20
  const taskController = new task_1.TaskController();
20
21
  const delay = dailyTaskDelay(hours, minutes);
21
- setTimeout(() => exec(hours, minutes, task, taskController), delay);
22
+ setTimeout(() => exec(hours, minutes, task, taskController, timeout), delay);
22
23
  return taskController;
23
24
  }
24
25
  exports.scheduleDailyTask = scheduleDailyTask;
@@ -44,13 +45,13 @@ function dailyTaskDelay(hours, minutes) {
44
45
  return tomorrowTime - now.getTime();
45
46
  }
46
47
  exports.dailyTaskDelay = dailyTaskDelay;
47
- function exec(hours, minutes, task, controller) {
48
+ function exec(hours, minutes, task, controller, timeout) {
48
49
  Promise.resolve()
49
50
  .then(async () => {
50
51
  if (controller.isStopped()) {
51
52
  return;
52
53
  }
53
- await (0, task_1.execTask)(task);
54
+ await (0, task_1.execTask)(task, timeout);
54
55
  const delay = dailyTaskDelay(hours, minutes);
55
56
  setTimeout(() => exec(hours, minutes, task, controller), delay);
56
57
  })
@@ -9,24 +9,25 @@ const task_1 = require("./task");
9
9
  * @param initialDelay 第一次执行延迟的时间,单位秒
10
10
  * @param delay 每次的延迟时间,单位秒
11
11
  * @param task 任务
12
+ * @param timeout 任务超时时间,单位毫秒
12
13
  */
13
- function scheduleWithFixedDelay(initialDelay, delay, task) {
14
+ function scheduleWithFixedDelay(initialDelay, delay, task, timeout) {
14
15
  (0, validation_1.validate)({ initialDelay, delay }, {
15
16
  initialDelay: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(3600 * 24)],
16
17
  delay: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(3600 * 24)]
17
18
  });
18
19
  const controller = new task_1.TaskController();
19
- setTimeout(() => exec(delay, task, controller), initialDelay * 1000);
20
+ setTimeout(() => exec(delay, task, controller, timeout), initialDelay * 1000);
20
21
  return controller;
21
22
  }
22
23
  exports.scheduleWithFixedDelay = scheduleWithFixedDelay;
23
- function exec(delay, task, controller) {
24
+ function exec(delay, task, controller, timeout) {
24
25
  Promise.resolve()
25
26
  .then(async () => {
26
27
  if (controller.isStopped()) {
27
28
  return;
28
29
  }
29
- await (0, task_1.execTask)(task);
30
+ await (0, task_1.execTask)(task, timeout);
30
31
  // 下次执行
31
32
  setTimeout(() => exec(delay, task, controller), delay * 1000);
32
33
  })
@@ -9,24 +9,25 @@ const task_1 = require("./task");
9
9
  * @param initialDelay 第一次执行延迟的时间,单位秒
10
10
  * @param period 每次的延迟时间,单位秒
11
11
  * @param task 任务
12
+ * @param timeout 任务超时时间,单位毫秒
12
13
  */
13
- function scheduleWithFixedRate(initialDelay, period, task) {
14
+ function scheduleWithFixedRate(initialDelay, period, task, timeout) {
14
15
  (0, validation_1.validate)({ initialDelay, period }, {
15
16
  initialDelay: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(3600 * 24)],
16
17
  period: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(3600 * 24)]
17
18
  });
18
19
  const taskController = new task_1.TaskController();
19
- setTimeout(() => exec(period, task, taskController), initialDelay * 1000);
20
+ setTimeout(() => exec(period, task, taskController, timeout), initialDelay * 1000);
20
21
  return taskController;
21
22
  }
22
23
  exports.scheduleWithFixedRate = scheduleWithFixedRate;
23
- function exec(fixedDelay, task, controller) {
24
+ function exec(fixedDelay, task, controller, timeout) {
24
25
  Promise.resolve()
25
26
  .then(async () => {
26
27
  if (controller.isStopped()) {
27
28
  return;
28
29
  }
29
- const res = await (0, task_1.execTask)(task);
30
+ const res = await (0, task_1.execTask)(task, timeout);
30
31
  // 下次执行
31
32
  let delay = res.start + fixedDelay * 1000 - new Date().getTime();
32
33
  if (delay < 0) {
package/dist/task/task.js CHANGED
@@ -18,13 +18,27 @@ exports.TaskController = TaskController;
18
18
  /**
19
19
  * 任务执行,封装任务执行过程中的一些通用信息输出和异常控制.
20
20
  * @param task
21
+ * @param timeout 任务超时时间,单位毫秒
21
22
  * @returns
22
23
  */
23
- async function execTask(task) {
24
+ async function execTask(task, timeout) {
24
25
  const start = new Date().getTime();
25
26
  try {
26
27
  (0, log_1.getLogger)().debug(`START TASK:${task.name}`);
27
- await task.run();
28
+ // 支持任务超时设置
29
+ if (timeout && timeout > 0) {
30
+ await Promise.race([
31
+ task.run(),
32
+ new Promise((_, reject) => {
33
+ setTimeout(() => {
34
+ reject('The task has timed out.');
35
+ }, timeout);
36
+ })
37
+ ]);
38
+ }
39
+ else {
40
+ await task.run();
41
+ }
28
42
  }
29
43
  catch (e) {
30
44
  (0, log_1.getLogger)().error(`TASK ERROR: ${task.name}`, e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wok-server",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "packageManager": "pnpm@8.9.0",
5
5
  "description": "一个基于 NodeJs 和 TypeScript 的后端框架,轻量级、克制、简洁。A lightweight, restrained, and concise backend framework based on Node.js and TypeScript.",
6
6
  "scripts": {
@@ -116,7 +116,7 @@ export declare abstract class BaseMysqlManager {
116
116
  * @param updater
117
117
  * @returns 更新是否成功
118
118
  */
119
- updateOne<T>(table: Table<T>, criteria: Partial<T>, updater: Updater<T>): Promise<boolean>;
119
+ updateOne<T>(table: Table<T>, criteria: MixCriteria<T>, updater: Updater<T>): Promise<boolean>;
120
120
  /**
121
121
  * 部分更新
122
122
  * @param table 表信息
@@ -39,7 +39,7 @@ export declare function partialUpdate<T>(config: MysqlConfig, connection: PoolCo
39
39
  * @param query
40
40
  * @param data
41
41
  */
42
- export declare function updateOne<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, query: Partial<T>, updater: Updater<T>): Promise<boolean>;
42
+ export declare function updateOne<T>(config: MysqlConfig, connection: PoolConnection, table: Table<T>, query: MixCriteria<T>, updater: Updater<T>): Promise<boolean>;
43
43
  /**
44
44
  * 更新选项
45
45
  */
@@ -4,9 +4,10 @@ import { Task, TaskController } from './task';
4
4
  * @param hours 时
5
5
  * @param minutes 分
6
6
  * @param task 要执行的任务
7
+ * @param timeout 任务超时时间,单位毫秒
7
8
  * @returns
8
9
  */
9
- export declare function scheduleDailyTask(hours: number, minutes: number, task: Task): TaskController;
10
+ export declare function scheduleDailyTask(hours: number, minutes: number, task: Task, timeout?: number): TaskController;
10
11
  /**
11
12
  * 计算到下次指定时间点的延迟
12
13
  * @param hours
@@ -4,5 +4,6 @@ import { Task, TaskController } from './task';
4
4
  * @param initialDelay 第一次执行延迟的时间,单位秒
5
5
  * @param delay 每次的延迟时间,单位秒
6
6
  * @param task 任务
7
+ * @param timeout 任务超时时间,单位毫秒
7
8
  */
8
- export declare function scheduleWithFixedDelay(initialDelay: number, delay: number, task: Task): TaskController;
9
+ export declare function scheduleWithFixedDelay(initialDelay: number, delay: number, task: Task, timeout?: number): TaskController;
@@ -4,5 +4,6 @@ import { Task, TaskController } from './task';
4
4
  * @param initialDelay 第一次执行延迟的时间,单位秒
5
5
  * @param period 每次的延迟时间,单位秒
6
6
  * @param task 任务
7
+ * @param timeout 任务超时时间,单位毫秒
7
8
  */
8
- export declare function scheduleWithFixedRate(initialDelay: number, period: number, task: Task): TaskController;
9
+ export declare function scheduleWithFixedRate(initialDelay: number, period: number, task: Task, timeout?: number): TaskController;
@@ -24,9 +24,10 @@ export declare class TaskController {
24
24
  /**
25
25
  * 任务执行,封装任务执行过程中的一些通用信息输出和异常控制.
26
26
  * @param task
27
+ * @param timeout 任务超时时间,单位毫秒
27
28
  * @returns
28
29
  */
29
- export declare function execTask(task: Task): Promise<{
30
+ export declare function execTask(task: Task, timeout?: number): Promise<{
30
31
  start: number;
31
32
  cost: number;
32
33
  end: number;