local shell = require "resty.auto-ssl.vendor.shell"
local start_sockproc = require "resty.auto-ssl.utils.start_sockproc"

return function(command)
  -- Make sure the sockproc has started before trying to execute any commands
  -- (since it's started by only a single worker in init_worker, it's possible
  -- other workers have already finished their init_worker phases before the
  -- process is actually started).
  if not ngx.shared.auto_ssl:get("sockproc_started") then
    start_sockproc()

    local wait_time = 0
    local sleep_time = 0.01
    local max_time = 5
    while not ngx.shared.auto_ssl:get("sockproc_started") do
      ngx.sleep(sleep_time)
      wait_time = wait_time + sleep_time

      if wait_time > max_time then
        break
      end
    end
  end

  local status, out, err = shell.execute(command)

  -- If the script fails due to a missing sockproc socket, try starting up
  -- the sockproc process again and then retry.
  if status ~= 0 and err == "no such file or directory" then
    ngx.log(ngx.ERR, "auto-ssl: sockproc unexpectedly not available, trying to restart")
    start_sockproc(true)
    status, out, err = shell.execute(command)
  end

  return status, out, err
end

local shell = require "resty.auto-ssl.vendor.shell"
local start_sockproc = require "resty.auto-ssl.utils.start_sockproc"

return function(command)
  -- Make sure the sockproc has started before trying to execute any commands
  -- (since it's started by only a single worker in init_worker, it's possible
  -- other workers have already finished their init_worker phases before the
  -- process is actually started).
  if not ngx.shared.auto_ssl:get("sockproc_started") then
    start_sockproc()

    local wait_time = 0
    local sleep_time = 0.01
    local max_time = 5
    while not ngx.shared.auto_ssl:get("sockproc_started") do
      ngx.sleep(sleep_time)
      wait_time = wait_time + sleep_time

      if wait_time > max_time then
        break
      end
    end
  end

  local status, out, err = shell.execute(command)

  -- If the script fails due to a missing sockproc socket, try starting up
  -- the sockproc process again and then retry.
  if status ~= 0 and err == "no such file or directory" then
    ngx.log(ngx.ERR, "auto-ssl: sockproc unexpectedly not available, trying to restart")
    start_sockproc(true)
    status, out, err = shell.execute(command, { timeout = 60 })
  end

  return status, out, err
end

Increase default timeout for calling letsencrypt.sh to 60 seconds.
Increase default timeout for calling letsencrypt.sh to 60 seconds.

This attempts to workaround potential slowdowns on Let's Encrypt's end
without dropping the initial request while registering the cert.

The previous default timeout builtin to resty-shell was 15 seconds.
Hopefully certificate registrations won't actually take that long, but
this should hopefully help with random slowness (or at least provide a
better error message from letsencrypt.sh, rather than timing out while
the letsencrypt.sh script continues to run).

See: https://github.com/GUI/lua-resty-auto-ssl/issues/11
