impl asses.checkLast(), .check()

main
Bel LaPointe 2025-05-08 15:40:53 -06:00
parent 137fdf07ed
commit 6b51a0c0a3
4 changed files with 91 additions and 14 deletions

View File

@ -18,9 +18,9 @@ func Next(ctx context.Context) (time.Time, error) {
} }
result, err := db.QueryOne[Did](ctx, ` result, err := db.QueryOne[Did](ctx, `
SELECT executed_at AS "Did" SELECT executed_at AS "Did"
FROM "asses.executions" FROM "asses.executions"
ORDER BY executed_at DESC ORDER BY executed_at DESC
LIMIT 1 LIMIT 1
`) `)
return result.Did, err return result.Did, err
} }
@ -33,11 +33,49 @@ func Record(ctx context.Context) error {
return db.Exec(ctx, `INSERT INTO "asses.executions" (id, executed_at) VALUES ($1, $2)`, uuid.New().String(), time.Now()) return db.Exec(ctx, `INSERT INTO "asses.executions" (id, executed_at) VALUES ($1, $2)`, uuid.New().String(), time.Now())
} }
type last struct {
T time.Time `json:"checked_at"`
Cksum string `json:"cksum"`
}
func checkLast(ctx context.Context, p string) (last, error) {
if err := initDB(ctx); err != nil {
return last{}, err
}
return db.QueryOne[last](ctx, `
SELECT checked_at, cksum
FROM "asses.checks"
WHERE p=$1
`, p)
}
func checked(ctx context.Context, p, cksum string) error {
if err := initDB(ctx); err != nil {
return err
}
return db.Exec(ctx, `
INSERT INTO "asses.checks"
(p, checked_at, cksum)
VALUES ($1, $2, $3)
ON CONFLICT DO UPDATE
SET checked_at=$2, cksum=$3
WHERE p=$1
`, p, time.Now(), cksum)
}
func initDB(ctx context.Context) error { func initDB(ctx context.Context) error {
return db.InitializeSchema(ctx, "asses", []string{ return db.InitializeSchema(ctx, "asses", []string{
`CREATE TABLE "asses.executions" ( `CREATE TABLE "asses.executions" (
id TEXT PRIMARY KEY NOT NULL, id TEXT PRIMARY KEY NOT NULL,
executed_at TIMESTAMP NOT NULL executed_at TIMESTAMP NOT NULL
)`, )`,
`CREATE TABLE "asses.checks" (
p TEXT PRIMARY KEY NOT NULL,
checked_at TIMESTAMP NOT NULL,
cksum TEXT NOT NULL
)`,
}) })
} }

View File

@ -0,0 +1,29 @@
package asses_test
import (
"context"
"show-rss/src/asses"
"show-rss/src/db"
"testing"
"time"
)
func TestNextRecord(t *testing.T) {
ctx := db.Test(t, context.Background())
if v, err := asses.Next(ctx); err != nil {
t.Fatal(err)
} else if zero := v.IsZero(); !zero {
t.Fatal(v)
}
if err := asses.Record(ctx); err != nil {
t.Fatal(err)
}
if v, err := asses.Next(ctx); err != nil {
t.Fatal(err)
} else if since := time.Since(v); since > time.Minute {
t.Fatal(since)
}
}

View File

@ -1,29 +1,29 @@
package asses_test package asses
import ( import (
"context" "context"
"show-rss/src/asses"
"show-rss/src/db" "show-rss/src/db"
"testing" "testing"
"time"
) )
func TestNextRecord(t *testing.T) { func TestLast(t *testing.T) {
ctx := db.Test(t, context.Background()) ctx := db.Test(t, context.Background())
if v, err := asses.Next(ctx); err != nil { if last, err := checkLast(ctx, "p"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if zero := v.IsZero(); !zero { } else if !last.T.IsZero() || last.Cksum != "" {
t.Fatal(v) t.Fatal(last)
} }
if err := asses.Record(ctx); err != nil { if err := checked(ctx, "p", "cksum"); err != nil {
t.Fatal(err)
} else if err := checked(ctx, "p", "cksum"); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v, err := asses.Next(ctx); err != nil { if last, err := checkLast(ctx, "p"); err != nil {
t.Fatal(err) t.Fatal(err)
} else if since := time.Since(v); since > time.Minute { } else if last.T.IsZero() || last.Cksum != "cksum" {
t.Fatal(since) t.Fatal(last)
} }
} }

10
src/asses/one.go Normal file
View File

@ -0,0 +1,10 @@
package asses
import (
"context"
"io"
)
func One(ctx context.Context, p string) error {
return io.EOF
}