Skip to content
Snippets Groups Projects
Commit 47b473e4 authored by Luke Duncalfe's avatar Luke Duncalfe :two:
Browse files

Merge branch 'timeout-for-rest2html' into 'master'

Wrap call to rest2html in a timeout

See merge request !33
parents 162f61d5 56780b75
No related branches found
No related tags found
1 merge request!33Wrap call to rest2html in a timeout
Pipeline #420603344 passed
......@@ -12,6 +12,8 @@ module GitHub
end
class CommandImplementation < Implementation
DEFAULT_GITLAB_MARKUP_TIMEOUT = '10'.freeze
attr_reader :command, :block, :name
def initialize(regexp, command, name, &block)
......@@ -27,7 +29,8 @@ module GitHub
call_block(rendered, content)
end
private
private
def call_block(rendered, content)
if block && block.arity == 2
block.call(rendered, content)
......@@ -38,20 +41,41 @@ module GitHub
end
end
def timeout_in_seconds
ENV.fetch('GITLAB_MARKUP_TIMEOUT', DEFAULT_GITLAB_MARKUP_TIMEOUT)
end
def prepend_command_timeout_prefix(command)
timeout_command_prefix = "timeout --signal=KILL #{timeout_in_seconds}"
# Preserve existing support for command being either a String or an Array
if command.is_a?(String)
"#{timeout_command_prefix} #{command}"
else
timeout_command_prefix.split(' ') + command
end
end
if defined?(POSIX::Spawn)
def execute(command, target)
spawn = POSIX::Spawn::Child.new(*command, :input => target)
command_with_timeout_prefix = prepend_command_timeout_prefix(command)
spawn = POSIX::Spawn::Child.new(*command_with_timeout_prefix, :input => target)
if spawn.status.success?
sanitize(spawn.out, target.encoding)
elsif spawn.status.termsig == Signal.list['KILL']
raise TimeoutError.new("Command was killed, probably due to exceeding GITLAB_MARKUP_TIMEOUT limit of #{timeout_in_seconds} seconds")
else
raise CommandError.new(spawn.err.strip)
end
end
else
def execute(command, target)
stdout_str, stderr_str, status = Open3.capture3(*command, stdin_data: target)
command_with_timeout_prefix = prepend_command_timeout_prefix(command)
stdout_str, stderr_str, status = Open3.capture3(*command_with_timeout_prefix, stdin_data: target)
if status.success?
sanitize(stdout_str, target.encoding)
elsif status.termsig == Signal.list['KILL']
raise TimeoutError.new("Command was killed, probably due to exceeding GITLAB_MARKUP_TIMEOUT limit of #{timeout_in_seconds} seconds")
else
raise CommandError.new(stderr_str.strip)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment