Make the function for waiting for jobs to complete more reliable.

This commit is contained in:
w0rp 2016-10-17 23:43:31 +01:00
parent bf45ab6d8d
commit 02e42feeaa

View file

@ -262,7 +262,12 @@ endfunction
" The time taken will be a very rough approximation, and more time may be " The time taken will be a very rough approximation, and more time may be
" permitted than is specified. " permitted than is specified.
function! ale#engine#WaitForJobs(deadline) abort function! ale#engine#WaitForJobs(deadline) abort
let l:time_ticked = 0 let l:start_time = system('date +%s%3N') + 0
if l:start_time == 0
throw 'Failed to read milliseconds from the clock!'
endif
let l:job_list = [] let l:job_list = []
for l:job_id in keys(s:job_info_map) for l:job_id in keys(s:job_info_map)
@ -276,17 +281,24 @@ function! ale#engine#WaitForJobs(deadline) abort
for l:job in l:job_list for l:job in l:job_list
if job_status(l:job) ==# 'run' if job_status(l:job) ==# 'run'
if l:time_ticked > a:deadline let l:now = system('date +%s%3N') + 0
if l:now - l:start_time > a:deadline
" Stop waiting after a timeout, so we don't wait forever. " Stop waiting after a timeout, so we don't wait forever.
throw 'Jobs did not complete on time!' throw 'Jobs did not complete on time!'
endif endif
" Wait another 10 milliseconds " Wait another 10 milliseconds
let l:time_ticked += 10
let l:should_wait_more = 1 let l:should_wait_more = 1
sleep 10ms sleep 10ms
break break
endif endif
endfor endfor
endwhile endwhile
" Sleep for a small amount of time after all jobs finish.
" This seems to be enough to let handlers after jobs end run, and
" prevents the occasional failure where this function exits after jobs
" end, but before handlers are run.
sleep 10ms
endfunction endfunction