Class: Elders::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/elders/task.rb

Constant Summary collapse

TIMEOUT =

Task timeout

1500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, image, command) ⇒ Task

Creates a new task, but not start it

Parameters:

  • name (String)
    • Task name

  • image (String)
    • Docker image that the command

    is going to be runned on.

  • command (String)
    • The bash command that is

    going to be runned.



15
16
17
18
19
20
# File 'lib/elders/task.rb', line 15

def initialize(name, image, command)
  # Define the values
  @name = name
  @image_name = image
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Attributes



7
8
9
# File 'lib/elders/task.rb', line 7

def command
  @command
end

#nameObject (readonly)

Attributes



7
8
9
# File 'lib/elders/task.rb', line 7

def name
  @name
end

#promiseObject (readonly)

Attributes



7
8
9
# File 'lib/elders/task.rb', line 7

def promise
  @promise
end

Instance Method Details

#cleanObject

Clean the task



66
67
68
69
70
71
72
# File 'lib/elders/task.rb', line 66

def clean
  # There is nothing to clean
  return true unless container?

  # Delete the container
  delete == nil
end

#completed?Boolean

Completed the task?

Returns:

  • (Boolean)


98
99
100
# File 'lib/elders/task.rb', line 98

def completed?
  @promise.fulfilled?
end

#containerObject

Get the task container



75
76
77
78
79
80
81
# File 'lib/elders/task.rb', line 75

def container
  # Check if @container exists
  raise 'Task was not started' unless container?

  # Return the container
  @container
end

#container?Boolean

Check if the container exists

Returns:

  • (Boolean)


84
85
86
# File 'lib/elders/task.rb', line 84

def container?
  !@container.nil?
end

#deleteObject

Delete the container



54
55
56
57
58
59
60
61
62
63
# File 'lib/elders/task.rb', line 54

def delete
  # Delete the containe
  res = container.delete force: true

  # Clean it
  @container = nil if res == nil

  # Return the result
  res
end

#error?Boolean

Error in running the task?

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/elders/task.rb', line 89

def error?
  # Check if the task is completed
  return nil unless completed?

  # Check the task status code
  @promise.value['StatusCode'] > 0
end

#logsObject

Task logs



49
50
51
# File 'lib/elders/task.rb', line 49

def logs
  container.logs stdout: true
end

#start(params = nil, env = nil) ⇒ Object

Start the task Shellwords.escape

Parameters:

  • params (String) (defaults to: nil)
    • Params for the task/command



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elders/task.rb', line 25

def start(params = nil, env = nil)
  # Clean
  clean

  # Add params to the command
  command = @command
  command = "#{command} #{params}" unless params.nil?

  # Create the container
  @container = Docker::Container.create 'Image' => @image_name, 'Cmd' => Shellwords.split(command), 'Env' => env

  # Start it
  @container.start

  # Wait for the container to end
  @promise = Concurrent::Promise.execute { @container.wait }
end

#stopObject

Stop the task



44
45
46
# File 'lib/elders/task.rb', line 44

def stop
  container.stop
end

#success?Boolean

Check if the task was a success

Returns:

  • (Boolean)


103
104
105
# File 'lib/elders/task.rb', line 103

def success?
  completed? && !error?
end