Class: Bosh::Director::Errand::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/errand/runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(job, result_file, instance_manager, logs_fetcher) ⇒ Runner

Returns a new instance of Runner.

Parameters:



8
9
10
11
12
13
14
# File 'lib/bosh/director/errand/runner.rb', line 8

def initialize(job, result_file, instance_manager, logs_fetcher)
  @job = job
  @result_file = result_file
  @instance_manager = instance_manager
  @agent_task_id = nil
  @logs_fetcher = logs_fetcher
end

Instance Method Details

#cancelObject



59
60
61
# File 'lib/bosh/director/errand/runner.rb', line 59

def cancel
  agent.cancel_task(agent_task_id) if agent_task_id
end

#run(&blk) ⇒ String

Runs errand on job instances

Returns:

  • (String)

    short description of the errand result

Raises:

  • (@cancel_error)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bosh/director/errand/runner.rb', line 18

def run(&blk)
  unless instance
    raise DirectorError, 'Must have at least one instance group instance to run an errand'
  end

  agent_task_result = nil
  event_log_stage = Config.event_log.begin_stage('Running errand', 1)

  begin
    event_log_stage.advance_and_track("#{@job.name}/#{instance.index}") do
      run_errand_result = agent.run_errand
      @agent_task_id = run_errand_result['agent_task_id']
      agent_task_result = agent.wait_for_task(agent_task_id, &blk)
    end
  rescue TaskCancelled => e
    # Existing run_errand long running task will return a result
    # after agent cancels the task
    agent_task_result = agent.wait_for_task(agent_task_id)
    @cancel_error = e
  end

  begin
    logs_blobstore_id = @logs_fetcher.fetch(instance.model, 'job', nil)
  rescue DirectorError => e
    @fetch_logs_error = e
  end

  if agent_task_result
    errand_result = Errand::Result.from_agent_task_results(agent_task_result, logs_blobstore_id)
    @result_file.write(JSON.dump(errand_result.to_hash) + "\n")
  end

  # Prefer to raise cancel error because
  # it was triggered before trying to fetch logs
  raise @cancel_error if @cancel_error

  raise @fetch_logs_error if @fetch_logs_error

  errand_result.short_description(@job.name)
end