Compare commits

...

3 Commits

5 changed files with 69 additions and 2 deletions

View File

@ -4,7 +4,7 @@ import models { GitRepo, GitRepoFilter }
import net.http { Method }
import response { Response }
// get_git_repos returns the current list of repos.
// get_git_repos returns a list of GitRepo's, given a filter object.
pub fn (c &Client) get_git_repos(filter GitRepoFilter) ?[]GitRepo {
params := models.params_from(filter)
data := c.send_request<[]GitRepo>(Method.get, '/api/repos', params)?
@ -12,6 +12,27 @@ pub fn (c &Client) get_git_repos(filter GitRepoFilter) ?[]GitRepo {
return data.data
}
// get_all_git_repos retrieves *all* GitRepo's from the API using the default
// limit.
pub fn (c &Client) get_all_git_repos() ?[]GitRepo {
mut repos := []GitRepo{}
mut offset := u64(0)
for {
sub_repos := c.get_git_repos(offset: offset)?
if sub_repos.len == 0 {
break
}
repos << sub_repos
offset += u64(sub_repos.len)
}
return repos
}
// get_git_repo returns the repo for a specific ID.
pub fn (c &Client) get_git_repo(id int) ?GitRepo {
data := c.send_request<GitRepo>(Method.get, '/api/repos/$id', {})?

View File

@ -0,0 +1,30 @@
module schedule
import cli
import cron.expression { parse_expression }
import time
// cmd returns the cli submodule for previewing a cron schedule.
pub fn cmd() cli.Command {
return cli.Command{
name: 'schedule'
usage: 'schedule'
description: 'Preview the behavior of a cron schedule.'
flags: [
cli.Flag{
name: 'count'
description: 'How many scheduled times to show.'
flag: cli.FlagType.int
default_value: ['5']
},
]
execute: fn (cmd cli.Command) ? {
ce := parse_expression(cmd.args.join(' '))?
count := cmd.flags.get_int('count')?
for t in ce.next_n(time.now(), count)? {
println(t)
}
}
}
}

View File

@ -178,7 +178,7 @@ fn (mut d Daemon) schedule_build(repo GitRepo) {
fn (mut d Daemon) renew_repos() {
d.linfo('Renewing repos...')
mut new_repos := d.client.get_git_repos() or {
mut new_repos := d.client.get_all_git_repos() or {
d.lerror('Failed to renew repos. Retrying in ${daemon.api_update_retry_timeout}s...')
d.api_update_timestamp = time.now().add_seconds(daemon.api_update_retry_timeout)

View File

@ -121,6 +121,20 @@ pub fn (ce &CronExpression) next_from_now() ?time.Time {
return ce.next(time.now())
}
// next_n returns the n next occurences of the expression, given a starting
// time.
pub fn (ce &CronExpression) next_n(ref time.Time, n int) ?[]time.Time {
mut times := []time.Time{cap: n}
times << ce.next(ref)?
for i in 1 .. n {
times << ce.next(times[i - 1])?
}
return times
}
// parse_range parses a given string into a range of sorted integers, if
// possible.
fn parse_range(s string, min int, max int, mut bitv []bool) ? {

View File

@ -5,6 +5,7 @@ import server
import cli
import console.git
import console.logs
import console.schedule
import cron
fn main() {
@ -27,6 +28,7 @@ fn main() {
git.cmd(),
cron.cmd(),
logs.cmd(),
schedule.cmd(),
]
}
app.setup()