Sha256: 865bff3998a3f2eb56a4a89354a8261d0e33dc7fd339f7f8221360f8775e6d20

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module CloudCrowd
  
  class App < Sinatra::Default
        
    # static serves files from /public, methodoverride allows the _method param.
    enable :static, :methodoverride
    
    helpers CloudCrowd::Helpers
    
    # Start a new job. Accepts a JSON representation of the job-to-be.
    post '/jobs' do
      Job.create_from_request(JSON.parse(params[:json])).to_json
    end
    
    # Check the status of a job, returning the output if finished, and the
    # number of work units remaining otherwise. 
    get '/jobs/:job_id' do
      current_job.to_json
    end
    
    # Cleans up a Job's saved S3 files. Delete a Job after you're done 
    # downloading the results.
    delete '/jobs/:job_id' do
      current_job.cleanup
      ''
    end
    
    # 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.
    get '/work' do
      begin
        unit = WorkUnit.first(:conditions => {:status => CloudCrowd::INCOMPLETE, :taken => false}, :order => "created_at desc")
        return status(204) && '' unless unit
        unit.update_attributes(:taken => true)
        unit.to_json
      rescue ActiveRecord::StaleObjectError => e
        return status(204) && ''
      end
    end
    
    # When workers are done with their unit, either successfully on in failure,
    # they mark it back on the central server.
    put '/work/:work_unit_id' do
      case params[:status]
      when 'succeeded' then current_work_unit.finish(params[:output], params[:time])
      when 'failed'    then current_work_unit.fail(params[:output], params[:time])
      else             return error(500, "Completing a work unit must specify status.")
      end
      return status(204) && ''
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
documentcloud-cloud-crowd-0.0.1 lib/cloud_crowd/app.rb