-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscovery.rb
82 lines (68 loc) · 2.17 KB
/
discovery.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
require 'sinatra'
require './config'
#########################################
########### Foreman Methods #############
#########################################
# POST /tasks?n=num_tasks
# Pay for a list of tasks and their associated workers; returns at most
# num_tasks tasks (possibly less)
post '/tasks' do
foreman_addr = request.ip
num_tasks_requested = (params[:n] || 1).to_i
make_tasks(foreman_addr, num_tasks_requested)
end
# DELETE /tasks/:id(?valid=1)
# If a task is valid, return a key and give credits to workers. Otherwise
# restore credits to the foreman and return how many credits the foreman has.
# If request is invalid, don't tell the user whether the task doesn't exist or
# they simply don't have access
delete '/tasks/:id' do
foreman_addr = request.ip
task_id = params[:id]
valid = (params[:valid] == '1')
missing = params[:missing]
atomic_delete_task(task_id, foreman_addr, valid, missing)
end
#########################################
########### Worker Methods ##############
#########################################
# POST /workers?port=P&ttl=T
# Register as a worker
post '/workers' do
addr = request.ip
port = params[:port]
ttl = params[:ttl]
add_worker(addr, port, ttl)
end
# GET /tasks/:id
# Return a key to workers involved in a task
get '/tasks/:id' do
client_addr = request.ip
task_id = params[:id]
get_task_key(task_id, client_addr)
end
# GET /
# Returns info about the requester
get '/' do
get_client(request.ip)
end
#########################################
######### Development stuff #############
#########################################
# Danger! Deletes the database and seeds it
get '/seed' do
REDIS.flushdb
seed_db(request.ip)
end
#########################################
############ Error Handling #############
#########################################
# Library methods raise exceptions so that controller routes don't have to.
# This catches the ones we expect--they must be subclassed from
# DiscoveryServiceException and respond to a "code" method (http_status in
# Sinatra 1.4.x, but whatever)
error DiscoveryServiceException do
exception = env['sinatra.error']
status exception.code
body exception.response
end