remove mark
This commit is contained in:
15
wxmsg/task/worker/bun.config.ts
Normal file
15
wxmsg/task/worker/bun.config.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { build } from 'bun';
|
||||
|
||||
await build({
|
||||
entrypoints: ['./index.ts'],
|
||||
outdir: './dist',
|
||||
target: 'node',
|
||||
format: 'esm',
|
||||
naming: {
|
||||
entry: 'app.js',
|
||||
},
|
||||
minify: false,
|
||||
sourcemap: false,
|
||||
});
|
||||
|
||||
console.log('✅ Build complete: dist/app.js');
|
||||
38
wxmsg/task/worker/index.ts
Normal file
38
wxmsg/task/worker/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Worker } from "bullmq";
|
||||
import { redis } from './redis.ts';
|
||||
|
||||
import { Wx } from "../../src/wx";
|
||||
|
||||
const worker = new Worker('wxmsg', async job => {
|
||||
const wx = new Wx({
|
||||
appId: process.env.WX_APPID || '',
|
||||
appSecret: process.env.WX_APPSECRET || '',
|
||||
redis: redis
|
||||
});
|
||||
if (job.name === 'analyzeUserMsg') {
|
||||
const { touser, msg } = job.data;
|
||||
const accessToken = await wx.getAccessToken();
|
||||
const sendData = {
|
||||
touser,
|
||||
msgtype: 'text',
|
||||
text: {
|
||||
content: 'Hello World' + new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
await wx.sendUserMessage(sendData);
|
||||
} else {
|
||||
throw new Error(`Unknown job name: ${job.name}`);
|
||||
}
|
||||
}, {
|
||||
connection: redis
|
||||
});
|
||||
|
||||
worker.on('completed', (job) => {
|
||||
console.log(`Job ${job.id} has completed!`);
|
||||
});
|
||||
|
||||
worker.on('failed', (job, err) => {
|
||||
console.log(`Job ${job?.id} has failed with error ${err.message}`);
|
||||
});
|
||||
|
||||
console.log('Worker is running...');
|
||||
37
wxmsg/task/worker/package.json
Normal file
37
wxmsg/task/worker/package.json
Normal file
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@kevisual/wxmsg-worker",
|
||||
"version": "0.0.2",
|
||||
"description": "",
|
||||
"main": "index.ts",
|
||||
"basename": "/root/wxmsg-worker",
|
||||
"app": {
|
||||
"type": "pm2-system-app",
|
||||
"entry": "./app.js"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "bun index.ts",
|
||||
"build": "bun run bun.config.ts",
|
||||
"prepub": "rimraf dist && rimraf pack-dist && pnpm build",
|
||||
"pub": "envision pack -p -u -c"
|
||||
},
|
||||
"keywords": [],
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@10.24.0",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@kevisual/context": "^0.0.4",
|
||||
"@kevisual/router": "0.0.33",
|
||||
"@types/node": "^24.10.1",
|
||||
"crypto-js": "^4.2.0",
|
||||
"xml2js": "^0.6.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/bun": "^1.3.3",
|
||||
"@types/crypto-js": "^4.2.2",
|
||||
"@types/xml2js": "^0.4.14"
|
||||
}
|
||||
}
|
||||
42
wxmsg/task/worker/redis.ts
Normal file
42
wxmsg/task/worker/redis.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import Redis from "ioredis";
|
||||
import { useConfig } from '@kevisual/use-config';
|
||||
import { useContextKey } from "@kevisual/context";
|
||||
|
||||
export const config = useConfig()
|
||||
|
||||
// 首先从 process.env 读取环境变量
|
||||
const redisConfig = {
|
||||
host: process.env.REDIS_HOST || 'kevisual.cn',
|
||||
port: parseInt(process.env.REDIS_PORT || '6379'),
|
||||
password: process.env.REDIS_PASSWORD,
|
||||
};
|
||||
|
||||
export const createRedisClient = (options = {}) => {
|
||||
const redis = new Redis({
|
||||
// host: 'localhost', // Redis 服务器的主机名或 IP 地址
|
||||
// port: 6379, // Redis 服务器的端口号
|
||||
// password: 'your_password', // Redis 的密码 (如果有)
|
||||
db: 0, // 要使用的 Redis 数据库索引 (0-15)
|
||||
keyPrefix: '', // key 前缀
|
||||
retryStrategy(times) {
|
||||
// 连接重试策略
|
||||
return Math.min(times * 50, 2000); // 每次重试时延迟增加
|
||||
},
|
||||
maxRetriesPerRequest: null, // 允许请求重试的次数 (如果需要无限次重试)
|
||||
...options,
|
||||
});
|
||||
// 监听连接事件
|
||||
redis.on('connect', () => {
|
||||
console.log('Redis 连接成功');
|
||||
});
|
||||
|
||||
redis.on('error', (err) => {
|
||||
console.error('Redis 连接错误', err);
|
||||
});
|
||||
redis.on('ready', () => {
|
||||
console.log('Redis 已准备好处理请求');
|
||||
});
|
||||
return redis;
|
||||
};
|
||||
const redis = useContextKey('redis', createRedisClient(redisConfig));
|
||||
export { redis };
|
||||
Reference in New Issue
Block a user