Add admin_api and database logging of runs #9 #10

This commit is contained in:
2023-01-31 20:25:52 -07:00
parent 724757b23c
commit 4bda3c7a3b
11 changed files with 400 additions and 136 deletions
+10 -5
View File
@@ -8,14 +8,14 @@ import (
"context"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/op/go-logging"
)
var log = logging.MustGetLogger("cursorius-server")
type Database struct {
Conn *pgx.Conn
Conn *pgxpool.Pool
}
func LaunchDB(conf config.DBConfig) (Database, error) {
@@ -41,8 +41,9 @@ func LaunchDB(conf config.DBConfig) (Database, error) {
var err error
for i := 0; i < 10; i++ {
// TODO: retry logic is broken with pgxpool
log.Infof("Connecting to database with URL \"%v\" (attempt %v)", dbURLNoPasswd, i)
db.Conn, err = pgx.Connect(context.Background(), dbURL)
db.Conn, err = pgxpool.New(context.Background(), dbURL)
if err == nil {
break
}
@@ -80,12 +81,13 @@ SELECT EXISTS (
return db, nil
}
func initDB(conn *pgx.Conn) error {
func initDB(conn *pgxpool.Pool) error {
createTablesQuery := `
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE version (
version INT NOT NULL
);
CREATE TABLE pipelines (
@@ -115,7 +117,10 @@ CREATE TABLE runners (
CREATE TABLE runs (
id UUID PRIMARY KEY,
pipeline UUID,
result BOOLEAN NOT NULL,
in_progress BOOLEAN DEFAULT NULL,
result BIGINT DEFAULT NULL,
stdout TEXT DEFAULT NULL,
stderr TEXT DEFAULT NULL,
CONSTRAINT fk_pipeline
FOREIGN KEY(pipeline)