Class: Krane::RunnerTask

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/runner_task.rb

Overview

Run a pod that exits upon completing a task

Defined Under Namespace

Classes: TaskTemplateMissingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, context:, logger: nil, global_timeout: nil) ⇒ RunnerTask

Initializes the runner task



26
27
28
29
30
31
32
# File 'lib/krane/runner_task.rb', line 26

def initialize(namespace:, context:, logger: nil, global_timeout: nil)
  @logger = logger || Krane::FormattedLogger.build(namespace, context)
  @task_config = Krane::TaskConfig.new(context, namespace, @logger)
  @namespace = namespace
  @context = context
  @global_timeout = global_timeout
end

Instance Attribute Details

#pod_nameObject (readonly)

Returns the value of attribute pod_name.



18
19
20
# File 'lib/krane/runner_task.rb', line 18

def pod_name
  @pod_name
end

Instance Method Details

#run(*args) ⇒ Boolean

Runs the task, returning a boolean representing success or failure



37
38
39
40
41
42
# File 'lib/krane/runner_task.rb', line 37

def run(*args)
  run!(*args)
  true
rescue DeploymentTimeoutError, FatalDeploymentError
  false
end

#run!(template:, command:, arguments:, env_vars: [], verify_result: true) ⇒ nil

Runs the task, raising exceptions in case of issues



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/krane/runner_task.rb', line 53

def run!(template:, command:, arguments:, env_vars: [], verify_result: true)
  start = Time.now.utc
  @logger.reset

  @logger.phase_heading("Initializing task")

  @logger.info("Validating configuration")
  verify_config!(template)
  @logger.info("Using namespace '#{@namespace}' in context '#{@context}'")

  pod = build_pod(template, command, arguments, env_vars, verify_result)
  validate_pod(pod)

  @logger.phase_heading("Running pod")
  create_pod(pod)

  if verify_result
    @logger.phase_heading("Streaming logs")
    watch_pod(pod)
  else
    record_status_once(pod)
  end
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('success'))
  @logger.print_summary(:success)
rescue DeploymentTimeoutError
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('timeout'))
  @logger.print_summary(:timed_out)
  raise
rescue FatalDeploymentError
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('failure'))
  @logger.print_summary(:failure)
  raise
end