Class: Kernai::Task
- Inherits:
-
Object
- Object
- Kernai::Task
- Defined in:
- lib/kernai/plan.rb
Overview
A single executable unit within a plan. Each task is delegated to a sub-agent by the TaskScheduler.
Constant Summary collapse
- STATUSES =
%i[pending running done].freeze
Instance Attribute Summary collapse
-
#depends_on ⇒ Object
Returns the value of attribute depends_on.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#parallel ⇒ Object
Returns the value of attribute parallel.
-
#result ⇒ Object
Returns the value of attribute result.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #done? ⇒ Boolean
-
#initialize(id:, input:, parallel: false, depends_on: [], status: :pending) ⇒ Task
constructor
A new instance of Task.
- #parallel? ⇒ Boolean
- #pending? ⇒ Boolean
- #running? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(id:, input:, parallel: false, depends_on: [], status: :pending) ⇒ Task
Returns a new instance of Task.
161 162 163 164 165 166 167 168 |
# File 'lib/kernai/plan.rb', line 161 def initialize(id:, input:, parallel: false, depends_on: [], status: :pending) @id = id @input = input @parallel = parallel == true @depends_on = depends_on || [] @status = status @result = nil end |
Instance Attribute Details
#depends_on ⇒ Object
Returns the value of attribute depends_on.
132 133 134 |
# File 'lib/kernai/plan.rb', line 132 def depends_on @depends_on end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
131 132 133 |
# File 'lib/kernai/plan.rb', line 131 def id @id end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
131 132 133 |
# File 'lib/kernai/plan.rb', line 131 def input @input end |
#parallel ⇒ Object
Returns the value of attribute parallel.
132 133 134 |
# File 'lib/kernai/plan.rb', line 132 def parallel @parallel end |
#result ⇒ Object
Returns the value of attribute result.
132 133 134 |
# File 'lib/kernai/plan.rb', line 132 def result @result end |
#status ⇒ Object
Returns the value of attribute status.
132 133 134 |
# File 'lib/kernai/plan.rb', line 132 def status @status end |
Class Method Details
.from_hash(hash) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/kernai/plan.rb', line 135 def from_hash(hash) return nil unless hash.is_a?(Hash) id = hash['id'] input = hash['input'] return nil unless id.is_a?(String) && !id.strip.empty? return nil unless input.is_a?(String) && !input.strip.empty? new( id: id, input: input, parallel: hash['parallel'] == true, depends_on: Array(hash['depends_on']).select { |d| d.is_a?(String) && !d.empty? }, status: normalize_status(hash['status']) ) end |
.normalize_status(value) ⇒ Object
153 154 155 156 157 158 |
# File 'lib/kernai/plan.rb', line 153 def normalize_status(value) return :pending if value.nil? sym = value.to_s.to_sym STATUSES.include?(sym) ? sym : :pending end |
Instance Method Details
#done? ⇒ Boolean
178 179 180 |
# File 'lib/kernai/plan.rb', line 178 def done? @status == :done end |
#parallel? ⇒ Boolean
182 183 184 |
# File 'lib/kernai/plan.rb', line 182 def parallel? @parallel end |
#pending? ⇒ Boolean
170 171 172 |
# File 'lib/kernai/plan.rb', line 170 def pending? @status == :pending end |
#running? ⇒ Boolean
174 175 176 |
# File 'lib/kernai/plan.rb', line 174 def running? @status == :running end |
#to_h ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/kernai/plan.rb', line 186 def to_h h = { id: @id, input: @input, parallel: @parallel, depends_on: @depends_on, status: @status.to_s } h[:result] = @result unless @result.nil? h end |