Module: Textus::Ports::Fetch::Detached

Defined in:
lib/textus/ports/fetch/detached.rb

Class Method Summary collapse

Class Method Details

.acting_role(store) ⇒ Object



11
12
13
# File 'lib/textus/ports/fetch/detached.rb', line 11

def acting_role(store)
  store.manifest.policy.actor_for("fetch")
end

.spawn(store_root:, key:) ⇒ Object



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
# File 'lib/textus/ports/fetch/detached.rb', line 15

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::Ports::Fetch::Lock.new(root: store_root, key: key)
    exit(0) unless lock.try_acquire

    begin
      store = Textus::Store.new(store_root)
      # No fetch-holder configured — exit the child cleanly. In practice
      # this is unreachable: the background fork only happens after a
      # foreground fetch was already authorized (so a fetch-holder
      # exists). Config-time detection is doctor's job (ADR 0044 Q2).
      role = acting_role(store)
      exit(0) unless role
      store.as(role).fetch(key)
    rescue StandardError
      # Already logged via :fetch_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/ports/fetch/detached.rb', line 7

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