Commit 86856f1d authored by ml's avatar ml

修改 增加 systemTrigger 定时任务

parent 6a569627
'use strict';
import { ormDB, coinType, systemTrigger } from "@madex/ex-ts-dao";
let { redisUtilsCommon } = require('@madex/ex-js-common')
const TRIGGER_TYPE_COIN = 1;
const TRIGGER_TYPE_EX_PAIR = 2;
const TRIGGER_STATUS_CANCEL = -1;
const TRIGGER_STATUS_UN_TRIGGER = 0;
const TRIGGER_STATUS_SUCCESS = 1;
const TRIGGER_STATUS_FAIL = 2;
const TRIGGER_ACTION_COIN_TYPE = {
dis_active: 1010,
active: 1011,
unable_deposit: 1020,
enable_deposit: 1021,
unable_withdraw: 1030,
enable_withdraw: 1031,
unable_transfer: 1040,
enable_transfer: 1041,
};
const TRIGGER_ACTION_PAIR = {
dis_active: 2010,
active: 2011,
no_hide: 2020,
hide: 2021,
}
const TRIGGER_ACTION_PAIR_ACTIVE_DIS = 2010;
const TRIGGER_ACTION_PAIR_ACTIVE = 2011;
const TRIGGER_ACTION_PAIR_HIDE_DIS = 2020;
const TRIGGER_ACTION_PAIR_HIDE = 2021;
const KEY_PAIR_LIST = 'active_pairList';
export const getCoinTypeUnTriggers = async function () {
let unTriggeredList: any = [];
try {
unTriggeredList = await systemTrigger.prototype.findAll({
where: {
trigger_type: TRIGGER_TYPE_COIN,
status: TRIGGER_STATUS_UN_TRIGGER,
},
raw: true,
});
}
catch (e) {
console.log('get coin_type unTrigger items fail:', e);
}
return unTriggeredList;
}
export const getExPairUnTriggers = async function () {
let unTriggeredList: any = [];
try {
unTriggeredList = await systemTrigger.prototype.findAll({
where: {
trigger_type: TRIGGER_TYPE_EX_PAIR,
status: TRIGGER_STATUS_UN_TRIGGER,
},
raw: true,
});
}
catch (e) {
console.log('get ex_pair unTrigger items fail:', e);
}
return unTriggeredList;
}
export const doExPairTrigger = async function (trigId: any, ex_pair: any, action: any) {
let rtn:any = false;
try {
//TODO:目前交易对 没有激活 隐藏相关字段了 之后有的话 再补充
//let pair = await exPair.prototype.findOne({ where: { pair: ex_pair }, raw: true });
let pair = null;
if (!pair) {
//交易对不存在,标记触发失败
await systemTrigger.prototype.update({ status: TRIGGER_STATUS_FAIL }, { where: { id: trigId, status: 0 } });
rtn = false;
}
else {
let target_value = {};
switch (action) {
case TRIGGER_ACTION_PAIR.dis_active:
target_value = { is_active: 0 };
break;
case TRIGGER_ACTION_PAIR.active:
target_value = { is_active: 1 };
break;
case TRIGGER_ACTION_PAIR.no_hide:
target_value = { is_hide: 0 };
break;
case TRIGGER_ACTION_PAIR.hide:
target_value = { is_hide: 1 };
break
default:
await systemTrigger.prototype.update({ status: TRIGGER_STATUS_FAIL }, { where: { id: trigId, status: 0 } });
rtn = false;
return rtn;
}
//操作ex_pair 和 system_trigger
let t;
try {
console.log('start transaction');
t = await ormDB.transaction();
//TODO: 目前交易对 没有激活 隐藏相关字段了 之后有的话 再补充
//await exPair.prototype.update(target_value, { where: { pair: ex_pair }, transaction: t });
await systemTrigger.prototype.update({ status: TRIGGER_STATUS_SUCCESS }, { where: { id: trigId, status: 0 }, transaction: t });
await t.commit();
rtn = true;
}
catch (e) {
console.log('transaction fail', e);
if (t) {
t.rollback();
}
rtn = false;
}
}
}
catch (e) {
console.error('doExPairTrigger:', e);
}
return rtn;
}
export const doCoinTypeTrigger = async function (trigId: any, coinSymbol: any, action: any) {
let rtn = false;
try {
let symbol = await coinType.prototype.findOne({ where: { symbol: coinSymbol }, raw: true });
if (!symbol) {
//币种不存在
await systemTrigger.prototype.update({ status: TRIGGER_STATUS_FAIL }, { where: { id: trigId, status: 0 } });
rtn = false;
}
else {
let target_value = {};
switch (action) {
case TRIGGER_ACTION_COIN_TYPE.dis_active:
target_value = { is_active: 0 };
break;
case TRIGGER_ACTION_COIN_TYPE.active:
target_value = { is_active: 1 };
break;
case TRIGGER_ACTION_COIN_TYPE.unable_deposit:
target_value = { enable_deposit: 0 };
break;
case TRIGGER_ACTION_COIN_TYPE.enable_deposit:
target_value = { enable_deposit: 1 };
break;
case TRIGGER_ACTION_COIN_TYPE.unable_withdraw:
target_value = { enable_withdraw: 0 };
break;
case TRIGGER_ACTION_COIN_TYPE.enable_withdraw:
target_value = { enable_withdraw: 1 };
break;
case TRIGGER_ACTION_COIN_TYPE.unable_transfer:
target_value = { enable_transfer: 0 };
break;
case TRIGGER_ACTION_COIN_TYPE.enable_transfer:
target_value = { enable_transfer: 1 };
break;
}
//操作ex_pair 和 system_trigger
let t;
try {
console.log('start transaction');
t = await ormDB.transaction();
await coinType.prototype.update(target_value, { where: { symbol: coinSymbol }, transaction: t });
await systemTrigger.prototype.update({ status: TRIGGER_STATUS_SUCCESS }, { where: { id: trigId, status: 0 }, transaction: t });
await t.commit();
rtn = true;
}
catch (e) {
console.log('transaction fail', e);
if (t) {
t.rollback();
}
rtn = false;
}
}
}
catch (e) {
console.error('doCoinTypeTrigger fail:', e);
}
return rtn;
}
/**
* 更新redis active_pairList
*/
export const updateRedisPairs = async function () {
console.log('updateRedisPairs');
try {
//TODO:没有了
//let list = await exPair.prototype.findAll({ where: { is_active: 1 } });
// redisUtilsCommon.writeSync(KEY_PAIR_LIST, JSON.stringify(list));
}
catch (e) {
console.error(e);
}
}
'use strict';
const schedule = require('node-schedule');
const config = require('../../config');
import * as triggerSvr from "../service/task.system.trigger.service";
const AHEAD_TIME_ALLOWED = 2000; //允许提前时间
import * as RobotUtil from "../../src/utils/robotUtils";
/**
* 每5秒监控一次交易对设置变化
*/
let jobTrigger = function () {
schedule.scheduleJob('*/2 * * * * *', async function () {
try {
let triggerList = await triggerSvr.getExPairUnTriggers();
let nowTime = new Date();
console.log('jobExPairTrigger trigger at ', nowTime);
let hasTriggered: any = false;
for (let item of triggerList) {
if (new Date(item.trigger_time).getTime() - AHEAD_TIME_ALLOWED <= nowTime.getTime()) {
let rtn = await triggerSvr.doExPairTrigger(item.id, item.trigger_symbol, item.trigger_action);
hasTriggered |= rtn;
RobotUtil.sendRobotMessage(RobotUtil.ROBOT_KEYS.COMMON_KEY,
`${config.env} 触发交易对更新-${item.trigger_symbol}:${item.trigger_action}--${rtn}`);
}
}
//更新redis交易对信息
if (hasTriggered) {
triggerSvr.updateRedisPairs();
}
}
catch (e) {
console.error('jobExPairTrigger fail:', e);
}
});
/**
* 币种表触发 每分钟触发
*/
schedule.scheduleJob('*/1 * * * *', async function () {
try {
let triggerList = await triggerSvr.getCoinTypeUnTriggers();
let nowTime = new Date();
console.log('jobExPairTrigger trigger at ', nowTime);
for (let item of triggerList) {
if (new Date(item.trigger_time).getTime() - AHEAD_TIME_ALLOWED <= nowTime.getTime()) {
let rtn = await triggerSvr.doCoinTypeTrigger(item.id, item.trigger_symbol, item.trigger_action);
RobotUtil.sendRobotMessage(RobotUtil.ROBOT_KEYS.COMMON_KEY,
`${config.env} 触发币种更新-${item.trigger_symbol}:${item.trigger_action}--${rtn}`);
}
}
}
catch (e) {
console.error('jobExPairTrigger fail:', e);
}
});
}
jobTrigger();
\ No newline at end of file
......@@ -38,4 +38,9 @@ export const APP_SPECIAL_MODEL_REDIS_KEY = "b029.app.sepecial.model.redis.key";
*/
export const REWARD_TIME_PERIOD_REDIS_KEY = "bk_reward_time_period_config";
/**
* Madex 用户的SessionId集合
*/
export const M_SESSION_ID_PREFIX = "session_id.list."
......@@ -14,6 +14,7 @@ import { tradeAsset2USDTByUids, walletAsset2USDTByUids } from "./mUserAssets.ser
import BigNumber from "bignumber.js";
import { getCoinWithdrawMapByUids } from "./coinWithdraw.service";
import { getCoinAddressMapByUids } from "./coinAddress.service";
import { M_SESSION_ID_PREFIX } from "../../../constant/redis-val";
let _ = require('lodash');
let { logger, datetimeUtils } = require('@madex/ex-js-public');
......@@ -182,7 +183,7 @@ export async function updateUserEmail(currentUserId: number, m_user_id: any, ema
await modifyUserEmail(m_user_id, email, dbEmail);
//踢出登陆
authCommon.delSessionIdList(m_user_id);
killAllLogin(m_user_id);
//新邮箱发邮件
sendEmail(email, m_user_id, EMAIL_FORBID_TEMPLATE);
//旧邮箱发送邮件
......@@ -211,7 +212,7 @@ export async function lockAccount(currentUserId: number, m_user_id: any, ip: any
if (lock_type == 1) {
await updateMUserInfo(m_user_id, { is_lock: 1 });
//踢出登陆
authCommon.delSessionIdList(m_user_id);
killAllLogin(m_user_id);
comment = `ip:${ip},锁定用户:${fatherUserId}`;
}
else {
......@@ -222,23 +223,23 @@ export async function lockAccount(currentUserId: number, m_user_id: any, ip: any
throw ErrorCode.DAD_ACCOUNT_NOT_EXIST;
}
fatherUserId = temp;
let uids = await getUidsByFatherUid(fatherUserId);
uids.push(fatherUserId);
//更新主账户及其子账户
await userInfo.prototype.update({
is_lock: 1,
updatedAt: new Date()
}, {
where: {
user_id: { [ormDB.Op.in]: uids }
}
});
//踢出登陆
for (let uid of uids) {
authCommon.delSessionIdList(uid);
}
let uids = await getUidsByFatherUid(fatherUserId);
uids.push(fatherUserId);
//更新主账户及其子账户
await userInfo.prototype.update({
is_lock: 1,
updatedAt: new Date()
}, {
where: {
user_id: { [ormDB.Op.in]: uids }
}
comment = `ip:${ip},锁定用户:${uids}`;
});
//踢出登陆
for (let uid of uids) {
killAllLogin(uid);
}
comment = `ip:${ip},锁定用户:${uids}`;
}
//管理后台操作日志
......@@ -282,21 +283,21 @@ export async function unlockAccount(currentUserId: number, m_user_id: any, ip: a
throw ErrorCode.DAD_ACCOUNT_NOT_EXIST;
}
fatherUserId = temp;
let uids = await getUidsByFatherUid(fatherUserId);
uids.push(fatherUserId);
//更新主账户及其子账户
await userInfo.prototype.update({
is_lock: 0,
login_error_times: 0,
updatedAt: new Date()
}, {
where: {
user_id: { [ormDB.Op.in]: uids }
}
});
comment = `ip:${ip},解锁用户:${uids}`;
}
let uids = await getUidsByFatherUid(fatherUserId);
uids.push(fatherUserId);
//更新主账户及其子账户
await userInfo.prototype.update({
is_lock: 0,
login_error_times: 0,
updatedAt: new Date()
}, {
where: {
user_id: { [ormDB.Op.in]: uids }
}
});
comment = `ip:${ip},解锁用户:${uids}`;
}
//管理后台操作日志
......@@ -511,8 +512,8 @@ async function delLoginLimit(user_id: number) {
redisUtilsCommon.delSync(totpCodeErrKey);
redisUtilsCommon.delSync(verifyCodeErrKey);
redisUtilsCommon.writeSync(totpCodeErrTimesKey,0);
redisUtilsCommon.writeSync(verifyCodeErrTimesKey,0);
redisUtilsCommon.writeSync(totpCodeErrTimesKey, 0);
redisUtilsCommon.writeSync(verifyCodeErrTimesKey, 0);
}
......@@ -539,6 +540,17 @@ function getLoginLimitKeys(user_id: number) {
return [totpCodeErrKey, verifyCodeErrKey, totpCodeErrTimesKey, verifyCodeErrTimesKey, loginPwdErrKey];
}
async function killAllLogin(user_id: number) {
let key = M_SESSION_ID_PREFIX + user_id;
let sessionList = await redisUtilsCommon.getSync(key);
if (sessionList && sessionList.length){
sessionList.push(key);
for (let item of sessionList) {
await redisUtilsCommon.delSync(item);
}
}
}
......
......@@ -393,11 +393,6 @@ export async function review(id: any, currentUser: any, ip: string | undefined)
//检查交易对是否存在
await checkPairExist(pair, type);
//TODO:新撮合是否需要此操作 初始化
/*String errStr = spotRpcService.initPair1(dbpair);
if (errStr != null) {
return ResponseResult.failure("admin rpc fail: " + errStr);
}*/
reason = "发布交易对到撮合完成";
await updateApply(Number(id), PAIR_APPLY_STATUS.ADMIN_ISSUE_PAIR, currentUser.account, reason, pair);
}
......@@ -410,6 +405,7 @@ export async function review(id: any, currentUser: any, ip: string | undefined)
throw ErrorCode.ACTIVE_TM_EXPIRE;
}
//TODO:需要确认 trigger_type 等字段
/*
await systemTrigger.prototype.create({
trigger_symbol: pair,
trigger_type: 2,
......@@ -419,6 +415,7 @@ export async function review(id: any, currentUser: any, ip: string | undefined)
createdAt: new Date(),
updatedAt: new Date(),
});
*/
reason = "交易对激活定时器完成";
await updateApply(Number(id), PAIR_APPLY_STATUS.TIMER_ACTIVE, currentUser.account, reason, pair);
}
......
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