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) ⇒ Service


351
352
353
354
# File 'lib/heroku/client.rb', line 351

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

Instance Attribute Details

#attachedObject

Returns the value of attribute attached


349
350
351
# File 'lib/heroku/client.rb', line 349

def attached
  @attached
end

Instance Method Details

#eachObject

Iterate over all output chunks until EOF is reached.


397
398
399
400
401
402
403
# File 'lib/heroku/client.rb', line 397

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?


373
374
375
# File 'lib/heroku/client.rb', line 373

def end_of_stream?
  @next_chunk.nil?
end

#readObject

Read the next chunk of output.


378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/heroku/client.rb', line 378

def read
  chunk = @client.get(@next_chunk)
  if chunk.headers[:location].nil? && chunk.code != 204
    # no more chunks
    @next_chunk = nil
    chunk.to_s
  elsif chunk.to_s == ''
    # 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.to_s
  end
end

#start(command, attached = false) ⇒ Object

start the service


357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/heroku/client.rb', line 357

def start(command, attached=false)
  @attached = attached
  @response = @client.post(
    "/apps/#{@app}/services",
    command,
    :content_type => 'text/plain'
  )
  @next_chunk = @response.to_s
  @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


406
407
408
409
410
# File 'lib/heroku/client.rb', line 406

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