nvim-lua / plenary.nvim

plenary: full; complete; entire; absolute; unqualified. All the lua functions I don't want to write twice.
MIT License
2.74k stars 290 forks source link

[Feature Request]Job timeout #482

Open sys9kdr opened 1 year ago

sys9kdr commented 1 year ago

Commands in plenary.nvim's job sometimes take a long time to resolve, which can be frustrating for users. To address this, I propose adding a timeout option to the plugin's job feature. This would allow users to set a timeout for commands and automatically quit the command with an error message if it takes too long to execute.

local Job = require'plenary.job'

Job:new({
  command = 'someTooLongTimeCommand',
  args = { '--files' },
  cwd = '/usr/bin',
  env = { ['a'] = 'b' },
  on_exit = function(j, return_val)
    print(return_val)
    print(j:result())
  end,
  timeout = 5000 -- timout with ms
}):sync() -- or start()
miversen33 commented 1 year ago

FWIW, I have implemented this in my abstraction of spawn. Feel free to yoink this for plenary. Its basically just making everything async and "waiting" until a vim.loop.timer fires to kill the running job if its still running.

sys9kdr commented 1 year ago

In my case, I use a wrapper to kill jobs by timeout.

wrapFunc = function(someFuncReturnPid, timeout)
    timeout = timeout or 5000
    local pid = someFuncReturnPid
    local timer = vim.loop.new_timer()
    timer:start(timeout, 0, function()
        vim.loop.kill(pid)
        print('job timeout')
    end)
end

I think that wrapper approach is redundant. So I want a timeout option.

Conni2461 commented 1 year ago

Job:sync already has a timeout: https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/job.lua#L451

sys9kdr commented 1 year ago

Wouldn't it be more convenient to have timout in the job itself? I don't like using sync because of waiting time.