Class: Kernai::Task

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_onObject

Returns the value of attribute depends_on.



132
133
134
# File 'lib/kernai/plan.rb', line 132

def depends_on
  @depends_on
end

#idObject (readonly)

Returns the value of attribute id.



131
132
133
# File 'lib/kernai/plan.rb', line 131

def id
  @id
end

#inputObject (readonly)

Returns the value of attribute input.



131
132
133
# File 'lib/kernai/plan.rb', line 131

def input
  @input
end

#parallelObject

Returns the value of attribute parallel.



132
133
134
# File 'lib/kernai/plan.rb', line 132

def parallel
  @parallel
end

#resultObject

Returns the value of attribute result.



132
133
134
# File 'lib/kernai/plan.rb', line 132

def result
  @result
end

#statusObject

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

Returns:

  • (Boolean)


178
179
180
# File 'lib/kernai/plan.rb', line 178

def done?
  @status == :done
end

#parallel?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/kernai/plan.rb', line 182

def parallel?
  @parallel
end

#pending?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/kernai/plan.rb', line 170

def pending?
  @status == :pending
end

#running?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/kernai/plan.rb', line 174

def running?
  @status == :running
end

#to_hObject



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