egg-bulls
v1.1.22
Published
egg.js的Bull插件
Downloads
7
Readme
egg-bulls
安装
$ npm i egg-bulls --save
依赖说明
依赖的 egg 版本
| egg-bulls 版本 | egg 2.x | | -------------- | ------- | | 1.x | 😁 |
依赖的插件
| egg-bulls 版本 | Bull | | -------------- | ----- | | 1.x | 3.6.x |
更多 Bull.js 的信息请 点击
使用场景
- 在 egg.js 中使用 Bull.js
开启插件
// {app_root}/config/plugin.js
exports.bulls = {
enable: true,
package: "egg-bulls"
};
插件配置
单实例
// {app_root}/config/config.default.js
exports.bulls = {
client: {
topic: "queue",
queueOptions: {
redis: {
host: "localhost",
port: 6379,
db: 0,
password: "Pass1234"
}
}
}
};
多实例
// {app_root}/config/config.default.js
exports.bulls = {
clients: {
queue1: {
// topic must same with key
topic: "queue1",
queueOptions: {
redis: {
host: "localhost",
port: 6379,
db: 1,
password: "Pass1234"
}
}
},
queue2: {
// topic must same with key
topic: "queue2",
queueOptions: {
redis: {
host: "localhost",
port: 6379,
db: 2,
password: "Pass1234"
}
}
}
}
};
或者使用相同的 Redis 配置
// Use the same redis configuration
exports.bulls = {
clients: {
queue1: { topic: "queue1" },
queue2: { topic: "queue2" }
},
default: {
queueOptions: {
redis: {
host: "localhost",
port: 6379,
db: 0,
password: "Pass1234"
}
}
}
};
更多配置请查看 config/config.default.js
示例
文件挂载路径为{app_root}/app/{topic},在 app 上访问 app[topic]
单实例
// 配置
// topic 默认为 queue 时,如果使用自定义名称挂载路径变为{app_root}/app/{topic}
// {app_root}/config/config.default.js
exports.bulls = {
client: {
topic: "queue",
redis: {
host: "localhost",
port: 6379,
db: 0,
password: "Pass1234"
}
}
};
// 可以直接在 app 上使用
app.bulls.process(job => {
console.log(job.data.job1); // 'this is a job'
});
app.bulls.add({ job1: "this is a job" });
app.bulls.on('completed', (job)=>{
// do something
})
app.bulls.on('failed', (err,job)=>{
// do something
});
// 或者批量添加
// {app_root}/app.js
class AppBootHook{
constructor(app){
this.app = app
}
async didReady(){
const jobs = [{
// topic:queue
queue:'queue'
data:{
// job data
},
jobName:'job-Name',
options:{
// job options
}
}];
await this.app.addJobtToQueue(jobs)
}
}
// {app_root}/app/queue/status.js
module.exports = app => {
return {
completed(job) {
// do something
}
failed(job,error){
// do something
}
//
// use async
//
async completed(job) {
// do something
}
async failed(job,error){
// do something
}
};
};
// {app_root}/app/queue/handle.js
module.exports = app => {
return job => {
// do something
}
};
// topic 为自定义时
// {app_root}/config/config.default.js
exports.bulls = {
client: {
topic: "test",
redis: {
host: "localhost",
port: 6379,
db: 0,
password: "Pass1234"
}
}
};
// 可以直接在 app 上使用
app.bulls.process(job => {
console.log(job.data.job1); // 'this is a job'
});
app.bulls.add({ job1: "this is a job" });
app.bulls.on('completed', (job)=>{
// do something
})
app.bulls.on('failed', (err,job)=>{
// do something
});
// 或者
// {app_root}/app.js
class AppBootHook{
constructor(app){
this.app = app
}
async didReady(){
const jobs = [{
// topic:test
queue:'test'
data:{
// job data
},
jobName:'job-Name',
options:{
// job options
}
}];
await this.app.addJobtToQueue(jobs)
}
}
// {app_root}/app/test/status.js
module.exports = app => {
return {
completed(job) {
// do something
}
failed(job,error){
// do something
}
//
// use async
//
async completed(job) {
// do something
}
async failed(job,error){
// do something
}
};
};
// {app_root}/app/test/handle.js
module.exports = app => {
return job => {
// do something
}
};
多实例
// 配置
// {app_root}/config/config.default.js
exports.bulls = {
clients: {
queue1: {
topic: "queue1",
redis: {
host: "localhost",
port: 6379,
db: 1,
password: "Pass1234"
}
},
queue2: {
topic: "queue2",
redis: {
host: "localhost",
port: 6379,
db: 2,
password: "Pass1234"
}
}
}
};
// 可以直接在 app 上使用
app.bulls.get("queue1").process(job => {
console.log(job.data.job1); // 'this is a job1'
});
app.bulls.get("queue2").process(job => {
console.log(job.data.job2); // 'this is a job2'
});
app.bulls.get("queue1").add({ job1: "this is a job1" });
app.bulls.get("queue2").add({ job2: "this is a job2" });
// 或者
// {app_root}/app.js
class AppBootHook{
constructor(app){
this.app = app
}
async didReady(){
const jobs = [{
data:{
// job data
},
jobName:'job-Name1',
options:{
// job options
},
// topic:queue1
queue:'queue1'
},{
data:{
// job data
},
jobName:'job-Name2',
options:{
// job options
},
// topic: queue1
queue:'queue2'
}];
await this.app.addJobtToQueue(jobs)
}
}
// {app_root}/app/queue1/status.js
module.exports = app => {
return {
completed(job) {
// do something
}
failed(job,error){
// do something
}
//
// use async
//
async completed(job) {
// do something
}
async failed(job,error){
// do something
}
};
};
// {app_root}/app/queue2/status.js
module.exports = app => {
return {
completed(job) {
// do something
}
failed(job,error){
// do something
}
//
// use async
//
async completed(job) {
// do something
}
async failed(job,error){
// do something
}
};
};
// {app_root}/app/queue1/handle.js
module.exports = app => {
return job => {
// do something
}
};
// {app_root}/app/queue2/handle.js
module.exports = app => {
return job => {
// do something
}
};
提问交流
请到 egg-bulls issues 异步交流。