-
Notifications
You must be signed in to change notification settings - Fork 2
/
view_warmer.rb
executable file
·112 lines (87 loc) · 2.44 KB
/
view_warmer.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
#!/usr/bin/ruby
require 'thread'
###################
# CONF #
###################
# The smallest amount of changed documents before the views are updated
MIN_NUM_OF_CHANGED_DOCS = 10
# URL to the DB on the CouchDB server
URL = "http://localhost:5984"
# Set the minimum pause between calls to the database
PAUSE = 1 # seconds
# One entry for each design document
# in each database
VIEWS = {"ride_on_api_dev" => [
"Stop/_view/all?limit=0",
"StopTime/_view/all?limit=0",
"Trip/_view/all?limit=0",
"ShapePoint/_view/all?limit=0",
"Route/_view/all?limit=0",
"Trip/_spatial/by_route_and_date?count=true",
"StopTime/_spatial/by_stop_and_date?count=true",
"VehiclePosition/_view/all?limit=0"
]}
###################
# RUNTIME #
###################
run = true
number_of_changed_docs = VIEWS.inject({}) do |h, (k,v)|
views_count = v.inject({}) do |vh, vn|
vh[vn] = 0
vh
end
h[k] = views_count
h
end
threads = []
mutex = Mutex.new
# Updates the views
VIEWS.each_pair do |db_name, views|
views.each do |v_name|
threads << Thread.new do
while run do
number_of_docs = number_of_changed_docs[db_name][v_name]
if number_of_docs >= MIN_NUM_OF_CHANGED_DOCS
# Reset the value
mutex.synchronize {
number_of_changed_docs[db_name][v_name] = 0
}
`curl #{URL}/#{db_name}/_design/#{v_name}`
end
# Pause before starting over again
sleep PAUSE
end
end
end
end
# Receives the update notification from CouchDB
threads << Thread.new do
while run do
STDERR << "Waiting for input\n"
update_call = gets
# When CouchDB exits the script gets called with
# a never ending series of nil
if update_call == nil
run = false
else
# Get the database name out of the call data
# The data looks something like this:
# {"type":"updated","db":"DB_NAME"}\n
update_call =~ /\"db\":\"(\w+)\"/
database_name = $1
if number_of_changed_docs.has_key?(database_name)
mutex.synchronize do
# Set to 0 if it hasn't been initialized before
# Add one pending changed document to the list of documents
# in the DB
number_of_changed_docs[database_name] = number_of_changed_docs[database_name].inject({}) do |h, (k,v)|
h[k] = v + 1
h
end
end
end
end
end
end
# Good bye
threads.each {|thr| thr.join}