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
167
168
169
170
171
172
173
|
# File 'lib/spider_bot/cli.rb', line 99
def start
puts "start....."
$expire_num = options[:expire].to_i if options[:expire]
if options[:env]
ENV['RACK_ENV'] = options[:env]
else
ENV['RACK_ENV']= 'development'
end
require File.join(File.expand_path('../..',__FILE__), "spider_bot/load")
FileUtils.mkdir_p("tmp/pids") if !File.exists?("tmp/pids")
daemon_options = {
app_name: 'spider',
ontop: true,
dir: 'tmp/pids',
}
sleep_time = 10
if options[:daemon]
daemon_options[:ontop] = false
else
puts "press ctrl-c exit"
end
stop if File.exists?("tmp/spider.pid")
if option_time = options[:time]
parse_time = option_time.match(/[d|h|m]/)
sleep_time = if parse_time
case parse_time[0]
when "d"
option_time.to_i * 60 * 60 * 24
when "h"
option_time.to_i * 60 * 60
when "m"
option_time.to_i * 60
end
else
option_time.to_i
end
end
Daemons.daemonize(daemon_options)
loop do
threads = []
BOTDIR.each do |file|
threads << Thread.new do
begin
SpiderBot.logger.info "loading bot file with #{file}."
load file
rescue Exception => e
SpiderBot.logger.error "has errors with loading bot file #{ file }"
SpiderBot.logger.error e.to_s
end
sleep(10)
end
end
threads.each { |t| t.join }
if options[:random]
random_time = Random.new.rand(sleep_time)
sleep(random_time.to_i)
else
sleep(sleep_time.to_i)
end
end
end
|