Class: Elders::Task
- Inherits:
-
Object
- Object
- Elders::Task
- Defined in:
- lib/elders/task.rb
Constant Summary collapse
- TIMEOUT =
Task timeout
1500
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Attributes.
-
#name ⇒ Object
readonly
Attributes.
-
#promise ⇒ Object
readonly
Attributes.
Instance Method Summary collapse
-
#clean ⇒ Object
Clean the task.
-
#completed? ⇒ Boolean
Completed the task?.
-
#container ⇒ Object
Get the task container.
-
#container? ⇒ Boolean
Check if the container exists.
-
#delete ⇒ Object
Delete the container.
-
#error? ⇒ Boolean
Error in running the task?.
-
#initialize(name, image, command) ⇒ Task
constructor
Creates a new task, but not start it.
-
#logs ⇒ Object
Task logs.
-
#start(params = nil, env = nil) ⇒ Object
Start the task Shellwords.escape.
-
#stop ⇒ Object
Stop the task.
-
#success? ⇒ Boolean
Check if the task was a success.
Constructor Details
#initialize(name, image, command) ⇒ Task
Creates a new task, but not start it
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
#command ⇒ Object (readonly)
Attributes
7 8 9 |
# File 'lib/elders/task.rb', line 7 def command @command end |
#name ⇒ Object (readonly)
Attributes
7 8 9 |
# File 'lib/elders/task.rb', line 7 def name @name end |
#promise ⇒ Object (readonly)
Attributes
7 8 9 |
# File 'lib/elders/task.rb', line 7 def promise @promise end |
Instance Method Details
#clean ⇒ Object
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?
101 102 103 |
# File 'lib/elders/task.rb', line 101 def completed? @promise.fulfilled? end |
#container ⇒ Object
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
87 88 89 |
# File 'lib/elders/task.rb', line 87 def container? !@container.nil? end |
#delete ⇒ Object
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?
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 |
#logs ⇒ Object
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
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 |
#stop ⇒ Object
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
106 107 108 |
# File 'lib/elders/task.rb', line 106 def success? completed? && !error? end |