pkg/{timex,wait}: Unify code

This commit is contained in:
Tom Wiesing 2022-10-16 19:33:25 +02:00
parent 59fff07b59
commit 8701fab93b
No known key found for this signature in database
10 changed files with 77 additions and 74 deletions

View file

@ -15,12 +15,14 @@ import (
)
func (home *Home) updateInstances(ctx context.Context, io stream.IOStream) {
timex.SetInterval(ctx, home.RefreshInterval, func(t time.Time) {
io.Printf("[%s]: reloading instance list\n", t.Format(time.Stamp))
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading instance list\n", t.Format(time.Stamp))
names, _ := home.instanceMap()
home.instanceNames.Set(names)
})
names, _ := home.instanceMap()
home.instanceNames.Set(names)
}
}()
}
func (home *Home) instanceMap() (map[string]struct{}, error) {
@ -37,12 +39,14 @@ func (home *Home) instanceMap() (map[string]struct{}, error) {
}
func (home *Home) updateRender(ctx context.Context, io stream.IOStream) {
timex.SetInterval(ctx, home.RefreshInterval, func(t time.Time) {
io.Printf("[%s]: reloading home render\n", t.Format(time.Stamp))
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading home render\n", t.Format(time.Stamp))
bytes, _ := home.homeRender()
home.homeBytes.Set(bytes)
})
bytes, _ := home.homeRender()
home.homeBytes.Set(bytes)
}
}()
}
//go:embed "home.html"

View file

@ -12,11 +12,14 @@ import (
)
func (home *Home) updateRedirect(ctx context.Context, io stream.IOStream) {
timex.SetInterval(ctx, home.RefreshInterval, func(t time.Time) {
io.Printf("[%s]: reloading overrides\n", t.Format(time.Stamp))
redirect, _ := home.loadRedirect()
home.redirect.Set(&redirect)
})
go func() {
for t := range timex.TickContext(ctx, home.RefreshInterval) {
io.Printf("[%s]: reloading overrides\n", t.Format(time.Stamp))
redirect, _ := home.loadRedirect()
home.redirect.Set(&redirect)
}
}()
}
func (home *Home) loadRedirect() (redirect Redirect, err error) {

View file

@ -10,11 +10,13 @@ import (
// updatePrefixes starts updating prefixes
func (resolver *Resolver) updatePrefixes(io stream.IOStream, ctx context.Context) {
timex.SetInterval(ctx, resolver.RefreshInterval, func(t time.Time) {
io.Printf("[%s]: reloading prefixes\n", t.Format(time.Stamp))
prefixes, _ := resolver.AllPrefixes()
resolver.prefixes.Set(prefixes)
})
go func() {
for t := range timex.TickContext(ctx, resolver.RefreshInterval) {
io.Printf("[%s]: reloading prefixes\n", t.Format(time.Stamp))
prefixes, _ := resolver.AllPrefixes()
resolver.prefixes.Set(prefixes)
}
}()
}
// AllPrefixes returns a list of all prefixes from the server.

View file

@ -6,16 +6,16 @@ import (
"fmt"
"net"
"sync/atomic"
"time"
mysqldriver "github.com/go-sql-driver/mysql"
"github.com/tkw1536/goprogram/stream"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/wait"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
)
//
@ -42,10 +42,10 @@ func (sql *SQL) Exec(query string, args ...interface{}) error {
// WaitExec waits for the query interface to be able to connect to the database
func (sql *SQL) WaitExec() error {
return wait.Wait(func() bool {
return timex.TickUntilFunc(func(time.Time) bool {
err := sql.Exec("select 1;")
return err == nil
}, sql.PollInterval, sql.PollContext)
}, sql.PollContext, sql.PollInterval)
}
//
@ -91,12 +91,10 @@ func (sql *SQL) QueryTable(silent bool, table string) (*gorm.DB, error) {
// WaitQueryTable waits for a connection to succeed via QueryTable
func (sql *SQL) WaitQueryTable() error {
// TODO: Establish a convention on when to wait for this!
n := stream.FromNil()
return wait.Wait(func() bool {
return timex.TickUntilFunc(func(time.Time) bool {
_, err := sql.QueryTable(true, models.InstanceTable)
n.EPrintf("[SQL.WaitQueryTable]: %s\n", err)
return err == nil
}, sql.PollInterval, sql.PollContext)
}, sql.PollContext, sql.PollInterval)
}
//

View file

@ -3,11 +3,12 @@ package sql
import (
"errors"
"fmt"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
"github.com/FAU-CDI/wisski-distillery/pkg/sqle"
"github.com/FAU-CDI/wisski-distillery/pkg/wait"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
"github.com/tkw1536/goprogram/exit"
"github.com/tkw1536/goprogram/stream"
)
@ -22,11 +23,10 @@ func (sql *SQL) Shell(io stream.IOStream, argv ...string) (int, error) {
// unsafeWaitShell waits for a connection via the database shell to succeed
func (sql *SQL) unsafeWaitShell() error {
n := stream.FromNil()
return wait.Wait(func() bool {
return timex.TickUntilFunc(func(time.Time) bool {
code, err := sql.Shell(n, "-e", "select 1;")
n.EPrintf("[SQL.unsafeWaitShell]: %d %s\n", code, err)
return err == nil && code == 0
}, sql.PollInterval, sql.PollContext)
}, sql.PollContext, sql.PollInterval)
}
// unsafeQuery shell executes a raw database query.

View file

@ -6,8 +6,9 @@ import (
"io"
"mime/multipart"
"net/http"
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/wait"
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
"github.com/pkg/errors"
"github.com/tkw1536/goprogram/stream"
)
@ -87,7 +88,7 @@ func (ts Triplestore) OpenRaw(method, url string, body interface{}, bodyName str
// This is achieved using a polling strategy.
func (ts Triplestore) Wait() error {
n := stream.FromNil()
return wait.Wait(func() bool {
return timex.TickUntilFunc(func(time.Time) bool {
res, err := ts.OpenRaw("GET", "/rest/repositories", nil, "", "")
n.EPrintf("[Triplestore.Wait]: %s\n", err)
if err != nil {
@ -95,7 +96,7 @@ func (ts Triplestore) Wait() error {
}
defer res.Body.Close()
return true
}, ts.PollInterval, ts.PollContext)
}, ts.PollContext, ts.PollInterval)
}
// TriplestorePurgeUser deletes the specified user from the triplestore