This repository was archived by the owner on Oct 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.rb
More file actions
79 lines (68 loc) · 1.6 KB
/
source.rb
File metadata and controls
79 lines (68 loc) · 1.6 KB
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
require 'observer'
require 'faraday'
require 'oj'
require 'active_support'
require 'active_support/core_ext'
class Source
include Observable
attr_reader :name, :location, :polling_frequency, :data, :thread, :connection, :has_data
def initialize uni_shortname, term_shortname, name, location, polling_frequency
@uni_shortname = uni_shortname
@term_shortname = term_shortname
@name = name
@location = location
@polling_frequency = polling_frequency
@data = read_state
@has_data = @data.present?
end
def start
@thread.exit if @thread
@thread = Thread.new { run }
end
def healthy?
@thread && @thread.alive?
end
private
def run
@connection = Faraday.new(url: "#{@location}/#{@term_shortname}")
loop do
update
sleep @polling_frequency
end
end
def update
begin
response = @connection.get
data = Oj.load response.body
STDERR.puts "DEBUG: Got #{data.first[1].size} records from source #{@name}"
if data != @data
changed
@data = data
write_state
@has_data = true
notify_observers self
end
rescue Exception => msg
STDERR.puts msg
STDERR.puts "ERROR: Unable to get data from source #{@name}"
sleep 5
update
end
end
def handle_data new_data
@data = data
end
def state_key
"#{@uni_shortname}/malg_state/term/#{@term_shortname}/source/#{@name}"
end
def read_state
begin
Oj.load Redis.current.get state_key
rescue
nil
end
end
def write_state
Redis.current.set state_key, @data.to_json
end
end