-
Notifications
You must be signed in to change notification settings - Fork 52
/
run.js
111 lines (103 loc) · 3.4 KB
/
run.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
/**
* ux crawler entrance
*/
////log setting////////////////////////////////////////////////////////////////////
var logging = require('./lib/logging.js');
////arguments parse///////////////////////////////////////////////////////////////
var userArgv = require('optimist')
.usage('Usage: $0 -i [instance name] -a [crawl|test|config|proxy|schedule] -p [num] -l[url] -h')
.options('i', {
'alias' : 'instance',
'default' : 'pengtouba',
'describe' : 'Specify a instance',
'demand' : true
})
.options('a', {
'alias' : 'action',
'default' : 'crawl',
'describe' : 'Specify a action[crawl|test|config|proxy|schedule]',
'demand' : true
})
.options('p', {
'alias' : 'port',
'default' : 2013,
'describe' : 'Specify a service port, for config service and proxy router'
})
.options('l', {
'alias' : 'link',
'default' : '',
'describe' : 'Specify a url to test crawling'
})
.options('h', {
'alias' : 'help',
'describe' : 'Help infomation'
});
var options = userArgv.argv;
if(options['h']){userArgv.showHelp();process.exit();}
var settings = require('./instance/'+options['i']+'/'+'settings.json');
settings['instance'] = options['i'];
////log level/////////////////////////////////////////////////////////////////
var log_level = 'DEBUG';
if(settings['log_level'])log_level = settings['log_level'];
////crawling action///////////////////////////////////////////////////////////
var crawling = function(){
var logger = logging.getLogger('crawling',options['i'],log_level);
settings['logger'] = logger;
settings['instance'] = options['i'];
var spider = new (require('./spider'))(settings);
spider.start();
}
////proxy Service////////////////////////////////////////////////////////////
var proxyService = function(){
var logger = logging.getLogger('proxy-service',options['i'],log_level);
settings['logger'] = logger;
settings['port'] = parseInt(options['p']);
var proxyRouter = new (require('./proxyrouter'))(settings);
proxyRouter.start();
}
////config service////////////////////////////////////////////////////////////
var configService = function(){
var logger = logging.getLogger('config-service',options['i'],log_level);
settings['logger'] = logger;
settings['port'] = parseInt(options['p']);
var webConfig = new(require('./webconfig'))(settings);
webConfig.start();
}
////scheduler///////////////////////////////////////////////////////////////
var schedule = function(){
var logger = logging.getLogger('schedule',options['i'],log_level);
settings['logger'] = logger;
var scheduler = new (require('./scheduler'))(settings);
scheduler.start();
}
////test url/////////////////////////////////////////////////////////////////
var testUrl = function(){
if(options['l']!=''){
var logger = logging.getLogger('crawling-testing',options['i'],'DEBUG');
settings['logger'] = logger;
settings['test'] = true;
settings['use_proxy'] = false;
var spider = new (require('./spider'))(settings);
spider.test(options['l']);
}
}
////route/////////////////////////////////////////////////////////////////////
switch(options['a']){
case 'crawl':
crawling();
break;
case 'proxy':
proxyService();
break;
case 'config':
configService();
break;
case 'schedule':
schedule();
break;
case 'test':
testUrl();
break;
default:
userArgv.showHelp();
}