tools: add v retry apt update, intended to replace the retry.sh script, for more robust CI jobs (#21104)

This commit is contained in:
Delyan Angelov 2024-03-26 17:44:07 +02:00 committed by GitHub
parent 38d73d303b
commit 992334390c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 0 deletions

57
cmd/tools/vretry.v Normal file
View File

@ -0,0 +1,57 @@
import os
import time
import flag
struct Context {
mut:
show_help bool
timeout time.Duration
delay time.Duration
retries int
}
fn main() {
mut context := Context{}
mut fp := flag.new_flag_parser(os.args#[1..])
fp.application('v retry')
fp.version('0.0.1')
fp.description('Run the command CMD in a loop, untill it succeeds, or until a predetermined amount of seconds pass.')
fp.arguments_description('CMD')
fp.skip_executable()
fp.limit_free_args_to_at_least(1)!
context.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
context.timeout = fp.float('timeout', `t`, 300.0, 'Timeout in seconds. Default: 300.0 seconds.') * time.second
context.delay = fp.float('delay', `d`, 1.0, 'Delay between each retry in seconds. Default: 1.0 second.') * time.second
context.retries = fp.int('retries', `r`, 999_999_999, 'Maximum number of retries. Default: 999_999_999.')
if context.show_help {
println(fp.usage())
exit(0)
}
command_args := fp.finalize() or {
eprintln('Error: ${err}')
exit(1)
}
cmd := command_args.join(' ')
spawn fn (context Context) {
time.sleep(context.timeout)
eprintln('error: exceeded maximum timeout (${context.timeout.seconds()}s)!')
exit(1)
}(context)
mut res := 0
mut i := 0
for {
i++
res = os.system(cmd)
if res == 0 {
break
}
if i >= context.retries {
eprintln('error: exceeded maximum number of retries (${context.retries})!')
exit(res)
}
time.sleep(context.delay)
}
exit(res)
}

View File

@ -31,6 +31,7 @@ const external_tools = [
'missdoc',
'repl',
'repeat',
'retry',
'self',
'setup-freetype',
'shader',