hexindai / egg-bus Goto Github PK
View Code? Open in Web Editor NEW🐣 用 egg 编写优雅的队列与事件
License: MIT License
🐣 用 egg 编写优雅的队列与事件
License: MIT License
报错:BRPOPLPUSH ReplyError: ERR 'BRPOPLPUSH' command keys must in same slot
node_modules/_redis-parser@3.0.0@redis-parser/lib/parser.js:302:14) {
command: {
name: 'brpoplpush',
args: [ 'bull:default:wait', 'bull:default:active', '5' ]
}
}
原因是集群版lua脚本的使用并不同于单机版,为了保持事务,同一个lua脚本访问应该访问同一个slot,但是redis集群会根据KEY进行hash并取模,因此如果采用默认hash的话,那么就会产生这个错误,解决方案是在key也就是queue的名字上加上{}。
但是在
Line 20 in fc3e661
简单来说只要这儿按照config里传的default queue name命名default queue,或者default queue的名字上加上{},问题就能解决
Hello,
I have a problem, it's my sample code below:
async run(data, job) {
try {
await this.app.mysql.get('xxx'); // this will return a promise
let eth_transaciton = await web3.eth.sendSignedTransaction(row_data); // can't get the promise from this function, and catch doesn't work
console.log('xxx'); // this doesn't work
} catch {
log('err');
}
}
I can't get the promise from the "web3.eth.sendSignedTransaction" function, but other function work well, do you know what's going on?
thanks a lot :)
Hello,
I'd like to know how to create a job, could you give me an example?
thank you.
例如:因为数据库短时不可用的原因导致job多次重试不可用, 现在修复好数据库后。然后怎样去重试这些失败后的job。有什么好的建议吗?你们是怎么处理类似的问题的?
感谢大佬!!!
I was not able to successfully load the queue config.
Then I found the reason is that the internal config load code will add prefix
defined in the config to the queue name, then egg-bus
will not be able to locate the correct config.
Example:
I have a job file inside job folder
// /app/job/schedule.ts
import { Job } from 'egg-bus';
export default class DemoJob extends Job {
static get queue() {
return 'schedule'; // 使用的队列名称
}
static get attempts() {
return 5; // 重试次数
}
async run(data, job) {
const { ctx } = this;
ctx.logger.info(`Receive data: ${data}, job: ${job}`);
}
failed(data) {
const { ctx } = this;
ctx.logger.info(`Failed data: ${data}`);
}
}
Here is the config file
config.bus = {
debug: true, // Debug 模式下会打印更多日志信息
concurrency: 1, // Bull 中队列处理的并发数:https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueprocess
listener: {
baseDir: 'listener',
options: { // Bull Job 配置: https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueadd
attempts: 5,
backoff: {
delay: 3000,
type: 'fixed',
},
},
},
job: {
options: { // Bull Job 配置: https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueadd
attempts: 5,
backoff: {
delay: 3000,
type: 'fixed',
},
},
},
bull: { // Bull 队列配置:https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queue
redis: {
host: 'localhost',
port: 6379,
db: 0,
},
},
queues: {
schedule: {
limiter: {
max: 1,
duration: 1000,
},
},
},
};
Then the config won't work, there is no rateLimit for the queue.
I dig into the code a little, and print out the queue name when loading the config file, then I found it was bus:schedule
, after I change the queue name in queues
to bus:schedule
, everything works fine. So I think it would be better to fix this, since this is not quite clear that the config needs to include the prefix
.
我这里有个执行时间比较长的 job,希望能监听到 bull 的 queue 的 completed 事件,以便在某个 job 完成后再执行下一个 job。请问根据目前的版本有办法实现嘛?
[egg-bus] bull error: ReplyError: NOAUTH Authentication required.
然而我找遍这个项目demo,居然没有任何一处使用密码。
bull: { // Bull 队列配置:https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queue redis: { port: 6379, // Redis port host: '127.0.0.1', // Redis host db: 1, password: 'Super123456', }, },
我参考了bull文档添加了密码,依然报错。
为了证明我的redis环境没有问题,我在同一个项目使用了egg-redis 是可以正常连接并使用的。
我看数据还在redis里,而且没有设置过期时间。多久数据会删除掉?
大佬,请教一下,怎么能实现,队列任务执行的过程中不阻塞请求, 就是相当于任务变成后台任务
考虑一种情况,某任务分为几个步骤,未完整执行完的情况下服务重启了
如何从特定步骤开始重新执行(重试)?
如果只能在worker(app.js)中使用bus对象,在多进程部署情况下还将考虑加锁以避免多worker同时执行.
目前只能通过定时任务 设置type为worker 确保某单一worker执行重试,不过为何要引入定时任务呢.
如果能在agent上发起重试步骤或许可以解决这个问题.
发起了一个pr https://github.com/hexindai/egg-bus/pull/31
请看看是否可以合并进入
rt
类似如下设置,跳过queue.process
queues: {
default: {
push_only: false // 便于拆分生产、消费节点
}
}
除了调用方式和目录不一样,Job 和 Listener 的区别是什么?
什么场景下适合使用 Job,什么场景下适合使用 Listener?
最佳实践是什么?
rt
job文件
class BaseQueue extends Job {
static get queue() {
return 'accessTokenSchedule'; // 使用的队列名称
}
async run(info) {
try {
const queue = await this.app.bus.get('accessTokenSchedule');
console.log('queue->>', queue);
} catch (error) {
console.log(error);
}
}
}
触发
app.bus.dispatch(
'accessTokenSchedule',
{}
// {
// repeat: {
// every: 10000,
// limit: 10,
// },
// }
);
queue为undefined
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.