-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp2.rb
153 lines (123 loc) · 3.27 KB
/
app2.rb
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
# encoding: UTF-8
# A X&O Lab Creative Project
# http://www.x-and-o.co/lab
#
# (C) 2013 X&O. All Rights Reserved
# License information: LICENSE.md
# START IT UP...
APP_MODE = 'scraper'
APP_ROOT = File.expand_path('.', File.dirname(__FILE__))
DEBUG = true
TIME_START = Time.now
require "rubygems"
require "bundler"
Bundler.require
require "#{APP_ROOT}/config.rb"
require 'optparse'
Dir.glob("#{APP_ROOT}/scripts/modules/*.rb").each{|r| require r}
# Ensure the proper options are set on start
parts, options = {}, {}
OptionParser.new do |opts|
opts.banner = "Usage: app.rb [options]"
# opts.on("--debug", "Debug Mode") {|v| DEBUG ||= true}
opts.on("-d", "--daemon", "Daemon Mode") {|v| options[:daemon] = true}
PageQueue::STEPS.each{|k,z|
opts.on("--#{k}") {|v| parts[k] = (parts[k] || 0) + 1}
}
end.parse!
# DEBUG ||= false
PageQueue::STEPS.each{|k,v| parts[k] = 1} if parts.blank?
_heading(parts.map{|k,v| "#{k} : #{v}"}.join(" -- "))
@run_active = true
def spawn_thread(p,i)
return false unless @run_active
sleep(1) # Give each a second to spin up
Thread.new {
Thread.current[:name] = "#{p}_#{i}"
Thread.current[:info] = {:name => p, :number => i}
begin
mod = page_module_for_step(p.to_sym).new(page_module_attrs_for_step(p,i)) rescue nil
mod.run if mod
rescue => err
_error("Error: #{p}_#{i}: #{err}")
puts err.backtrace.inspect
end
}
end
# --- QUEUE ---
result = Proc.new{|parts,opts|
threads = []
begin
parts.each do |part,q|
q ||= 1
(1..q).each do |i|
_subheading("Spawning thread: #{part}_#{i}...")
threads << {:part => part, :i => i, :thread => spawn_thread(part,i)}
end
end
rescue => err
puts "ERROR for Queue: #{err}"
end
# Kill threads upon kill command
trap(0) do
begin
@run_active = false
threads.each {|thread| thread[:thread].exit }
rescue => err
_error(err)
ensure
ActiveRecord::Base.connection.close
_debug('...done0!')
end
end
trap(2) do
begin
@run_active = false
threads.each {|thread| thread[:thread].exit }
rescue => err
_error(err)
ensure
ActiveRecord::Base.connection.close
_debug('...done2!')
exit
end
end
# Keep-alive
while !threads.blank? do
threads.each_with_index do |thread, i|
begin
if thread[:thread].status.blank?
thread[:thread].exit rescue nil
_subheading("Respawning thread: #{thread[:part]}_#{thread[:i]}...")
thread[i][:thread] = spawn_thread(thread[:part], thread[:i])
else
thread[:thread].join(0.5)
end
rescue
_subheading("Respawning thread2: #{thread[:part]}_#{thread[:i]}...")
threads[i][:thread] = spawn_thread(thread[:part], thread[:i])
end
end
end
}
# --- RUN QUEUE ---
begin
if options[:daemon]
puts "Forking process..."
p = fork { sleep(2); result.call(parts, options) }
sleep(2)
s = Process.getpgid(p) rescue nil
if s
Process.detach(p)
File.open('./scraper.pid', "w") {|f| f.write p}
puts " running as #{p}."
else
puts " did not start"
end
else
result.call(parts, options)
end
rescue => err
puts "ERROR: #{err}"
err.backtrace.map{|l| puts " #{l}"} if DEBUG
end