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



69
70
71
72
73
74
75
# File 'lib/elders/task.rb', line 69

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

  # Delete the container
  delete == nil
end

#completed?Boolean

Completed the task?

Returns:

  • (Boolean)


101
102
103
# File 'lib/elders/task.rb', line 101

def completed?
  @promise.fulfilled?
end

#containerObject

Get the task container



78
79
80
81
82
83
84
# File 'lib/elders/task.rb', line 78

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)


87
88
89
# File 'lib/elders/task.rb', line 87

def container?
  !@container.nil?
end

#deleteObject

Delete the container



57
58
59
60
61
62
63
64
65
66
# File 'lib/elders/task.rb', line 57

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)


92
93
94
95
96
97
98
# File 'lib/elders/task.rb', line 92

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



52
53
54
# File 'lib/elders/task.rb', line 52

def logs
  container.logs stdout: true, stderr: 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
42
43
44
# 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,
                                        'Tty'   => true

  # Start it
  @container.start

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

#stopObject

Stop the task



47
48
49
# File 'lib/elders/task.rb', line 47

def stop
  container.stop
end

#success?Boolean

Check if the task was a success

Returns:

  • (Boolean)


106
107
108
# File 'lib/elders/task.rb', line 106

def success?
  completed? && !error?
end