Module: Textus::Infra::Refresh::Detached

Defined in:
lib/textus/infra/refresh/detached.rb

Class Method Summary collapse

Class Method Details

.spawn(store_root:, key:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/textus/infra/refresh/detached.rb', line 11

def spawn(store_root:, key:)
  return nil unless supported?

  pid = Process.fork do
    $stdin.close
    $stdout.reopen(File::NULL, "w")
    $stderr.reopen(File::NULL, "w")

    lock = Textus::Infra::Refresh::Lock.new(root: store_root, key: key)
    exit(0) unless lock.try_acquire

    begin
      store = Textus::Store.new(store_root)
      Textus::Refresh.call(store, key, as: "script")
    rescue StandardError
      # Already logged via :refresh_failed; exit cleanly.
    ensure
      lock.release
      exit(0)
    end
  end
  Process.detach(pid)
  pid
end

.supported?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/textus/infra/refresh/detached.rb', line 7

def supported?
  Process.respond_to?(:fork)
end