Class: Pi::Browser::Taskbar::Rails::Broker::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/pi/browser/taskbar/rails/broker.rb

Instance Method Summary collapse

Constructor Details

#initialize(project_root:, executable:, task_timeout: 1_800, abort_timeout: DEFAULT_ABORT_TIMEOUT, termination_timeout: DEFAULT_TERMINATION_TIMEOUT, max_restart_attempts: DEFAULT_RESTART_ATTEMPTS, restart_delay: DEFAULT_RESTART_DELAY) ⇒ Runtime

Returns a new instance of Runtime.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 104

def initialize(project_root:, executable:, task_timeout: 1_800,
  abort_timeout: DEFAULT_ABORT_TIMEOUT,
  termination_timeout: DEFAULT_TERMINATION_TIMEOUT,
  max_restart_attempts: DEFAULT_RESTART_ATTEMPTS,
  restart_delay: DEFAULT_RESTART_DELAY)
  @project_root = project_root
  @executable = executable
  @task_timeout = task_timeout
  @abort_timeout = abort_timeout
  @termination_timeout = termination_timeout
  @max_restart_attempts = max_restart_attempts
  @restart_delay = restart_delay
  @mutex = Mutex.new
  @write_mutex = Mutex.new
  @session_id = nil
  @model = nil
  @error = nil
  @task = nil
  @phase = "starting"
  @reset_condition = ConditionVariable.new
  @reset_result = nil
  @reset_recovering = false
  @restart_attempts = 0
  @closed = false
  start_pi
end

Instance Method Details

#cancel(id) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 170

def cancel(id)
  @mutex.synchronize do
    return [:not_found, snapshot_unlocked] unless @task && @task["id"] == id

    case @task["status"]
    when "running"
      @task["abort_command_id"] = "abort-#{@task["id"]}"
      write(type: "abort", id: @task["abort_command_id"])
      @task["status"] = "cancelling"
      @task["activity"] = "Stopping Pi"
      Thread.new do
        sleep @abort_timeout
        abort_timeout(id)
      end
      [:accepted, snapshot_unlocked]
    when "cancelling"
      [:accepted, snapshot_unlocked]
    when "cancelled"
      [:cancelled, snapshot_unlocked]
    else
      [:not_cancellable, snapshot_unlocked]
    end
  rescue IOError, SystemCallError
    restart_after_protocol_failure_unlocked(
      "Pi stopped unexpectedly", "Pi became unavailable before accepting cancellation"
    )
    [:unavailable, snapshot_unlocked]
  end
end

#closeObject



217
218
219
220
221
222
223
224
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 217

def close
  @mutex.synchronize do
    @closed = true
    stop_pi_unlocked
  end
rescue IOError, SystemCallError
  nil
end

#resetObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 200

def reset
  @mutex.synchronize do
    return [:reset_while_busy, snapshot_unlocked] unless @phase == "ready"

    @phase = "resetting"
    @reset_result = nil
    @reset_command_id = "reset-#{SecureRandom.urlsafe_base64(18, false)}"
    begin
      write(type: "new_session", id: @reset_command_id)
    rescue IOError, SystemCallError
      recover_reset_unlocked
    end
    @reset_condition.wait(@mutex) while @reset_result.nil?
    [@reset_result, snapshot_unlocked]
  end
end

#snapshotObject



131
132
133
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 131

def snapshot
  @mutex.synchronize { snapshot_unlocked }
end

#submit(value) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 139

def submit(value)
  task = Task.parse(value)
  @mutex.synchronize do
    return [:busy, snapshot_unlocked] if @phase == "busy"
    return [:unavailable, snapshot_unlocked] unless @phase == "ready"

    id = SecureRandom.urlsafe_base64(18, false)
    now = Time.now.utc.iso8601(6)
    @task = {
      "id" => id, "prompt" => task.prompt, "status" => "running", "output" => "",
      "output_truncated" => false, "activity" => "Starting Pi", "error" => nil,
      "started_at" => now, "finished_at" => nil, "command_id" => "task-#{id}",
      "prompt_accepted" => false, "pending_error" => nil
    }
    @phase = "busy"
    write(type: "prompt", id: @task["command_id"], message: task.pi_prompt)
    Thread.new do
      sleep @task_timeout
      timeout(id)
    end
    [:accepted, snapshot_unlocked]
  rescue IOError, SystemCallError
    restart_after_protocol_failure_unlocked(
      "Pi stopped unexpectedly", "Pi became unavailable before accepting the task"
    )
    [:unavailable, snapshot_unlocked]
  end
rescue Task::Invalid => error
  [:invalid, error.message]
end

#work_active?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/pi/browser/taskbar/rails/broker.rb', line 135

def work_active?
  @mutex.synchronize { %w[starting busy resetting].include?(@phase) }
end