Skip to content
Kward Search API index

Class: Kward::Hooks::CommandHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/hooks/command_handler.rb

Overview

Executes an external command hook using JSON over stdin/stdout.

Constant Summary collapse

DEFAULT_TIMEOUT_SECONDS =
10

Instance Method Summary collapse

Constructor Details

#initialize(command:, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, env: nil, failure_policy: Catalog::DEFAULT_FAILURE_POLICY) ⇒ CommandHandler

Returns a new instance of CommandHandler.



15
16
17
18
19
20
# File 'lib/kward/hooks/command_handler.rb', line 15

def initialize(command:, timeout_seconds: DEFAULT_TIMEOUT_SECONDS, env: nil, failure_policy: Catalog::DEFAULT_FAILURE_POLICY)
  @command = command.to_s
  @timeout_seconds = positive_timeout(timeout_seconds)
  @env = stringify_hash(env || {})
  @failure_policy = Catalog.normalize_failure_policy(failure_policy)
end

Instance Method Details

#call(event, _context = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/kward/hooks/command_handler.rb', line 22

def call(event, _context = nil)
  stdout, stderr, status = run_command(JSON.dump(event.to_h))
  return failure_decision("Command hook failed: #{stderr.strip.empty? ? "exit #{status.exitstatus}" : stderr.strip}") unless status.success?

  parse_decision(stdout)
rescue Timeout::Error
  failure_decision("Command hook timed out after #{@timeout_seconds}s")
rescue StandardError => e
  failure_decision("Command hook failed: #{e.message}")
end