Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

Commit

Permalink
fix: 修改部分fecs报错
Browse files Browse the repository at this point in the history
  • Loading branch information
xuexb committed Mar 28, 2016
1 parent 7b5240b commit 60e8536
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 140 deletions.
3 changes: 1 addition & 2 deletions src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import {resolve} from 'path';
import KeyCache from 'key-cache';

let cache = new KeyCache({
export default new KeyCache({
dir: resolve(__dirname, '../cache/'),
md5key: false
});

export default cache;
255 changes: 129 additions & 126 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,151 +33,154 @@ import config from '../config.json';
*
* @return {Promise} 请求数据的Promise
*/
let Check = (options = {}) => {
// 如果没有需要监听
if (!options.watch) {
return Check._exec(options);
}

// 先执行下,再绑定事件
return Check._exec(options).then(data => {
// 创建定时任务
let rule = new RecurrenceRule();
export default class Check {

/**
* 获取数据
*
* @param {Object} options 配置参数
*
* @return {Promise} 请求参数Promise
*/
static getData = (options = {}) => {
// 合并参数
options = {
...config, ...options
};

// 如果是每天检查则设置每天的00:00检查,否则设置为每小时的0分检查
if (options.time === 'day') {
rule.hour = 0;
// 如果规则不存在则报错
if (!options.rule || !options.rule.length) {
return Promise.reject('config.rule is empty');
}
else {
rule.minute = 0;
}

// 执行定时任务
scheduleJob(rule, () => Check._exec(options));

// 返回数据
return data;
});
};
// 所有请求的promise,并列执行,后续优化成串行的
let promiseAll = [];

// 上次版本号的缓存
let versionCache = cache.get('version') || {};

// 循环规则,生成Promise
options.rule.forEach(val => {
// 生成一个promise
let defer = new Promise((resolve, reject) => {
// 请求源码
request.get({
headers: options.header || {},
url: val.url
}, (err, response, body) => {
// 如果有错误
if (err) {
val.errcode = 1;
val.errmsg = err;
reject(val);
}
// 如果响应码不是200
else if (response.statusCode !== 200) {
val.errcode = 2;
val.errmsg = response.statusCode;
reject(val);
}
else {
let reg = new RegExp(val.reg);
let match = val.match || '$1';
match = Math.floor(match.replace('$', '')) || 0;

/**
* 执行
*
* @param {Object} options 配置参数
*
* @return {Promise} 请求参数Promise
*/
Check._exec = (options = {}) => {
let defer = Check.getData(options);
try {
body = body.match(reg);
if (body && body.length > match - 1) {
body = body[match];
}

return defer.then(data => sendMail(options, data)).then(data => send(options, data)).catch(err => {
error(err);
});
};
}
catch (e) {
body = null;
}

val.version = body;

/**
* 获取数据
*
* @param {Object} options 配置参数
*
* @return {Promise} 请求参数Promise
*/
Check.getData = (options = {}) => {
// 合并参数
options = {...config, ...options};
resolve(val);
}
});
});

// 如果规则不存在则报错
if (!options.rule || !options.rule.length) {
return Promise.reject('config.rule is empty');
}
promiseAll.push(defer);
});

// 所有请求的promise,并列执行,后续优化成串行的
let promiseAll = [];

// 上次版本号的缓存
let versionCache = cache.get('version') || {};

// 循环规则,生成Promise
options.rule.forEach(val => {
// 生成一个promise
let defer = new Promise((resolve, reject) => {
// 请求源码
request.get({
headers: options.header || {},
url: val.url
}, (err, response, body) => {
// 如果有错误
if (err) {
val.errcode = 1;
val.errmsg = err;
reject(val);
// 并行执行请求
// 并对数据进行处理,主要是检查版本号
return Promise.all(promiseAll).then(data => {
let res = {
update: [],
all: data
};

data.forEach(val => {
let key = val.name;

// 如果没有获取到版本
if (!val.version) {
versionCache[key] = val.version;
return;
}
// 如果响应码不是200
else if (response.statusCode !== 200) {
val.errcode = 2;
val.errmsg = response.statusCode;
reject(val);
}
else {
let reg = new RegExp(val.reg);
let match = val.match || '$1';
match = Math.floor(match.replace('$', '')) || 0;

try {
body = body.match(reg);
if (body && body.length > match - 1) {
body = body[match];
}
}
catch (e) {
body = null;
}

val.version = body;

resolve(val);
// 如果上个版本缓存过
// 如果是新加的规则则忽略,因为新加的无法与上次对比查看是否有更新
if (versionCache.hasOwnProperty(key)) {
// 如果版本号不同
// 如果上次为null,而走到这里时当前版本肯定是有的,然后这就算更新了
if (val.version !== versionCache[key] || versionCache[key] === null) {
val.prevVersion = versionCache[key];
res.update.push(val);
}
}

versionCache[key] = val.version;
});

cache.set('version', versionCache);
return res;
});
}

promiseAll.push(defer);
});
/**
* 执行
*
* @param {Object} options 配置参数
*
* @return {Promise} 请求参数Promise
*/
static _exec = (options = {}) => {
let defer = Check.getData(options);

return defer.then(data => sendMail(options, data)).then(data => send(options, data)).catch(err => {
error(err);
});
}

// 并行执行请求
// 并对数据进行处理,主要是检查版本号
return Promise.all(promiseAll).then(data => {
let res = {
update: [],
all: data
};
constructor(options = {}) {
// 如果没有需要监听
if (!options.watch) {
return Check._exec(options);
}

data.forEach(val => {
let key = val.name;
// 先执行下,再绑定事件
return Check._exec(options).then(data => {
// 创建定时任务
let rule = new RecurrenceRule();

// 如果没有获取到版本
if (!val.version) {
versionCache[key] = val.version;
return;
// 如果是每天检查则设置每天的00:00检查,否则设置为每小时的0分检查
if (options.time === 'day') {
rule.hour = 0;
}

// 如果上个版本缓存过
// 如果是新加的规则则忽略,因为新加的无法与上次对比查看是否有更新
if (versionCache.hasOwnProperty(key)) {
// 如果版本号不同
// 如果上次为null,而走到这里时当前版本肯定是有的,然后这就算更新了
if (val.version !== versionCache[key] || versionCache[key] === null) {
val.prevVersion = versionCache[key];
res.update.push(val);
}
else {
rule.minute = 0;
}

versionCache[key] = val.version;
});

cache.set('version', versionCache);
return res;
});
};
// 执行定时任务
scheduleJob(rule, () => Check._exec(options));

export default Check;
// 返回数据
return data;
});
}
}
12 changes: 3 additions & 9 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'colors';
*
* @param {string|Object} str 信息
*/
function error(str) {
export function error(str) {
if ('object' === typeof str) {
str = JSON.stringify(str);
}
Expand All @@ -23,7 +23,7 @@ function error(str) {
*
* @param {string|Object} str 信息
*/
function success(str) {
export function success(str) {
if ('object' === typeof str) {
str = JSON.stringify(str);
}
Expand All @@ -38,7 +38,7 @@ function success(str) {
*
* @return {Promise} Promise
*/
function send(options = {}, data = {}) {
export function send(options = {}, data = {}) {
return new Promise((resolve, reject) => {
if (data.update.length) {
success(`当前有${data.update.length}个更新.`);
Expand All @@ -65,9 +65,3 @@ function send(options = {}, data = {}) {
resolve(data);
});
}

export {
send,
success,
error
};
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author xiaowu
*/

import should from 'should';
import 'should';

import Check from '../src/index';
import types from './types';
Expand Down
4 changes: 2 additions & 2 deletions test/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author xiaowu
*/

import should from 'should';
import 'should';

import {success, error, send} from '../src/log';
import types from './types';
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('log.js', () => {
return send({}, {
update: [],
all: []
}).then((data) => {
}).then(data => {
data.update.length.should.be.equal(0);
data.all.length.should.be.equal(0);
done();
Expand Down

0 comments on commit 60e8536

Please sign in to comment.