Module: Pytty::Daemon::Components::Handler

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

Class Method Summary collapse

Class Method Details

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



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pytty/daemon/components/handler.rb', line 9

def self.handle(component:, id:, params:)
  return case component
  when "signal"
    process_yield = Pytty::Daemon.yields[id]
    return [404, "not found"] unless process_yield
    return [500, "not running"] unless process_yield.running?
    p params
    process_yield.signal params["signal"]
    [200, "ok"]
  when "spawn"
    process_yield = Pytty::Daemon.yields[id]
    return [404, "not found"] unless process_yield

    process_yield.spawn
    Pytty::Daemon.dump

    [200, "ok"]
  when "ps"
    output = []
    Pytty::Daemon.yields.each do |id, process_yield|
      output << process_yield
    end
    [200, output]
  when "yield"
    cmd = params.dig "cmd"
    env = params.dig "env"
    process_yield = Pytty::Daemon::ProcessYield.new cmd, env: env
    Pytty::Daemon.yields[process_yield.id] = process_yield
    Pytty::Daemon.dump

    [200, process_yield]
  when "rm"
    process_yield = Pytty::Daemon.yields[id]
    p Pytty::Daemon.yields
    p id
    return [404, "not found"] unless process_yield
    if process_yield.running?
      process_yield.kill
    end
    Pytty::Daemon.yields.delete process_yield.id
    Pytty::Daemon.dump

    [200, nil]
  when "spawn"
    process_yield = Pytty::Daemon.yields[id]
    return [404, "not found"] unless process_yield

    pipe = IO.pipe
    stderr_reader = Async::IO::Generic.new(pipe.first)
    stderr_writer = Async::IO::Generic.new(pipe.last)

    process_yield.spawn stdout: $stdout, stderr: stderr_writer, stdin: $stdin
    stderr_reader.close
    [200, process_yield]
  else
    raise "unknown: #{component}"
  end
end