@maiyunnet/kebab 4.0.2 → 4.1.0

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/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "4.0.2";
8
+ export declare const VER = "4.1.0";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '4.0.2';
9
+ export const VER = '4.1.0';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/cron.d.ts CHANGED
@@ -3,32 +3,17 @@ export declare function getRegulars(): IRegularData[];
3
3
  /**
4
4
  * --- 创建定时执行的计划任务 ---
5
5
  * @param task 计划任务对象
6
- * @param immediate 如果传入的时间小于当前时间且没有执行过则立即执行一次(格式:YmdHi
6
+ * @param immediate 如果传入的时间小于当前时间且[没有执行过]则立即执行一次(格式:YmdHi,系统时区)
7
7
  */
8
8
  export declare function regular(task: IRegular, immediate?: string): Promise<boolean>;
9
- /**
10
- * --- 执行定时任务 ---
11
- */
12
- export declare function run(): void;
13
9
  /** --- 定时任务 --- */
14
10
  export interface IRegular {
15
11
  /** --- 任务名称,只能小写字母、数字、短横线、下划线,长度 1-32 --- */
16
12
  'name': string;
17
- /** --- 任务日期对象(系统时区) --- */
18
- 'date': {
19
- /** --- -1, 1 - 12 --- */
20
- 'month': number;
21
- /** --- -1, 1 - 31 --- */
22
- 'day': number;
23
- /** --- -1, 0 - 23 --- */
24
- 'hour': number;
25
- /** --- -1, 0 - 59 --- */
26
- 'minute': number;
27
- /** --- -1, 0 - 6 --- */
28
- 'week': number;
29
- };
13
+ /** --- 规则,分、时、日、月、星期,与 linux 的 cron 相同(不支持秒) --- */
14
+ 'rule': string;
30
15
  /** --- 任务函数 --- */
31
- callback: (date: string) => void | Promise<void>;
16
+ callback: (date: string, immediate: boolean) => void | Promise<void>;
32
17
  }
