@jayfong/x-server 1.34.6 → 1.35.1

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.
@@ -3,10 +3,13 @@
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
4
 
5
5
  exports.__esModule = true;
6
+ exports.defineSliceTask = defineSliceTask;
6
7
  exports.defineTask = defineTask;
7
8
 
8
9
  var _bull = _interopRequireDefault(require("bull"));
9
10
 
11
+ var _date = require("vtils/date");
12
+
10
13
  var _x = require("../x");
11
14
 
12
15
  function defineTask(options) {
@@ -27,4 +30,34 @@ function defineTask(options) {
27
30
  return options.handle(job.data);
28
31
  });
29
32
  return queue;
33
+ }
34
+
35
+ function defineSliceTask(options) {
36
+ const task = defineTask({
37
+ name: options.name,
38
+ concurrency: 1,
39
+ handle: async data => {
40
+ const res = await _x.x.redis.multi([['lrange', data.redisKey, '0', '-1'], ['del', data.redisKey]]).exec();
41
+ const list = res[0][1].map(item => JSON.parse(item));
42
+ return options.handle(list, data.key);
43
+ }
44
+ });
45
+ const redisKeyPrefix = `${_x.x.appId}_batch_task_${options.name}`;
46
+ const res = {
47
+ add: async (data, addOptions) => {
48
+ const key = (addOptions == null ? void 0 : addOptions.key) || '';
49
+ const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
50
+ const res = await _x.x.redis.multi([['llen', redisKey], ['lpush', redisKey, JSON.stringify(data)]]).exec();
51
+
52
+ if (parseInt(res[0][1], 10) === 0) {
53
+ await task.add({
54
+ key: key,
55
+ redisKey: redisKey
56
+ }, {
57
+ delay: addOptions != null && addOptions.duration ? (0, _date.ms)(addOptions.duration) : typeof options.duration === 'function' ? (0, _date.ms)(options.duration(key)) : (0, _date.ms)(options.duration)
58
+ });
59
+ }
60
+ }
61
+ };
62
+ return res;
30
63
  }
@@ -1,2 +1,4 @@
1
1
  import type { XTask } from './types';
2
- export declare function defineTask<T>(options: XTask.Options<T>): XTask.Task<T>;
2
+ export declare function defineTask<T>(options: XTask.TaskOptions<T>): XTask.Task<T>;
3
+ export declare function defineSliceTask<T>(options: XTask.SliceTaskOptions<T, any>): XTask.SliceTaskNoKey<T>;
4
+ export declare function defineSliceTask<T, K extends string | number>(options: XTask.SliceTaskOptions<T, K>): XTask.SliceTask<T, K>;
@@ -1,4 +1,5 @@
1
1
  import Queue from 'bull';
2
+ import { ms } from 'vtils/date';
2
3
  import { x } from "../x";
3
4
  export function defineTask(options) {
4
5
  const queue = new Queue(options.name, {
@@ -18,4 +19,33 @@ export function defineTask(options) {
18
19
  return options.handle(job.data);
19
20
  });
20
21
  return queue;
22
+ }
23
+ export function defineSliceTask(options) {
24
+ const task = defineTask({
25
+ name: options.name,
26
+ concurrency: 1,
27
+ handle: async data => {
28
+ const res = await x.redis.multi([['lrange', data.redisKey, '0', '-1'], ['del', data.redisKey]]).exec();
29
+ const list = res[0][1].map(item => JSON.parse(item));
30
+ return options.handle(list, data.key);
31
+ }
32
+ });
33
+ const redisKeyPrefix = `${x.appId}_batch_task_${options.name}`;
34
+ const res = {
35
+ add: async (data, addOptions) => {
36
+ const key = (addOptions == null ? void 0 : addOptions.key) || '';
37
+ const redisKey = !key ? redisKeyPrefix : `${redisKeyPrefix}_${key}`;
38
+ const res = await x.redis.multi([['llen', redisKey], ['lpush', redisKey, JSON.stringify(data)]]).exec();
39
+
40
+ if (parseInt(res[0][1], 10) === 0) {
41
+ await task.add({
42
+ key: key,
43
+ redisKey: redisKey
44
+ }, {
45
+ delay: addOptions != null && addOptions.duration ? ms(addOptions.duration) : typeof options.duration === 'function' ? ms(options.duration(key)) : ms(options.duration)
46
+ });
47
+ }
48
+ }
49
+ };
50
+ return res;
21
51
  }
@@ -8,6 +8,7 @@ import type { AsyncOrSync, LiteralUnion, OneOrMore, RequiredDeep } from 'vtils/t
8
8
  import type { DisposeService } from '../services/dispose';
9
9
  import type { Handler } from './handler';
10
10
  import type { IncomingHttpHeaders } from 'http';
11
+ import type { MsValue } from 'vtils/date';
11
12
  import type { MultipartFile } from 'fastify-multipart';
12
13
  import type { Queue } from 'bull';
13
14
  import type { SocketStream } from '@fastify/websocket';
@@ -138,7 +139,7 @@ export declare namespace XHandler {
138
139
  type Hook = (options: Options, ...args: Parameters<Handle>) => AsyncOrSync<void>;
139
140
  }
140
141
  export declare namespace XTask {
141
- interface Options<T> {
142
+ interface TaskOptions<T> {
142
143
  /**
143
144
  * 任务名称
144
145
  */
@@ -156,6 +157,31 @@ export declare namespace XTask {
156
157
  }
157
158
  interface Task<T> extends Queue<T> {
158
159
  }
160
+ interface SliceTaskOptions<T, K> {
161
+ /**
162
+ * 任务名称
163
+ */
164
+ name: string;
165
+ /**
166
+ * 持续时长
167
+ */
168
+ duration: MsValue | ((key: K) => MsValue);
169
+ /**
170
+ * 处理器
171
+ */
172
+ handle: (data: T[], key: K) => any;
173
+ }
174
+ interface SliceTaskNoKey<T> {
175
+ add: (data: T, options?: {
176
+ duration?: MsValue;
177
+ }) => Promise<void>;
178
+ }
179
+ interface SliceTask<T, K> {
180
+ add: (data: T, options: {
181
+ key: K;
182
+ duration?: MsValue;
183
+ }) => Promise<void>;
184
+ }
159
185
  }
160
186
  export declare namespace XCron {
161
187
  interface Options {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "1.34.6",
3
+ "version": "1.35.1",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",