diff --git a/src/client/git.v b/src/client/git.v index fd14718..4496c08 100644 --- a/src/client/git.v +++ b/src/client/git.v @@ -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(Method.get, '/api/repos/$id', {})? diff --git a/src/console/schedule/schedule.v b/src/console/schedule/schedule.v new file mode 100644 index 0000000..8fceddd --- /dev/null +++ b/src/console/schedule/schedule.v @@ -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) + } + } + } +} diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v index 82c219d..f1206d6 100644 --- a/src/cron/daemon/daemon.v +++ b/src/cron/daemon/daemon.v @@ -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) diff --git a/src/cron/expression/expression.v b/src/cron/expression/expression.v index 5eae332..17d2dde 100644 --- a/src/cron/expression/expression.v +++ b/src/cron/expression/expression.v @@ -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) ? { diff --git a/src/db/db.v b/src/db/db.v index 57b437e..9a06a03 100644 --- a/src/db/db.v +++ b/src/db/db.v @@ -1,19 +1,65 @@ module db import sqlite -import models { BuildLog, GitRepo } struct VieterDb { conn sqlite.DB } +struct MigrationVersion { + id int [primary] + version int +} + +const ( + migrations_up = [$embed_file('migrations/001-initial/up.sql')] + migrations_down = [$embed_file('migrations/001-initial/down.sql')] +) + // init initializes a database & adds the correct tables. pub fn init(db_path string) ?VieterDb { conn := sqlite.connect(db_path)? sql conn { - create table GitRepo - create table BuildLog + create table MigrationVersion + } + + cur_version := sql conn { + select from MigrationVersion limit 1 + } + + // If there's no row yet, we add it here + if cur_version == MigrationVersion{} { + sql conn { + insert cur_version into MigrationVersion + } + } + + // Apply each migration in order + for i in cur_version.version .. db.migrations_up.len { + migration := db.migrations_up[i].to_string() + + version_num := i + 1 + + // vfmt does not like these dots + println('Applying migration $version_num' + '...') + + // The sqlite library seems to not like it when multiple statements are + // passed in a single exec. Therefore, we split them & run them all + // separately. + for part in migration.split(';').map(it.trim_space()).filter(it != '') { + res := conn.exec_none(part) + + if res != sqlite.sqlite_done { + return error('An error occurred while applying migration $version_num') + } + } + + // The where clause doesn't really matter, as there will always only be + // one entry anyways. + sql conn { + update MigrationVersion set version = version_num where id > 0 + } } return VieterDb{ diff --git a/src/db/migrations/001-initial/down.sql b/src/db/migrations/001-initial/down.sql new file mode 100644 index 0000000..37ce683 --- /dev/null +++ b/src/db/migrations/001-initial/down.sql @@ -0,0 +1,3 @@ +DROP TABLE BuildLog; +DROP TABLE GitRepoArch; +DROP TABLE GitRepo; diff --git a/src/db/migrations/001-initial/up.sql b/src/db/migrations/001-initial/up.sql new file mode 100644 index 0000000..ca0aace --- /dev/null +++ b/src/db/migrations/001-initial/up.sql @@ -0,0 +1,22 @@ +CREATE TABLE IF NOT EXISTS GitRepo ( + id INTEGER PRIMARY KEY, + url TEXT NOT NULL, + branch TEXT NOT NULL, + repo TEXT NOT NULL, + schedule TEXT +); + +CREATE TABLE IF NOT EXISTS GitRepoArch ( + id INTEGER PRIMARY KEY, + repo_id INTEGER NOT NULL, + value TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS BuildLog ( + id INTEGER PRIMARY KEY, + repo_id INTEGER NOT NULL, + start_time INTEGER NOT NULL, + end_time iNTEGER NOT NULL, + arch TEXT NOT NULL, + exit_code INTEGER NOT NULL +); diff --git a/src/main.v b/src/main.v index 6df45dc..885e0f3 100644 --- a/src/main.v +++ b/src/main.v @@ -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() diff --git a/src/server/server.v b/src/server/server.v index 090aa76..2309ee7 100644 --- a/src/server/server.v +++ b/src/server/server.v @@ -68,7 +68,9 @@ pub fn server(conf Config) ? { } db_file := os.join_path_single(conf.data_dir, server.db_file_name) - db := db.init(db_file) or { util.exit_with_message(1, 'Failed to initialize database.') } + db := db.init(db_file) or { + util.exit_with_message(1, 'Failed to initialize database: $err.msg()') + } web.run(&App{ logger: logger