33
18
  export interface IRegularData extends IRegular {
34
19
  /** --- 上次执行时间字符串,格式:YmdHi(系统时区) --- */
package/lib/cron.js CHANGED
@@ -4,6 +4,7 @@
4
4
  * Last: 2025-9-20 19:46:32
5
5
  */
6
6
  import * as kebab from '#kebab/index.js';
7
+ import * as nodeCron from 'node-cron';
7
8
  import * as lFs from '#kebab/lib/fs.js';
8
9
  import * as lCore from '#kebab/lib/core.js';
9
10
  import * as lText from '#kebab/lib/text.js';
@@ -17,7 +18,7 @@ export function getRegulars() {
17
18
  /**
18
19
  * --- 创建定时执行的计划任务 ---
19
20
  * @param task 计划任务对象
20
- * @param immediate 如果传入的时间小于当前时间且没有执行过则立即执行一次(格式:YmdHi
21
+ * @param immediate 如果传入的时间小于当前时间且[没有执行过]则立即执行一次(格式:YmdHi,系统时区)
21
22
  */
22
23
  export async function regular(task, immediate = '') {
23
24
  if (!/^[a-z0-9-_]{1,32}$/.test(task.name)) {
@@ -47,12 +48,24 @@ export async function regular(task, immediate = '') {
47
48
  obj.count = json.count;
48
49
  }
49
50
  await lFs.putContent(kebab.LOG_CWD + `cron/${obj.name}.json`, JSON.stringify(obj));
51
+ // --- 好,先注册 ---
52
+ nodeCron.schedule(task.rule, () => {
53
+ /** --- 当前日期字符串 --- */
54
+ const date = lTime.format(null, 'YmdHi');
55
+ obj.callback(date, false);
56
+ // --- 设置执行后的数据 ---
57
+ obj.last = date;
58
+ ++obj.count;
59
+ ++obj.rcount;
60
+ lFs.putContent(kebab.LOG_CWD + `cron/${obj.name}.json`, JSON.stringify(obj)).catch(() => { });
61
+ });
50
62
  // --- 检查是否需要立即执行 ---
51
63
  /** --- 当前日期字符串 --- */
52
64
  const date = lTime.format(null, 'YmdHi');
53
- if (immediate && (immediate < date)) {
65
+ if ((immediate && (immediate <= date) && (obj.last < immediate))) {
66
+ // --- 理应执行的时间早于当前,或要执行的时间就是现在 ---
54
67
  try {
55
- obj.callback(date);
68
+ obj.callback(date, true);
56
69
  }
57
70
  catch (e) {
58
71
  const msg = `[CRON][${obj.name}] ${lText.stringifyJson(e.message ?? '').slice(1, -1).replace(/"/g, '""')}`;
@@ -68,59 +81,3 @@ export async function regular(task, immediate = '') {
68
81
  regulars.push(obj);
69
82
  return true;
70
83
  }
71
- /**
72
- * --- 执行定时任务 ---
73
- */
74
- export function run() {
75
- /** --- 当前日期字符串 --- */
76
- const date = lTime.format(null, 'YmdHi');
77
- /** --- 当前时间 --- */
78
- const now = new Date();
79
- /** --- 当前月份 --- */
80
- const month = now.getMonth() + 1;
81
- /** --- 当前日期 --- */
82
- const day = now.getDate();
83
- /** --- 当前小时 --- */
84
- const hour = now.getHours();
85
- /** --- 当前分钟 --- */
86
- const minute = now.getMinutes();
87
- /** --- 当前星期几 --- */
88
- const week = now.getDay();
89
- // --- 检查定时任务是否需要执行 ---
90
- for (const task of regulars) {
91
- if ((task.date.minute !== -1) && (task.date.minute !== minute)) {
92
- continue;
93
- }
94
- if ((task.date.hour !== -1) && (task.date.hour !== hour)) {
95
- continue;
96
- }
97
- if ((task.date.week !== -1) && (task.date.week !== week)) {
98
- continue;
99
- }
100
- if ((task.date.day !== -1) && (task.date.day !== day)) {
101
- continue;
102
- }
103
- if ((task.date.month !== -1) && (task.date.month !== month)) {
104
- continue;
105
- }
106
- if (task.last === date) {
107
- continue;
108
- }
109
- // --- 执行回调 ---
110
- try {
111
- task.callback(date);
112
- }
113
- catch (e) {
114
- const msg = `[CRON][${task.name}] ${lText.stringifyJson(e.message ?? '').slice(1, -1).replace(/"/g, '""')}`;
115
- lCore.debug(msg);
116
- lCore.log({}, msg, '-error');
117
- }
118
- // --- 设置执行后的数据 ---
119
- task.last = date;
120
- ++task.count;
121
- ++task.rcount;
122
- lFs.putContent(kebab.LOG_CWD + `cron/${task.name}.json`, JSON.stringify(task)).catch(() => { });
123
- }
124
- }
125
- // --- 每15秒检查一次 ---
126
- setInterval(run, 15_000);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "4.0.2",
3
+ "version": "4.1.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -19,8 +19,8 @@
19
19
  "#kebab/*": "./*"
20
20
  },
21
21
  "dependencies": {
22
- "@aws-sdk/client-s3": "^3.922.0",
23
- "@aws-sdk/lib-storage": "^3.922.0",
22
+ "@aws-sdk/client-s3": "^3.925.0",
23
+ "@aws-sdk/lib-storage": "^3.925.0",
24
24
  "@litert/http-client": "^1.1.2",
25
25
  "@litert/mime": "^0.1.3",
26
26
  "@litert/redis": "^3.0.5",
@@ -30,10 +30,11 @@
30
30
  "ejs": "^3.1.10",
31
31
  "jszip": "^3.10.1",
32
32
  "mysql2": "^3.15.3",
33
- "openai": "^6.8.0",
33
+ "node-cron": "^4.2.1",
34
+ "openai": "^6.8.1",
34
35
  "ssh2": "^1.17.0",
35
36
  "svg-captcha": "^1.4.0",
36
- "tencentcloud-sdk-nodejs": "^4.1.136"
37
+ "tencentcloud-sdk-nodejs": "^4.1.138"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@litert/eslint-plugin-rules": "^0.3.1",
@@ -3088,17 +3088,11 @@ rtn.push(reader.readBCDString());</pre>${JSON.stringify(rtn)}`);
3088
3088
  const echo = [];
3089
3089
  let rtn = await lCron.regular({
3090
3090
  'name': 'test',
3091
- 'date': {
3092
- 'month': -1,
3093
- 'day': -1,
3094
- 'hour': -1,
3095
- 'minute': -1,
3096
- 'week': -1,
3091
+ 'rule': '40 * * * *',
3092
+ 'callback': (date, immediate) => {
3093
+ lCore.debug(`[${date}] test task run, immediate: ${immediate}`);
3097
3094
  },
3098
- 'callback': (date) => {
3099
- lCore.debug(`[${date}] test task run`);
3100
- },
3101
- });
3095
+ }, '202511062305');
3102
3096
  echo.push(`<pre>await lCron.regular({
3103
3097
  'name': 'test',
3104
3098
  'date': {