Class: Phronomy::Persistence::InMemory::Executions

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/persistence/in_memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ Executions

Returns a new instance of Executions.



139
# File 'lib/phronomy/persistence/in_memory.rb', line 139

def initialize(owner) = @owner = owner

Instance Method Details

#assert_idle!(agent_id) ⇒ Object

Raises AgentBusyError if there is an active execution for agent_id. Must be called from within a transaction (monitor already held).



204
205
206
207
208
209
# File 'lib/phronomy/persistence/in_memory.rb', line 204

def assert_idle!(agent_id)
  active = @owner.state[:executions].values.find do |candidate|
    candidate.agent_id == agent_id.to_s && candidate.active?
  end
  raise Phronomy::AgentBusyError, "agent has an active or suspended execution: #{agent_id}" if active
end

#create_active(execution) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/phronomy/persistence/in_memory.rb', line 141

def create_active(execution)
  @owner.synchronize do
    if @owner.state[:executions].key?(execution.execution_id.to_s)
      raise ConflictError, "execution already exists: #{execution.execution_id}"
    end
    active = @owner.state[:executions].values.find do |candidate|
      candidate.agent_id == execution.agent_id && candidate.active?
    end
    raise Phronomy::AgentBusyError, "agent is busy: #{execution.agent_id}" if active
    @owner.state[:executions][execution.execution_id] = execution
  end
  execution
end

#delete(execution_id) ⇒ Object



192
193
194
# File 'lib/phronomy/persistence/in_memory.rb', line 192

def delete(execution_id)
  @owner.synchronize { @owner.state[:executions].delete(execution_id.to_s) }
end

#delete_for_agent(agent_id) ⇒ Object



196
197
198
199
200
# File 'lib/phronomy/persistence/in_memory.rb', line 196

def delete_for_agent(agent_id)
  @owner.synchronize do
    @owner.state[:executions].delete_if { |_id, execution| execution.agent_id == agent_id.to_s }
  end
end

#list_active(agent_id) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/phronomy/persistence/in_memory.rb', line 184

def list_active(agent_id)
  @owner.synchronize do
    @owner.state[:executions].values.select do |execution|
      execution.agent_id == agent_id.to_s && execution.active?
    end.freeze
  end
end

#load(execution_id) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/phronomy/persistence/in_memory.rb', line 155

def load(execution_id)
  @owner.synchronize do
    @owner.state[:executions].fetch(execution_id.to_s) do
      raise NotFoundError, "execution not found: #{execution_id}"
    end
  end
end

#save(execution_id, expected_revision:, execution:) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/phronomy/persistence/in_memory.rb', line 163

def save(execution_id, expected_revision:, execution:)
  @owner.synchronize do
    current = load(execution_id)
    unless current.execution_revision == expected_revision
      raise ConflictError,
        "execution revision conflict: expected #{expected_revision}, actual #{current.execution_revision}"
    end
    unless execution.execution_id.to_s == execution_id.to_s
      raise ConflictError,
        "Execution identity mismatch: #{execution.execution_id} != #{execution_id}"
    end
    unless execution.execution_revision == expected_revision + 1
      raise ConflictError,
        "execution save must advance revision exactly once: " \
        "expected #{expected_revision + 1}, got #{execution.execution_revision}"
    end
    @owner.state[:executions][execution_id.to_s] = execution
  end
  execution
end