Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 8bd4d90

Browse files
committed
bot.mirror: monitori support. See avn#gs#2
gs link archlinuxvn/gs#2
1 parent 434723d commit 8bd4d90

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

bot/plugins/mirror.rb

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Date : 2014 Aug 09th
66

77
require 'json'
8+
require 'time'
89

910
class Mirror
1011
include Cinch::Plugin
@@ -14,42 +15,74 @@ class Mirror
1415
match /mirror (.+)/, :method => :mirror_status
1516
match /mirror$/, :method => :mirror_default
1617

18+
def initialize(*args)
19+
super
20+
@curl_data = {}
21+
end
22+
23+
def listen(m)
24+
mirror_cron(m)
25+
end
26+
1727
def mirror_default(m)
1828
mirror_status(m, "status")
1929
end
2030

21-
def mirror_status(m, msg)
22-
real_user = bot_real_user(m.user.nick).to_s.gsub(/_+$/, '')
31+
def mirror_monitor(m)
32+
if gs = @curl_data["f"]["report_time"].match(/^([0-9]{8})-([0-9]{2})([0-9]{2})([0-9]{2})/)
33+
date, h, m, s = gs[1], gs[2], gs[3], gs[4]
34+
offset = Time.now - Time.parse(sprintf("%s %s:%s:%s", date, h, m, s))
35+
if offset >= 4800 # 3600 + 1800 aka 1.5 hours
36+
m.reply "!! Warning: Mirror is out-of-sync. Last update is #{offset / 60} minutes ago" \
37+
unless _cache_expired(:mirror, "cron_warning", :cache_time => 1800)
38+
end
39+
else
40+
m.reply "!! Error: Invalid curl data found" \
41+
unless _cache_expired(:mirror, "cron_error", :cache_time => 1800)
42+
end
43+
return @curl_data
44+
end
45+
46+
# Update data every 0.5 hour
47+
def mirror_cron(m)
48+
return mirror_monitor(m) \
49+
if not _cache_expired?(:mirror, "cron", :cache_time => 1800)
50+
2351
url = "http://f.archlinuxvn.org/archlinux/status.json"
2452
fpt = "http://mirror-fpt-telecom.fpt.net/archlinux/lastsync"
2553

26-
if not _cache_expired?(:mirror, "#{real_user}.#{msg}", :cache_time => 65)
27-
m.reply "#{m.user.nick}: please wait some seconds..."
28-
return
29-
end
30-
3154
json_data = %x[curl --connect-timeout 3 -A "bot/#{BOT_NAME}" -s #{url}]
3255
begin
33-
status = JSON.parse(json_data)
56+
@curl_data["f"] = JSON.parse(json_data)
3457
rescue => e
35-
m.reply "#{m.user.nick}: unable to fetch status from f.archlinuxvn.org"
36-
return
58+
@curl_data["f"] = {}
3759
end
3860

3961
fpt_lastsync_i = %x[curl --connect-timeout 3 -A "archlinuxvn/bot/#{BOT_NAME}" #{fpt}].strip.to_i
4062
fpt_lastsync_s = Time.at(fpt_lastsync_i).localtime("+07:00").strftime("%Y%m%d-%H%M%S")
63+
@curl_data["fpt"] = fpt_lastsync_s
64+
65+
return mirror_monitor(m)
66+
end
67+
68+
def mirror_status(m, msg)
69+
real_user = bot_real_user(m.user.nick).to_s.gsub(/_+$/, '')
70+
71+
if not _cache_expired?(:mirror, "#{real_user}.#{msg}", :cache_time => 65)
72+
m.reply "#{m.user.nick}: please wait some seconds..."
73+
return
74+
end
75+
76+
@curl_data = mirror_cron(m)
4177

4278
echo = case msg.strip
43-
when "config" then status["mirror_config"]
79+
when "config" then @curl_data["mirror_config"] || "error"
4480
when "status" then
45-
sprintf("updated %s (up %s); packages: %s (64), %s (32), %s (any); size: %s; FPT updated %s", \
46-
status["report_time"],
47-
status["number_of_updated_packages"],
48-
status["number_of_packages_x86_64"],
49-
status["number_of_packages_i686"],
50-
status["number_of_packages_any"],
51-
status["repo_total_size_in_name"],
52-
fpt_lastsync_s)
81+
sprintf("updated %s (up %s); size: %s; FPT updated %s", \
82+
@curl_data["f"]["report_time"],
83+
@curl_data["f"]["number_of_updated_packages"],
84+
@curl_data["f"]["repo_total_size_in_name"],
85+
@curl_data["fpt"])
5386
end
5487

5588
m.reply "#{m.user.nick}: #{echo}"

0 commit comments

Comments
 (0)