owhat-job
Version:
owhat红包清理
124 lines (123 loc) • 5.2 kB
JavaScript
//console.log = function() {};
import V from 'gcl';
import iconv from 'iconv-lite';
import pro from 'myprocesscontroller';
pro.start(async function() {
const conf = V.AppSettings(this.Config, "sms");
if (!conf) throw new Error('配置获取失败!');
const count = parseInt(conf.count || 20000);
const max = parseInt(conf.max || 2000);
const threadcount = conf.threadcount || 2;
let count1 = {};
await this.Ni.excute('add.clearSMS', {});
this.Log.release('数据库初始化完成');
console.log('数据库初始化完成');
const that = this;
that.Temp = this.Middler.getObjectByAppName('Ni', 'MT');
return new pro.CACProcess(new class extends pro.ADataWorker {
constructor() {
super('wk', threadcount, max);
}
async getData(data) {
console.log('获取数据');
try {
// console.log("JDData", partner)
// const ret = await that.Temp.excute('es.getNSCData', { count: count });
const ret = await that.Temp.excute('add.getSMSData', {
count: count
});
// if (ret.hasData()) {
// console.log(ret.last())
// }
if (ret.hasData() && ret.last()[0].length && !ret.last()[0][0].affected) {
// if (ret.hasData() && ret.last()[0].length && !ret.last()[0].affected) {
that.Log.release(`获取${ret.last()[0].length}条数据`);
count1[ret.last()[1][0].num] = {
count: ret.last()[0].length,
now: 0,
sms: 0
};
console.log('create', `获取${ret.last()[0].length}条数据`);
return ret.last()[0];
} else if (ret.last().length > 1) {
V.watch();
console.log('没有返回数据!', ret.last()[1][0]);
} else {
console.log('奇怪错误 未返回返回数据 也没有此次请求的num');
// (await that.Ni.excute('es.clearNSC', {}));
(await that.Ni.excute('add.clearSMS', {}));
}
} catch (e) {
that.Log.error(e.message);
console.log(e.stack);
return null;
}
}
async custom(ret2, data) {
if (!data.array) data.array = 1;
else data.array++;
if (!data.Log) {
data.Log = that.Log;
data.Ni = that.Ni;
data.Config = that.Config;
}
const ret = ret2;
try {
//String softwareSerialNo, String key, String sendTime, String[] mobiles, String smsContent, String addSerial, String srcCharset, int smsPriority,long smsID
const result = await data.Ni.excute('add.sendSMS', {
arg0: conf.serialno,
arg1: conf.key,
arg2: null,
arg3: [ret.Mobile],
arg4: ret.Content,
arg5: '',
arg6: 'GBK',
arg7: 5,
arg8: 0
});
const smsback = result.last()[0] && result.last()[0][0] ? result.last()[0][0].return : -5201314;
//因为return为0 所以single无值
console.log(80, ret, result.last(), smsback);
if (smsback == 0) {
await data.Ni.excute('add.finishSMSData', {
ID: ret.ID,
ret: smsback
});
console.log('单条发送成功', ret, smsback);
} else throw new Error('发送失败!ret:' + smsback);
result.clear();
} catch (e) {
console.log("单条发送失败" + e.message, V.toJsonString(ret));
that.Log.error("单条发送失败" + e.message + ' ' + V.toJsonString(count1) + " " + V.toJsonString(ret));
await data.Ni.excute('add.failSMSData', {
ID: ret.ID,
ret: 1
});
} finally {
if (count1[ret.num] && ++count1[ret.num].now > count1[ret.num].count) {
console.log(ret.num + '全部发送完毕!');
delete count1[ret.num];
}
}
return false;
}
async rollback(v, data) {
console.log('rollback 抛弃数据', v);
that.Log.error("单条发送放弃" + ' ' + V.toJsonString(v));
return false;
}
async dispose(data) {
try {
await super.dispose(data);
} catch (e) {
console.log('dispose', e.message);
} finally {
//todo 关闭特有资源
delete data.Log;
delete data.Ni;
delete data.Config;
}
return false;
}
}, this.Log, threadcount, 10000);
});