lib/cloud_crowd/app.rb in documentcloud-cloud-crowd-0.0.5 vs lib/cloud_crowd/app.rb in documentcloud-cloud-crowd-0.0.6

- old
+ new

@@ -2,21 +2,22 @@ # The main CloudCrowd (Sinatra) application. The actions are: # # == Admin # [get /] Render the admin console, with a progress meter for running jobs. - # [get /jobs] Get the combined JSON of every active job in the queue. + # [get /status] Get the combined JSON of every active job and worker. # [get /heartbeat] Returns 200 OK to let monitoring tools know the server's up. # # == Public API # [post /jobs] Begin a new Job. Post with a JSON representation of the job-to-be. (see examples). # [get /jobs/:job_id] Check the status of a Job. Response includes output, if the Job has finished. # [delete /jobs/:job_id] Clean up a Job when you're done downloading the results. Removes all intermediate files. # # == Internal Workers API # [post /work] Dequeue the next WorkUnit, and hand it off to the worker. # [put /work/:unit_id] Mark a finished WorkUnit as completed or failed, with results. + # [put /worker] Keep a record of an actively running worker. class App < Sinatra::Default set :root, ROOT set :authorization_realm, "CloudCrowd" @@ -33,15 +34,27 @@ # Render the admin console. get '/' do erb :index end - # Get the JSON for every active job in the queue. - get '/jobs' do - json Job.incomplete + # Get the JSON for every active job in the queue and every active worker + # in the system. This action may get a little worrisome as the system grows + # larger -- keep it in mind. + get '/status' do + json( + 'jobs' => Job.incomplete, + 'workers' => WorkerRecord.alive(:order => 'name desc'), + 'work_unit_count' => WorkUnit.incomplete.count + ) end + # Get the JSON for a worker record's work unit, if one exists. + get '/worker/:name' do + record = WorkerRecord.find_by_name params[:name] + json((record && record.work_unit) || {}) + end + # To monitor the central server with Monit, God, Nagios, or another # monitoring tool, you can hit /heartbeat to make sure. get '/heartbeat' do "buh-bump" end @@ -64,10 +77,12 @@ delete '/jobs/:job_id' do current_job.cleanup json nil end + # INTERNAL WORKER DAEMON API: + # Internal method for worker daemons to fetch the work unit at the front # of the queue. Work unit is marked as taken and handed off to the worker. post '/work' do json dequeue_work_unit end @@ -86,9 +101,16 @@ json dequeue_work_unit(1) else error(500, "Completing a work unit must specify status.") end end + end + + # Every so often workers check in to let the central server know that + # they're still alive. Keep up-to-date records + put '/worker' do + params[:terminated] ? WorkerRecord.check_out(params) : WorkerRecord.check_in(params) + json nil end end end \ No newline at end of file