数据库插件
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
path:"app/*/api/*.js"
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* 加载api路由
|
||||
* 加载app/api目录下的文件到路由
|
||||
*/
|
||||
const config = require("./config")
|
||||
module.exports = async (app) => {
|
||||
let list = await app.load(config.path)
|
||||
list.forEach(item => {
|
||||
app.use(async (ctx, next) => {
|
||||
return await item.res.fun(ctx, next)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
path:"app/*/schedule/*.js",
|
||||
bullmq:{
|
||||
port: 6379, // Redis port
|
||||
host: "192.168.1.26", // Redis host
|
||||
// username: "", // needs Redis >= 6
|
||||
password: "9715115286",
|
||||
db: 0, // Defaults to 0
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 分布式定时任务
|
||||
* 加载schedule文件夹下的任务
|
||||
* https://blog.pincman.com/archives/18/
|
||||
* https://docs.bullmq.io
|
||||
*
|
||||
*/
|
||||
const config = require("./config")
|
||||
// const schedule = require("./schedule")
|
||||
const mq = require("./queue")
|
||||
module.exports = async (app) => {
|
||||
app.mq = mq
|
||||
|
||||
|
||||
const queueName = "boobam_schedule"
|
||||
const queue = mq.Queue(queueName);
|
||||
mq.QueueScheduler(queueName);//处理延时任务
|
||||
await queue.obliterate();//清空所有定时任务
|
||||
const list = await app.load(config.path)
|
||||
//等待所有插件载入完成后,启动Worker
|
||||
app.willReadyList.push(async () => {
|
||||
for (let el of list) {
|
||||
mq.Worker(queueName, async job => {
|
||||
if (job.name===el.parse.name){
|
||||
el.res.fun(app)
|
||||
}
|
||||
});
|
||||
await queue.add(el.parse.name,{},{
|
||||
repeat: el.res.time,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
const {Queue, QueueScheduler, Worker,QueueEvents} = require("bullmq")
|
||||
const config = require("./config")
|
||||
module.exports = {
|
||||
Queue: (name) => new Queue(name, {connection: config.bullmq}),
|
||||
QueueScheduler: (name) => new QueueScheduler(name, {connection: config.bullmq}),
|
||||
QueueEvents: (name) => new QueueEvents(name, {connection: config.bullmq}),
|
||||
Worker: (name, ops) => new Worker(name, ops, {connection: config.bullmq}),
|
||||
Job: (ops) => new Job(ops, {connection: config.bullmq}),
|
||||
}
|
||||
@@ -42,7 +42,7 @@ module.exports = async (app) => {
|
||||
const api = new Api(sequelize, sequelize.models)
|
||||
app.db = api
|
||||
// console.log(api);
|
||||
console.log(await app.db.table("User").find());
|
||||
// console.log(await app.db.table("User").find());
|
||||
|
||||
//等待所有插件载入完成后,自动更新数据库字段
|
||||
//把所有模型的md5值存入到redis里面去,哪个文件的md5有变动就更新哪个表的字段.
|
||||
|
||||
@@ -10,5 +10,6 @@ const Redis = require("ioredis");
|
||||
module.exports = async (app) => {
|
||||
const redis = new Redis(config);
|
||||
app.redis = redis
|
||||
app.Redis = Redis
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,13 @@ module.exports = async (app) => {
|
||||
app.io = io
|
||||
//默认命名空间
|
||||
io.on('connection', socket => {
|
||||
socket.emit('open', "id:"+socket.id+"连接成功!");//通知客户端已连接
|
||||
//通知客户端已连接
|
||||
socket.emit('open', {
|
||||
msg:"ok",
|
||||
data:{
|
||||
id:socket.id
|
||||
}
|
||||
});
|
||||
console.log('连接=>',"id:"+socket.id);
|
||||
//监听disconnect事件
|
||||
socket.on('disconnect', (eventName, callback) => {
|
||||
|
||||
Reference in New Issue
Block a user