Module: Phonehome
- Defined in:
- lib/phonehome.rb,
lib/phonehome/gem/version.rb
Defined Under Namespace
Modules: Gem
Classes: JobInfo
Constant Summary
collapse
- @@phonehomeURL =
"phonehome.io"
Class Method Summary
collapse
Class Method Details
.call(secret_key, id) ⇒ Object
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
|
# File 'lib/phonehome.rb', line 15
def self.call(secret_key, id)
begin
debug("Checking connection")
result = check_connection?{yield}
if result == false
p 'Phonehome not responding'
return false
end
debug("Checking secret key")
check_secret_key(secret_key){yield}
debug("Checking User events")
user_events = get_user_events(secret_key).split
status = user_events.include?(id.to_s) ? true : false
if status == false
p 'You try to run not your event!'
yield
exit
end
debug("Calling start")
start(secret_key, id){yield}
result_of_work = yield
debug("Calling stop." + result_of_work.to_s)
stop(id,result_of_work)
rescue => e
if e.class == RuntimeError && e.to_s =~ /All events are completed/
return false
else
debug("Sending error to server")
send_error(id, e)
raise e
end
end
end
|
.check_connection ⇒ Object
116
117
118
119
120
121
122
123
124
|
# File 'lib/phonehome.rb', line 116
def self.check_connection
begin
response = excon_get("http://#{@@phonehomeURL}")
rescue Excon::Errors::SocketError => error
p "#{error} - Server not responding"
sleep(5)
retry
end
end
|
.check_connection? ⇒ Boolean
check connection with server
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/phonehome.rb', line 126
def self.check_connection?
begin
response = excon_get("http://#{@@phonehomeURL}")
return true
rescue Excon::Errors::SocketError => error
begin
yield
rescue
return false
end
return false
end
end
|
.check_secret_key(secret_key) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/phonehome.rb', line 107
def self.check_secret_key(secret_key)
response = excon_post("http://#{@@phonehomeURL}/check_secret_key/#{secret_key}")
if response.data[:body] != 'true'
p 'Your secret key is invalid!'
yield
exit
end
end
|
.debug(message) ⇒ Object
163
164
165
|
# File 'lib/phonehome.rb', line 163
def self.debug(message)
end
|
.excon_get(url) ⇒ Object
150
151
152
153
|
# File 'lib/phonehome.rb', line 150
def self.excon_get(url)
debug("Excon.get #{url}")
Excon.get(url)
end
|
.excon_post(url) ⇒ Object
140
141
142
143
|
# File 'lib/phonehome.rb', line 140
def self.excon_post(url)
debug("Excon.post #{url}")
Excon.post(url)
end
|
.excon_post_json(url) ⇒ Object
145
146
147
148
|
# File 'lib/phonehome.rb', line 145
def self.excon_post_json(url)
debug("Excon.post JSON #{url}")
Excon.post(url, :headers => { "Content-Type" => "application/json" })
end
|
.get_app_name(id) ⇒ Object
96
97
98
99
|
# File 'lib/phonehome.rb', line 96
def self.get_app_name(id)
response = excon_post_json("http://#{@@phonehomeURL}/get_name/#{id}")
return response.data[:body]
end
|
.get_status(type) ⇒ Object
155
156
157
158
159
160
161
|
# File 'lib/phonehome.rb', line 155
def self.get_status(type)
if type == 1
return 'working'
else
return 'completed'
end
end
|
.get_user_events(secret_key) ⇒ Object
101
102
103
104
105
|
# File 'lib/phonehome.rb', line 101
def self.get_user_events(secret_key)
response = excon_post("http://#{@@phonehomeURL}/get_user_event/#{secret_key}")
response.data[:body]
return response.data[:body]
end
|
.send_error(id, e) ⇒ Object
89
90
91
92
93
94
|
# File 'lib/phonehome.rb', line 89
def self.send_error(id, e)
if @events_info.include?(id)
job_info = @events_info[id]
excon_post("http://#{@@phonehomeURL}/query_update/#{job_info.api_job_id}/failed/#{URI.encode_www_form('error' => e)}")
end
end
|
.set_url(url) ⇒ Object
11
12
13
|
# File 'lib/phonehome.rb', line 11
def self.set_url(url)
@@phonehomeURL = url
end
|
.start(secret_key, id) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/phonehome.rb', line 50
def self.start(secret_key, id)
begin_time = Time.now
app_name = get_app_name(id)
response = excon_post_json("http://#{@@phonehomeURL}/query/#{secret_key}/#{get_status(1)}/#{id}")
if JSON.parse(response.body)['status'] == 'All events are completed'
p "#{id}: All events are completed"
yield
raise "#{id}: All events are completed"
end
job_info = JobInfo.new(JSON.parse(response.body)['id'], JSON.parse(response.body)['status'], begin_time)
@events_info[id] = job_info
job_info
end
|
.stop(id, result_of_work = nil) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/phonehome.rb', line 66
def self.stop(id,result_of_work = nil)
finish = Time.now
status = 'completed'
if @events_info.include?(id)
job_info = @events_info[id]
if job_info.status == 'late'
status = 'late_completed'
end
text = "#{finish.to_i - job_info.begin_time.to_i} secs"
if (result_of_work.nil?)
else
text = result_of_work.to_s + " (" + text + ")"
end
excon_post("http://#{@@phonehomeURL}/query_update/#{job_info.api_job_id}/#{status}/#{URI.encode_www_form('result' => text)}")
else
puts "Event with id '#{id}' not found in events_info"
pp events_info
end
end
|