-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
97 lines (82 loc) · 2.17 KB
/
app.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
require 'sinatra'
require 'reve'
require 'yaml'
require 'models'
require 'lib/skills'
require 'appengine-apis/users'
require 'appengine-apis/logger'
require 'appengine-apis/mail'
require 'appengine-apis/labs/taskqueue'
before do
@user = Users.current_user
@pilot = Pilot.first(:nickname => @user.nickname) if @user
end
get '/' do
erb :index
end
get '/skillqueue' do
begin
@skills = @pilot.skill_queue
rescue Exception => e
return "Error fetching your queue, please check your API info and try again later: #{e.class.name} - #{e.message}"
end
return 'No skills in queue.' if @skills.empty?
erb :skillqueue
end
get '/pilot' do
@pilot ||= Pilot.new
erb :pilot
end
post '/pilot' do
@pilot ||= Pilot.new
@pilot.attributes = params['pilot']
@pilot.monitor = !params['pilot']['monitor'].nil?
if @pilot.email.nil? || @pilot.email.empty?
@pilot.email = @user.email
end
@pilot.nickname = @user.nickname
if @pilot.save
redirect '/'
else
"ERROR: #{@pilot.errors.inspect}"
end
end
get '/cron/monitor' do
Pilot.all(:monitor => true).each do |pilot|
url = "/queue/monitor/#{pilot.nickname}"
AppEngine::Labs::TaskQueue.add(:url => url)
end
'ok'
end
post '/queue/monitor/:pilot' do
pilot = Pilot.first(:nickname => params[:pilot])
begin
if pilot.skill_queue.empty? && !pilot.notified
url = "/queue/notify/#{pilot.nickname}"
AppEngine::Labs::TaskQueue.add(:url => url)
elsif pilot.skill_queue.any? && pilot.notified
pilot.notified = false
pilot.save
end
rescue Exception => e
gae_log = AppEngine::Logger.new
gae_log.warn "Problem with #{pilot.nickname}: #{e.class.name} - #{e.message}"
end
end
post '/queue/notify/:pilot' do
pilot = Pilot.first(:nickname => params[:pilot])
send_queue_warning(pilot)
gae_log = AppEngine::Logger.new
gae_log.info "Sent email to #{pilot.nickname}."
end
def send_queue_warning(pilot)
user_address = pilot.email
sender_address = "[email protected]"
subject = "[skeves] Skill queue empty."
body = <<-EOM
No skill in training.
EOM
AppEngine::Mail.send(sender_address, user_address, subject, body)
pilot.notified = true
pilot.save
end