Module: Polyrun::Hooks::WorkerShell

Included in:
Polyrun::Hooks
Defined in:
lib/polyrun/hooks/worker_shell.rb

Overview

Builds sh -c script for worker processes (shell + Ruby before_worker / after_worker).

Instance Method Summary collapse

Instance Method Details

#build_worker_shell_script(cmd, paths) ⇒ String

rubocop:disable Metrics/AbcSize – shell + ruby worker hook branches

Parameters:

  • cmd (Array<String>)

    argv before paths

  • paths (Array<String>)

Returns:

  • (String)

    shell script body for sh -c (worker process)



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
# File 'lib/polyrun/hooks/worker_shell.rb', line 12

def build_worker_shell_script(cmd, paths)
  main = Shellwords.join(cmd + paths)
  rb = RbConfig.ruby
  bw_shell = commands_for(:before_worker)
  aw_shell = commands_for(:after_worker)
  bw_ruby = ruby_registry&.any?(:before_worker)
  aw_ruby = ruby_registry&.any?(:after_worker)

  lines = []
  lines << "export POLYRUN_HOOK_PHASE=before_worker"
  if bw_ruby || bw_shell.any?
    lines << "set -e"
    lines << worker_ruby_line(rb, :before_worker) if bw_ruby
    bw_shell.each { |c| lines << c }
  end
  lines << "set +e"
  lines << main
  lines << "ec=$?"
  lines << "export POLYRUN_HOOK_PHASE=after_worker"
  if aw_ruby || aw_shell.any?
    lines << "set +e"
    aw_shell.each { |c| lines << "( #{c} ) || true" }
    lines << worker_ruby_line(rb, :after_worker, wrap_allow_fail: true) if aw_ruby
  end
  lines << "exit $ec"
  lines.join("\n")
end