Commit 179604c1 authored by 李尚科's avatar 李尚科

add schedule

parent cd09ccf4
Pipeline #12303 passed with stage
in 21 seconds
'use strict';
const moment = require('moment');
module.exports = app => {
const { INTEGER, STRING, DATE } = app.Sequelize;
const HuayunPaasCallin = app.prometheusModel.define('huayun_paas_callin', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
session_id: STRING,
start_time: {
type: DATE,
get() {
const date = this.getDataValue('start_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
end_time: {
type: DATE,
get() {
const date = this.getDataValue('end_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
remote_url: STRING,
local_url: STRING,
skill_name: STRING,
listtime: STRING,
duretion: STRING,
end_type: STRING,
agent_id: STRING,
call_type: STRING,
r_start_time: {
type: DATE,
get() {
const date = this.getDataValue('r_start_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
r_end_time: {
type: DATE,
get() {
const date = this.getDataValue('r_end_time');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
c_ivr_key: STRING,
path: STRING,
record_nama: STRING,
created_at: {
type: DATE,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
}
}, {
timestamps: false,
tableName: 'huayun_paas_callin',
});
HuayunPaasCallin.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HuayunPaasCallin.findOne({
attributes: attributes,
where: where,
});
}
HuayunPaasCallin.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await HuayunPaasCallin.findAll({
attributes: attributes,
where: where,
});
}
HuayunPaasCallin.list = async (data = {}) => {
const limit = data.limit ? data.limit : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await HuayunPaasCallin.findAndCountAll(condition);
return { page, count, rows };
}
HuayunPaasCallin.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await HuayunPaasCallin.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
HuayunPaasCallin.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await HuayunPaasCallin.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return HuayunPaasCallin;
};
'use strict';
const Subscription = require('egg').Subscription;
const moment = require('moment');
//促申请活动给用户添加奖品
class HuaYunPaas extends Subscription {
// 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() {
return {
// cron: '0 50 5 * * *',//每天8:50点执行
cron: '*/1 * * * *',//每1分钟执行一次脚本
type: 'worker',
env: ['prod'],
immediate: false
};
}
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
const { ctx, app } = this;
if (['https://dev-nginx.jianbing.com'].includes(this.config.NODE_BASE_URL)) {
//用redis的setnx来实现锁机制
let lock = 'huayun:pass:cron:' + moment().format('MMDD');
ctx.logger.info(lock);
let redRes = await app.redis.setnx(lock, 1);
if (Number(redRes) === 1) {
await this.logic();
}
}
}
async logic() {
const { ctx } = this;
const accessToken = await this.getAccessToken();
const url = `http://221.194.132.83:7780/paas/Interface/${accessToken}/CallIn`;
const lastOne = await ctx.prometheusModel.HuayunPaasCallin.findOne({ order: [['id', 'desc']] });
const start = 0;
const limit = 10000;
let start_time = moment().add(-1, 'days').format('YYYY-MM-DD HH:mm:ss');
const now_time = moment().format('YYYY-MM-DD HH:mm:ss');
if (lastOne && lastOne.end_time) {
start_time = lastOne.end_time;
}
ctx.logger.info(JSON.stringify({ start_time, now_time, start, limit }));
const result = await ctx.curl(url, { method: 'POST', contentType: 'json', dataType: 'json', timing: true, timeout: 300000, data: { start_time, now_time, start, limit } });
const ret = result.data;
if (ret.status != 0 || !ret.message || ret.count == 0) {
return;
}
const message = ret.message;
const list = message.data;
ctx.logger.info(JSON.stringify(message.count));
const insert_data = await this.format(list);
await ctx.prometheusModel.HuayunPaasCallin.bulkCreate(insert_data, { ignoreDuplicates: true });
}
async getAccessToken() {
const { ctx } = this;
const clientId = '961a170e-f939-4189-9163-c4c39051b586';
const clientScr = '53ed566c-72b8-4ebf-b0bb-78147623be71';
const url = 'http://221.194.132.83:7780/paas/accreditation';
const result = await ctx.helper.send_request(url, { clientId, clientScr });
const ret = result.data;
if (!ret || ret.status === 1 || !ret.message) {
ctx.faild('get accessToken failed!');
}
return ret.message;
}
async format(data) {
if (!Array.isArray(data) || data.length === 0) {
return [];
}
const ret = [];
for (let i in data) {
const item = data[i];
ret.push({
session_id: item.sessionId,
start_time: item.startTime,
end_time: item.endTime,
remote_url: item.remoteUrl,
local_url: item.localUrl,
skill_name: item.skillName,
listtime: item.listtime,
duretion: item.duretion,
keysser: item.keysser,
end_type: item.end_type,
agent_id: item.agentId,
call_type: item.call_Type,
r_start_time: item.r_startTime,
r_end_time: item.r_endTime,
path: item.path,
c_ivr_key: item.cIvrKey,
record_nama: item.record_Nama,
created_at: moment().format('YYYY-MM-DD HH:mm:ss'),
});
}
return ret;
}
}
module.exports = HuaYunPaas;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment