基于Vue2和Node.js的反欺诈系统设计与实现 (9)

原系统是接入了第三方的数据源去定时读取更新数据,再将数据清洗更新到我们自己的t_alarm表,一些原因这里我不方便做演示,所以笔者又新建了一张天气表,来向大家介绍eggjs中的定时任务。

在这里,我相中了万年历的接口,准备嫖一嫖给大家做一个演示的例子,它返回的数据格式如下

{ "data": { "yesterday": { "date": "19日星期四", "high": "高温 33℃", "fx": "东风", "low": "低温 24℃", "fl": "<![CDATA[1级]]>", "type": "小雨" }, "city": "杭州", "forecast": [ { "date": "20日星期五", "high": "高温 34℃", "fengli": "<![CDATA[2级]]>", "low": "低温 25℃", "fengxiang": "西南风", "type": "小雨" }, { "date": "21日星期六", "high": "高温 33℃", "fengli": "<![CDATA[2级]]>", "low": "低温 25℃", "fengxiang": "西南风", "type": "中雨" }, { "date": "22日星期天", "high": "高温 33℃", "fengli": "<![CDATA[1级]]>", "low": "低温 26℃", "fengxiang": "东风", "type": "小雨" }, { "date": "23日星期一", "high": "高温 32℃", "fengli": "<![CDATA[1级]]>", "low": "低温 26℃", "fengxiang": "南风", "type": "中雨" }, { "date": "24日星期二", "high": "高温 33℃", "fengli": "<![CDATA[1级]]>", "low": "低温 25℃", "fengxiang": "西南风", "type": "小雨" } ], "ganmao": "感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。", "wendu": "31" }, "status": 1000, "desc": "OK" }

我分别选取了天朝的一线城市和一些地域性比较强的城市去搞数据(等跑个两三个月,存了点数据,俺又可以写一篇基于echarts的天气可视化展示了,233333333),最后的效果如图

基于Vue2和Node.js的反欺诈系统设计与实现

首先我们创建一个类,继承了egg的Subscription类, 然后有一个schedule方法

static get schedule() { return { interval: '12h', type: 'worker', }; }

interval表示时间间隔,从楼上可以看出是每12小时去执行一次,type表示执行这个定时任务的进程,可以选all和worker,这边表示只在一个worker进程中执行该任务。

核心的业务逻辑,写在subscribe方法中,这里表示去请求万年历的数据,然后进行相应的数据清洗

async subscribe() { try { const result = []; for (const city of CITYS) { result.push(this.fetchData(city)); } await Promise.all(result); } catch (e) { this.ctx.app.logger.error(e); } }

最终实现代码如下:

const { Subscription } = require('egg'); const URL_PREFIX = 'http://wthrcdn.etouch.cn/weather_mini?city='; const CITYS = [ '杭州', '北京', '南京', '上海', '广州', '深圳', '成都', '武汉', '郑州', '哈尔滨', '海口', '三亚', '乌鲁木齐', '呼和浩特', '拉萨', '大理', '丽江', ]; const DAY_TIMESTAMP = 86400000; class WeatherSchedule extends Subscription { static get schedule() { return { interval: '12h', type: 'worker', }; } async refreshWeatherData( date, high, low, wendu = null, fengli, fengxiang, type, ganmao = null, city, weatherDate ) { const weather = await this.service.weather.getWeather(weatherDate, city); if (weather) { const { id, wendu: oldWendu, ganmao: oldGanmao } = weather; const newWendu = oldWendu || wendu; const newGanmao = oldGanmao || ganmao; await this.service.weather.set( id, date, high, low, newWendu, fengli, fengxiang, type, newGanmao, city, weatherDate ); } else { await this.service.weather.add( date, high, low, wendu, fengli, fengxiang, type, ganmao, city, weatherDate ); } } async fetchData(queryCity) { const res = await this.ctx.curl(`${URL_PREFIX}${queryCity}`, { dataType: 'json', }); const { data: { city, forecast = [], ganmao, wendu }, } = res.data; const result = []; const now = this.ctx.app.utils.date.now() * 1000; for (let i = 0; i < forecast.length; i++) { const { date, high, fengli, low, fengxiang, type } = forecast[i]; const weatherDate = this.ctx.app.utils.date.format2Date( now + i * DAY_TIMESTAMP ); if (i === 0) { result.push( this.refreshWeatherData( date, high, low, wendu, fengli, fengxiang, type, ganmao, city, weatherDate ) ); } else { result.push( this.refreshWeatherData( date, high, low, null, fengli, fengxiang, type, null, city, weatherDate ) ); } } await Promise.all(result); } async subscribe() { try { const result = []; for (const city of CITYS) { result.push(this.fetchData(city)); } await Promise.all(result); } catch (e) { this.ctx.app.logger.error(e); } } } module.exports = WeatherSchedule;

egg中的schedule: https://eggjs.org/zh-cn/basics/schedule.html

eggjs中的配置项config

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfxzj.html