Class: Heroku::Client::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, app, upid = nil) ⇒ Service

Returns a new instance of Service.



201
202
203
204
205
# File 'lib/heroku/client.rb', line 201

def initialize(client, app, upid=nil)
	@client = client
	@app = app
	@upid = upid
end

Instance Attribute Details

#attachedObject

Returns the value of attribute attached.



199
200
201
# File 'lib/heroku/client.rb', line 199

def attached
  @attached
end

#upidObject

Returns the value of attribute upid.



199
200
201
# File 'lib/heroku/client.rb', line 199

def upid
  @upid
end

Instance Method Details

#bounceObject



237
# File 'lib/heroku/client.rb', line 237

def bounce ; transition('bounce') ; end

#downObject



235
# File 'lib/heroku/client.rb', line 235

def down   ; transition('down') ; end

#eachObject

Iterate over all output chunks until EOF is reached.



264
265
266
267
268
269
270
# File 'lib/heroku/client.rb', line 264

def each
	until end_of_stream?
		sleep(@interval)
		output = read
		yield output unless output.empty?
	end
end

#end_of_stream?Boolean

Does the service have any remaining output?

Returns:

  • (Boolean)


240
241
242
# File 'lib/heroku/client.rb', line 240

def end_of_stream?
	@next_chunk.nil?
end

#readObject

Read the next chunk of output.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/heroku/client.rb', line 245

def read
	chunk = @client.get(@next_chunk)
	if chunk.nil? or chunk == ''
		# assume no content and back off
		@interval = 2
		''
	elsif location = chunk.headers[:location]
		# some data read and next chunk available
		@next_chunk = location
		@interval = 0
		chunk
	else
		# no more chunks
		@next_chunk = nil
		chunk
	end
end

#start(command, attached = false) ⇒ Object

start the service



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/heroku/client.rb', line 208

def start(command, attached=false)
	@attached = attached
	@response = @client.post(
		"/apps/#{@app}/services",
		command,
		:content_type => 'text/plain'
	)
	@next_chunk = @response
	@interval = 0
	self
rescue RestClient::RequestFailed => e
	raise AppCrashed, e.http_body  if e.http_code == 502
	raise
end

#to_sObject

All output as a string



273
274
275
276
277
# File 'lib/heroku/client.rb', line 273

def to_s
	buf = []
	each { |part| buf << part }
	buf.join
end

#transition(action) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/heroku/client.rb', line 223

def transition(action)
	@response = @client.put(
		"/apps/#{@app}/services/#{@upid}",
		action,
		:content_type => 'text/plain'
	)
	self
rescue RestClient::RequestFailed => e
	raise AppCrashed, e.http_body  if e.http_code == 502
	raise
end

#upObject



236
# File 'lib/heroku/client.rb', line 236

def up     ; transition('up')   ; end