-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.js
167 lines (141 loc) · 4.67 KB
/
delete.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var fs = require('fs');
var settings = require(__dirname + '/etc/settings.json');
var url = require('url');
var de = require('devent').createDEvent('sender');
var queue = require('queuer');
var logger = require('./lib/logger').logger(settings.logFile);
var util = require('util');
var event = require('events').EventEmitter;
//发送队列的API
var deleteQ = queue.getQueue('http://'+settings.queue.host+':'+settings.queue.port+'/queue', settings.queue.delete);
var Deleter= require('./lib/deleter').Deleter;
//发送对象保存在该数组中
var deleters = [];
var db = require('./lib/db').db;
db.init(settings);
//所有微博账号
var weiboAccounts = {};
db.loadAccounts(function(err, accounts){
if(err){
console.log('!!!load account error!!!');
return;
}
weiboAccounts = accounts;
console.log('access token loaded');
//由于发送依赖账号,所以必须先加载完账号才能开始处理发送请求
console.log('starting dequeue');
start();
});
var taskBack = function(task, status){
if(status){
de.emit('task-finished', task);
}else{
de.emit('task-error', task);
}
}
var dequeue = function(){
for(var i = 0; i < deleters.length; i++){
if(settings.mode == 'debug'){
console.log('running status--'+ i + '--'+ deleters[i].running);
}
if(deleters[i].running){
continue;
}
(function(deleter){
deleter.running = true;
deleteQ.dequeue(function(err, task){
if(err == 'empty' || task == undefined){
deleter.running = false;
console.log('delete queue is empty');
return;
}
console.log(['dequeue', task]);
del(task, deleter, {task:task});
});
})(deleters[i]);
}
}
var start = function(){
setInterval(function(){
dequeue();
}, settings.queue.interval);
de.on('queued', function( queue ){
if(queue == settings.queue.delete){
console.log( queue + "有内容");
dequeue();
}
});
console.log('deleter start ok');
}
var del = function(task, deleter, context){
db.getSentBlogByUri(task.uri, function(err, results){
if(err || results.length == 0){
logger.info("error\tNot found the resource:" + task.uri);
deleter.running = false;
return;
}
blog = results[0];
//微博账号错误
if(!weiboAccounts[blog.stock_code] ||
!weiboAccounts[blog.stock_code].access_token ||
!weiboAccounts[blog.stock_code].access_token_secret){
logger.info("error\t" + blog.id + "\t" + blog.stock_code + "\tNOT Found the account\t");
deleter.running = false;
taskBack(task, true);
return;
}
deleter.delete(blog, weiboAccounts[blog.stock_code], context);
});
};
/**
发送结束后的处理,返回true表示发送完成
*/
var complete = function(error, body, blog, task){
if(!error){
logger.info("success\t" + blog.id + "\t" + blog.weibo_id + "\t"+ blog.stock_code );
db.deleteSuccess(blog);
return true;
}
var errMsg = error.error;
logger.info("error\t" + blog.id +"\t"+ blog.weibo_id + "\t" + blog.stock_code + "\t" + errMsg);
if(task.retry >= settings.queue.retry){
logger.info("error\t" + blog.id +"\t"+ blog.weibo_id + "\t"+ blog.stock_code + "\tretry count more than "+settings.queue.retry);
return true;
}else{
return false;
}
}
for(i = 0; i < settings.deletersCount; i++){
var deleter = new Deleter();
deleter.init(settings);
(function(s){
s.on('delete', function(error, body, blog, context){
s.running = false;
taskBack(context.task, complete(error, body, blog, context.task));
dequeue();
})
})(deleter);
deleters.push(deleter);
}
fs.writeFileSync(__dirname + '/server.pid', process.pid.toString(), 'ascii');
//收到进程信号重新初始化
process.on('SIGUSR2', function () {
settings = require('./etc/settings.json');
db.init(settings);
for(i = 0; i < senders.length; i++){
senders[i].init(settings);
}
});
/**
* 测试代码
setTimeout(function(){
var sender = new Sender();
sender.init(settings);
var task = {uri:'mysql://abc.com/stock_radar#1'};
sender.on('send', function(error, body, blog, context){
console.log(error);
taskBack(context.task, complete(error, body, blog));
});
send(task, sender, {task:task});
}, 1000);
*/