-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_controller.rb
executable file
·84 lines (68 loc) · 1.65 KB
/
fetch_controller.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
require 'search_worker'
require 'web_worker'
class FetchController < ApplicationController
def query
worker = SearchWorker.new
worker.search(params)
sleep(1)
worker.get_results.each_with_index do |term, i|
search_term = "#{term[:company]} #{term[:city]} #{term[:state]}"
spawn_worker(search_term, i)
end
worker.done
render "process_list"
end
def process_list
list = params[:targets].split(/\r/)
list.map! {|x| x.lstrip}
list.each_with_index {|x, i| spawn_worker(x, i)}
end
def check_queue_status
@list = get_result
unless @list.empty?
@list.each {|x| log_result(x)}
render :layout => false
else
render :nothing => true
end
end
def export
data = []
params[:markup].each do |row|
t = row.split />/
rdata = [t[6], t[9], t[12], t[14]]
rdata.map! {|x| x.gsub /<.*$/, ''}
data << rdata.join(',')
end
send_data data.join("\n"), :type => "text/csv"
end
private
def spawn_worker(terms, position)
Spawnling.new(:method => :fork) do
with_beanstalk do |queue|
worker = WebWorker.new(terms, position)
queue.put Marshal.dump(worker)
end
end
end
def get_result
rval = []
with_beanstalk do |queue|
while queue.peek(:ready)
job = queue.reserve
rval << Marshal.load(job.body)
job.delete
end
end
rval
end
def with_beanstalk
conn = Beaneater::Pool.new(['localhost:11300'])
queue = conn.tubes["tube"]
yield queue
conn.close
end
def log_result(w)
logger.debug("RESULT #{w.query} #{w.url} #{w.framework} #{w.analytics}")
end
end