Class: LanguageOperator::Agent::ExecutionState

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/agent/execution_state.rb

Overview

Execution State Manager

Manages the execution state of agents to prevent concurrent executions. Thread-safe implementation ensures only one execution runs at a time.

Examples:

Check if execution is running

state = ExecutionState.new
state.running? # => false

Start an execution

state.start_execution('exec-123')
state.running? # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExecutionState

Initialize a new execution state



21
22
23
24
25
26
# File 'lib/language_operator/agent/execution_state.rb', line 21

def initialize
  @mutex = Mutex.new
  @current_execution_id = nil
  @started_at = nil
  @status = :idle # :idle, :running, :completed, :failed
end

Instance Attribute Details

#current_execution_idObject (readonly)

Returns the value of attribute current_execution_id.



18
19
20
# File 'lib/language_operator/agent/execution_state.rb', line 18

def current_execution_id
  @current_execution_id
end

#started_atObject (readonly)

Returns the value of attribute started_at.



18
19
20
# File 'lib/language_operator/agent/execution_state.rb', line 18

def started_at
  @started_at
end

#statusObject (readonly)

Returns the value of attribute status.



18
19
20
# File 'lib/language_operator/agent/execution_state.rb', line 18

def status
  @status
end

Instance Method Details

#complete_execution(result = nil) ⇒ Object

Mark execution as completed

Parameters:

  • result (Object) (defaults to: nil)

    Optional execution result

Returns:

  • (Object)

    The result passed in



50
51
52
53
54
55
# File 'lib/language_operator/agent/execution_state.rb', line 50

def complete_execution(result = nil)
  @mutex.synchronize do
    @status = :completed
    result
  end
end

#current_infoHash

Get current execution information

Returns:

  • (Hash)

    Current execution state details



78
79
80
81
82
83
84
85
86
# File 'lib/language_operator/agent/execution_state.rb', line 78

def current_info
  @mutex.synchronize do
    {
      status: @status,
      execution_id: @current_execution_id,
      started_at: @started_at
    }
  end
end

#fail_execution(error) ⇒ void

This method returns an undefined value.

Mark execution as failed

Parameters:

  • error (StandardError)

    The error that caused the failure



61
62
63
64
65
66
# File 'lib/language_operator/agent/execution_state.rb', line 61

def fail_execution(error)
  @mutex.synchronize do
    @status = :failed
    @last_error = error
  end
end

#running?Boolean

Check if an execution is currently running

Returns:

  • (Boolean)

    true if execution is in progress



71
72
73
# File 'lib/language_operator/agent/execution_state.rb', line 71

def running?
  @mutex.synchronize { @status == :running }
end

#start_execution(execution_id) ⇒ void

This method returns an undefined value.

Start a new execution

Parameters:

  • execution_id (String)

    Unique identifier for the execution

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/language_operator/agent/execution_state.rb', line 33

def start_execution(execution_id)
  @mutex.synchronize do
    if @status == :running
      raise ExecutionInProgressError,
            "Execution #{@current_execution_id} already running"
    end

    @current_execution_id = execution_id
    @started_at = Time.now
    @status = :running
  end
end