@jayfong/x-server 1.34.5 → 1.35.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.
|
@@ -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: typeof options.duration === 'function' ? (0, _date.ms)(options.duration(key)) : (0, _date.ms)(options.duration)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return res;
|
|
30
63
|
}
|
|
@@ -27,7 +27,7 @@ class CacheService {
|
|
|
27
27
|
*
|
|
28
28
|
* @param key 键
|
|
29
29
|
* @param value 值
|
|
30
|
-
* @param ttl
|
|
30
|
+
* @param ttl 缓存时间
|
|
31
31
|
* @returns 返回设置的缓存内容
|
|
32
32
|
*/
|
|
33
33
|
|
|
@@ -36,8 +36,12 @@ class CacheService {
|
|
|
36
36
|
const redisKey = this.toRedisKey(key);
|
|
37
37
|
const redisValue = JSON.stringify(value);
|
|
38
38
|
const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.ttl) : ttl;
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
|
|
40
|
+
if (redisTtl) {
|
|
41
|
+
await _x.x.redis.set(redisKey, redisValue, // 毫秒
|
|
42
|
+
'PX', (0, _date.ms)(redisTtl));
|
|
43
|
+
}
|
|
44
|
+
|
|
41
45
|
return value;
|
|
42
46
|
}
|
|
43
47
|
/**
|
|
@@ -100,7 +104,7 @@ class CacheService {
|
|
|
100
104
|
*
|
|
101
105
|
* @param key 键
|
|
102
106
|
* @param action 获取缓存内容的操作
|
|
103
|
-
* @param ttl
|
|
107
|
+
* @param ttl 缓存时间
|
|
104
108
|
* @returns 返回获取到的内容
|
|
105
109
|
*/
|
|
106
110
|
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
import type { XTask } from './types';
|
|
2
|
-
export declare function defineTask<T>(options: XTask.
|
|
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>;
|
package/lib/core/define_task.js
CHANGED
|
@@ -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: typeof options.duration === 'function' ? ms(options.duration(key)) : ms(options.duration)
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return res;
|
|
21
51
|
}
|
package/lib/core/types.d.ts
CHANGED
|
@@ -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
|
|
142
|
+
interface TaskOptions<T> {
|
|
142
143
|
/**
|
|
143
144
|
* 任务名称
|
|
144
145
|
*/
|
|
@@ -156,6 +157,28 @@ 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) => Promise<void>;
|
|
176
|
+
}
|
|
177
|
+
interface SliceTask<T, K> {
|
|
178
|
+
add: (data: T, options: {
|
|
179
|
+
key: K;
|
|
180
|
+
}) => Promise<void>;
|
|
181
|
+
}
|
|
159
182
|
}
|
|
160
183
|
export declare namespace XCron {
|
|
161
184
|
interface Options {
|
package/lib/services/cache.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export declare class CacheService<TData extends Data = Data, TKey extends Key<TD
|
|
|
27
27
|
*
|
|
28
28
|
* @param key 键
|
|
29
29
|
* @param value 值
|
|
30
|
-
* @param ttl
|
|
30
|
+
* @param ttl 缓存时间
|
|
31
31
|
* @returns 返回设置的缓存内容
|
|
32
32
|
*/
|
|
33
33
|
set<K extends TKeyPath, V extends Value<TData, K>>(key: K, value: V, ttl?: MsValue | ((data: V, defaultTTL: MsValue) => MsValue)): Promise<V>;
|
|
@@ -57,7 +57,7 @@ export declare class CacheService<TData extends Data = Data, TKey extends Key<TD
|
|
|
57
57
|
*
|
|
58
58
|
* @param key 键
|
|
59
59
|
* @param action 获取缓存内容的操作
|
|
60
|
-
* @param ttl
|
|
60
|
+
* @param ttl 缓存时间
|
|
61
61
|
* @returns 返回获取到的内容
|
|
62
62
|
*/
|
|
63
63
|
remember<K extends TKeyPath, V extends Value<TData, K>>(key: K, action: () => V | Promise<V>, ttl?: MsValue | ((data: V, defaultTTL: MsValue) => MsValue)): Promise<V>;
|
package/lib/services/cache.js
CHANGED
|
@@ -17,7 +17,7 @@ export class CacheService {
|
|
|
17
17
|
*
|
|
18
18
|
* @param key 键
|
|
19
19
|
* @param value 值
|
|
20
|
-
* @param ttl
|
|
20
|
+
* @param ttl 缓存时间
|
|
21
21
|
* @returns 返回设置的缓存内容
|
|
22
22
|
*/
|
|
23
23
|
|
|
@@ -26,8 +26,12 @@ export class CacheService {
|
|
|
26
26
|
const redisKey = this.toRedisKey(key);
|
|
27
27
|
const redisValue = JSON.stringify(value);
|
|
28
28
|
const redisTtl = typeof ttl === 'function' ? ttl(value, this.options.ttl) : ttl;
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
|
|
30
|
+
if (redisTtl) {
|
|
31
|
+
await x.redis.set(redisKey, redisValue, // 毫秒
|
|
32
|
+
'PX', ms(redisTtl));
|
|
33
|
+
}
|
|
34
|
+
|
|
31
35
|
return value;
|
|
32
36
|
}
|
|
33
37
|
/**
|
|
@@ -90,7 +94,7 @@ export class CacheService {
|
|
|
90
94
|
*
|
|
91
95
|
* @param key 键
|
|
92
96
|
* @param action 获取缓存内容的操作
|
|
93
|
-
* @param ttl
|
|
97
|
+
* @param ttl 缓存时间
|
|
94
98
|
* @returns 返回获取到的内容
|
|
95
99
|
*/
|
|
96
100
|
|