Class: Sabbath::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sabbath/server.rb

Defined Under Namespace

Classes: DeferrableBody, MethodOverride, StatsProvider

Constant Summary collapse

AsyncResponse =
[-1, {}, []].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, web_host, web_port, rackup, cookie_name = 'sabbath_id') ⇒ Server

Returns a new instance of Server.



90
91
92
93
94
95
96
97
98
99
# File 'lib/sabbath/server.rb', line 90

def initialize(backend, web_host, web_port, rackup, cookie_name = 'sabbath_id')
  @backend = backend
  @web_host, @web_port, @cookie_name, @rackup = web_host, web_port, cookie_name, rackup
  @router = Usher.new(:request_methods => [:request_method], :delimiters => ['/', '.'])
  @router.add_route('/:tube',                   :conditions => {:request_method => 'GET'})      .name(:get_latest_job)
  @router.add_route('/:tube/:job_id',           :conditions => {:request_method => 'GET'})      .name(:get_job)
  @router.add_route('/:tube',                   :conditions => {:request_method => 'POST'})     .name(:create_job)
  @router.add_route('/:tube/:job_id',           :conditions => {:request_method => 'DELETE'})   .name(:delete_job)
  @router.add_route('/:tube/:job_id/release',   :conditions => {:request_method => 'PUT'})      .name(:release_job)
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



88
89
90
# File 'lib/sabbath/server.rb', line 88

def backend
  @backend
end

Returns the value of attribute cookie_name.



88
89
90
# File 'lib/sabbath/server.rb', line 88

def cookie_name
  @cookie_name
end

#rackupObject (readonly)

Returns the value of attribute rackup.



88
89
90
# File 'lib/sabbath/server.rb', line 88

def rackup
  @rackup
end

#web_hostObject (readonly)

Returns the value of attribute web_host.



88
89
90
# File 'lib/sabbath/server.rb', line 88

def web_host
  @web_host
end

#web_portObject (readonly)

Returns the value of attribute web_port.



88
89
90
# File 'lib/sabbath/server.rb', line 88

def web_port
  @web_port
end

Instance Method Details

#builderObject



101
102
103
104
105
106
107
108
# File 'lib/sabbath/server.rb', line 101

def builder
  builder = Rack::Builder.new
  builder.use(StatsProvider, backend.name, backend.host, backend.port)
  builder.instance_eval(File.read(rackup)) if rackup
  builder.use(MethodOverride)
  builder.run(self)
  builder
end

#call(env) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sabbath/server.rb', line 110

def call(env)
  p env
  request = Rack::Request.new(env)
  query_params = Rack::Utils.parse_query(request.query_string)
  
  id = request.cookies[cookie_name] || UUID.new.generate
  common_response_headers = {'Content-Type' => 'text/javascript'}
  
  common_response_headers['Set-cookie'] = Rack::Utils.build_query(cookie_name => id) unless request.cookies[cookie_name]
  
  body = DeferrableBody.new
  # Get the headers out there asap, let the client know we're alive...
  EventMachine::next_tick {
    body.jsonp_callback = query_params['callback']
    case response = @router.recognize(request)
    when nil
      env['async.callback'].call([404, {}, []])
    else
      params = Hash[response.params]
      p response.path.route.named
      p params
      case response.path.route.named
      when :get_latest_job
        env['async.callback'].call([200, common_response_headers, body])
        backend.get_latest_job(id, params[:tube], params['timeout']) {|job|
          body.succeed_with(:id => job.id, :body => job.body)
        }.on_error {|message|
          body.succeed_with(:error => message)
        }
      when :get_job
        env['async.callback'].call([200, common_response_headers, body])
        backend.get_job(id, params[:tube], params[:job_id]) {|job|
          body.succeed_with(:id => job.id, :body => job.body)
        }.on_error {|message|
          body.succeed_with(:error => message)
        }
      when :create_job
        env['async.callback'].call([200, common_response_headers, body])
        backend.create_job(id, params[:tube], query_params['body']) {|id|
          body.succeed_with(:id => id)
        }.on_error {|message|
          body.succeed_with(:error => message)
        }
      when :delete_job
        env['async.callback'].call([200, common_response_headers, body])
        backend.delete_job(id, params[:tube], params[:job_id]) {
          body.succeed_with(:success => true)
        }.on_error {|message|
          body.succeed_with(:error => message)
        }
      when :release_job
        env['async.callback'].call([200, common_response_headers, body])
        backend.release_job(id, params[:tube], params[:job_id]) {
          body.succeed_with(:success => true)
        }.on_error {|message|
          body.succeed_with(:error => message)
        }
      end
    end
  }
  AsyncResponse
end

#startObject



173
174
175
176
177
# File 'lib/sabbath/server.rb', line 173

def start
  EM.run do
    Thin::Server.start(web_host, web_port, builder.to_app)
  end      
end