This commit is contained in:
2025-05-01 03:59:37 +08:00
parent 26ca4c21c8
commit e58adbc46b
45 changed files with 7460 additions and 87 deletions

0
src/modules/bullmq.ts Normal file
View File

View File

@@ -1,5 +1,6 @@
import { Redis } from 'ioredis';
import { config } from './config.ts';
import { useContextKey } from '@kevisual/use-config/context';
type initRedisOpts = {
onConnect?: () => void; // 连接成功的回调函数
@@ -36,7 +37,7 @@ const initRedis = (config?: any, options?: initRedisOpts) => {
return redis;
};
// 配置 Redis 连接
export const redis = useConfigKey('redis', () => initRedis(config));
export const redis = useContextKey('redis', () => initRedis(config));
// 初始化 Redis 客户端
export const redisPublisher = new Redis(); // 用于发布消息

2
src/task/common.ts Normal file
View File

@@ -0,0 +1,2 @@
// https://edith.xiaohongshu.com/api/sns/web/unread_count
export const XHS_GET_UNREAD = 'unread_count';

0
src/task/index.ts Normal file
View File

43
src/task/task.ts Normal file
View File

@@ -0,0 +1,43 @@
import { redis } from '@/modules/redis.ts';
import { Queue } from 'bullmq';
import { nanoid } from 'nanoid';
const XHS_QUEUE_NAME = 'XHS_QUEUE';
export const queue = new Queue(XHS_QUEUE_NAME, {
connection: redis,
});
// 初始启动
async function start() {
const res = await queue.add(
'start-job',
{
name: 'initialJob',
},
{
delay: 0, // 立即执行
removeOnComplete: true,
removeOnFail: true,
jobId: nanoid(), // 使用 nanoid 生成唯一 ID
},
);
console.log('Queue started:', res.id);
}
//
start();
const getTasks = async () => {
const tasks = await queue.getJobs(['waiting', 'active', 'completed', 'failed']);
return tasks;
};
const getTask = async (id: string) => {
const task = await queue.getJob(id);
return task;
};
const removeTask = async (id: string) => {
const task = await queue.getJob(id);
if (task) {
await task.remove();
}
};
// const task = await getTask('4');
// console.log('task', task);

52
src/task/worker.ts Normal file
View File

@@ -0,0 +1,52 @@
import { redis } from '@/modules/redis.ts';
import { Queue, Worker } from 'bullmq';
import { clamp } from 'lodash-es';
import { nanoid } from 'nanoid';
const XHS_QUEUE_NAME = 'XHS_QUEUE';
export const queue = new Queue(XHS_QUEUE_NAME);
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const worker = new Worker(
XHS_QUEUE_NAME,
async (job) => {
const startTime = Date.now();
console.log('job', job.name, job.data);
await sleep(1000);
const endTime = Date.now();
const duration = endTime - startTime;
return {
startTime: startTime,
endTime: endTime,
duration: duration,
};
},
{ connection: redis, concurrency: 1 },
);
worker.on('completed', async (job) => {
console.log('job completed', job.name, job.id, job.returnvalue);
const duration = job.returnvalue.duration || 0;
const maxNextTime = 20 * 1000; // 5 minutes
const nextTime = clamp(maxNextTime - duration, 0, maxNextTime);
const hasJobs = await queue.getJobCounts('waiting', 'wait', 'delayed');
console.log('hasJobs', nextTime, 'joblenght', hasJobs);
if (hasJobs.delayed + hasJobs.wait > 0) {
console.log('======has jobs, no need to add new job');
} else {
const id = nanoid();
queue.add(
'repeact-call-job' + id,
{},
{
delay: nextTime,
removeOnComplete: true,
removeOnFail: {
age: 24 * 3600, // keep up to 24 hours
},
jobId: id,
},
);
}
});