Module: Pytty::Daemon::Components::YieldHandler

Defined in:
lib/pytty/daemon/components/yield_handler.rb

Class Method Summary collapse

Class Method Details

.handle(component:, id:, params:) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pytty/daemon/components/yield_handler.rb', line 7

def self.handle(component:, id:, params:)
  process_yield = Pytty::Daemon.yields[id]
  return [404, "not found"] unless process_yield

  return case component
  when "stdin"
    process_yield.stdin.enqueue params["c"]

    [200, "ok"]
  when "signal"
    return [500, "not running"] unless process_yield.running?

    process_yield.signal params["signal"]

    [200, "ok"]
  when "spawn"
    return [500, "already running"] if process_yield.running?

    if process_yield.spawn
      Pytty::Daemon.dump
    else
      return [500, "could not spawn"]
    end

    [200, "ok"]
  when "rm"
    process_yield.signal("KILL") if process_yield.running?
    Pytty::Daemon.yields.delete process_yield.id
    Pytty::Daemon.dump

    [200, id]
  else
    raise "unknown: #{component} with id: #{id}"
  end
end