-
Notifications
You must be signed in to change notification settings - Fork 0
/
explorer.js
64 lines (55 loc) · 1.25 KB
/
explorer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const Promise = require('bluebird');
const logger = require('./logger');
const finder = require('entipic.finder');
const Data = finder.Data;
let stoped = false;
finder.logger.set(logger);
exports.explore = function() {
stoped = false;
var startDate = new Date();
function getList() {
if (process.env.RUN_TIMEOUT) {
var now = (new Date()).getTime();
var limit = startDate.getTime() + (process.env.RUN_TIMEOUT * 1000 * 60);
if (limit < now) {
stoped = true;
logger.warn('RUN_TIMEOUT');
return Promise.resolve([]);
}
}
return Data.access.unknownnames({
where: {
status: 'new'
},
limit: 10,
order: 'createdAt'
});
}
function start() {
return getList()
.then((list) => {
if (!list || list.length === 0 || stoped) {
return 0;
}
return Promise.each(list, (item) => {
return finder.find(item).then(() => {
return Data.control.removeUnknownName({
where: {
_id: item.id
}
});
})
.catch((error) => {
logger.error(error);
return Data.control.updateUnknownName({ id: item.id, status: 'error' });
});
})
.then(start);
});
}
return start();
};
exports.stop = function() {
stoped = true;
};