39 lines
926 B
TypeScript
39 lines
926 B
TypeScript
import { Worker } from "bullmq";
|
|
import { redis, config } from './redis.ts';
|
|
|
|
import { Wx } from "../../src/wx";
|
|
|
|
|
|
const worker = new Worker('wxmsg', async job => {
|
|
const url = 'http://localhost:3005/api/router';
|
|
const wx = new Wx({
|
|
appId: config.WX_MP_APP_ID, appSecret: config.WX_MP_APP_SECRET,
|
|
redis: redis,
|
|
url,
|
|
});
|
|
if (job.name === 'analyzeUserMsg') {
|
|
await wx.postCenter(job.data);
|
|
} else {
|
|
throw new Error(`Unknown job name: ${job.name}`);
|
|
}
|
|
}, {
|
|
connection: redis,
|
|
removeOnComplete: {
|
|
age: 3600, // 1 hour
|
|
count: 5000, // keep last 5000 jobs
|
|
},
|
|
removeOnFail: {
|
|
age: 7200, // 2 hours
|
|
count: 5000, // keep last 5000 jobs
|
|
},
|
|
});
|
|
|
|
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...'); |