-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertor-server2.js
88 lines (70 loc) · 2.03 KB
/
convertor-server2.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
var fs = require('fs');
// 读取配置
var config = __dirname + '/etc/setting.json';
var settings = JSON.parse(fs.readFileSync(config, 'utf8'));
var logger = require('./lib/logger').logger;
var _logger = logger(__dirname + '/' + settings.log.file);
var convert = require('./lib/convert').createConvert(settings);
convert.setLogger(_logger);
var devent = require('devent').createDEvent(settings.devent.name);
var queue = require('queuer');
var deq = queue.getQueue(settings.queue.host,settings.queue.deqname);
var enq = queue.getQueue(settings.queue.host,settings.queue.enqname);
var enq4subject = queue.getQueue(settings.queue.host,settings.queue.enqname4subject);
//事件通知有新任务
devent.on('queued', function(msg){
if(queue==settings.devent.name){
convert.emit('task-load');
}
});
//任务完成
var taskFinish=function(task){
try {
devent.emit('task-finished', task);
}catch(e) {
_logger.debug(e);
}
convert.start();
};
//任务异常
var taskError = function(task) {
try {
if(task.retry<=3) {
devent.emit('task-error', task);
}else {
devent.emit('task-finished', task);
}
}catch(e) {
_logger.debug(e);
}
convert.start();
};
//微博发送通知事件
var microPush=function(taskId, micro_blog_type){
if(micro_blog_type == 'zixun') {
enq4subject.enqueue(settings.queue.enqurl4subject+taskId);
} else {
enq.enqueue(settings.queue.enqurl+taskId);
}
};
//从队列中加载任务
var taskLoad=function(){
//出队列
deq.dequeue(function(error,task){
if(task && task!==null && task!==""){
convert.emit('has-task',task);
}else{
convert.start(1);
}
});
};
convert.on('task-finished',taskFinish );
convert.on('task-error',taskError );
convert.on('micro-push',microPush );
convert.on('task-load',taskLoad);
convert.start();
fs.writeFileSync(__dirname + '/convertor.pid', process.pid.toString(), 'ascii');
console.log('Server Started ' + new Date().toLocaleString());
process.on('uncaughtException', function(e){
console.log(['uncaughtException:', e]);
});