Add Cron interval to config
This commit is contained in:
parent
f52fe6abf3
commit
52dbfbf56e
6 changed files with 21 additions and 6 deletions
|
|
@ -2,7 +2,6 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
wisski_distillery "github.com/FAU-CDI/wisski-distillery"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
"github.com/FAU-CDI/wisski-distillery/internal/cli"
|
||||||
|
|
@ -51,7 +50,7 @@ func (s server) Run(context wisski_distillery.Context) error {
|
||||||
|
|
||||||
// start the cron tasks
|
// start the cron tasks
|
||||||
context.Printf("Starting cron tasks %s\n", s.Bind)
|
context.Printf("Starting cron tasks %s\n", s.Bind)
|
||||||
done := dis.Cron().Start(context.Context, time.Minute, notify)
|
done := dis.Cron().Start(context.Context, notify)
|
||||||
defer func() {
|
defer func() {
|
||||||
<-done
|
<-done
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config represents the configuration of a WissKI Distillery.
|
// Config represents the configuration of a WissKI Distillery.
|
||||||
|
|
@ -89,6 +90,9 @@ type Config struct {
|
||||||
// name of docker network to use
|
// name of docker network to use
|
||||||
DockerNetworkName string `env:"DOCKER_NETWORK_NAME" default:"distillery" parser:"nonempty"`
|
DockerNetworkName string `env:"DOCKER_NETWORK_NAME" default:"distillery" parser:"nonempty"`
|
||||||
|
|
||||||
|
// interval to trigger distillery cron tasks in
|
||||||
|
CronInterval time.Duration `env:"CRON_INTERVAL" default:"10m" parser:"duration"`
|
||||||
|
|
||||||
// ConfigPath is the path this configuration was loaded from (if any)
|
// ConfigPath is the path this configuration was loaded from (if any)
|
||||||
ConfigPath string
|
ConfigPath string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -76,3 +76,6 @@ KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_ADMIN_PASSWORD}
|
||||||
# The admin user and password required to access the /dis/ server and api
|
# The admin user and password required to access the /dis/ server and api
|
||||||
DIS_ADMIN_USER=${DIS_ADMIN_USER}
|
DIS_ADMIN_USER=${DIS_ADMIN_USER}
|
||||||
DIS_ADMIN_PASSWORD=${DIS_ADMIN_PASSWORD}
|
DIS_ADMIN_PASSWORD=${DIS_ADMIN_PASSWORD}
|
||||||
|
|
||||||
|
# the interval to run cron in
|
||||||
|
CRON_INTERVAL=10m
|
||||||
|
|
@ -91,16 +91,18 @@ func (control *Cron) Once(ctx context.Context) {
|
||||||
zerolog.Ctx(ctx).Info().Time("time", time.Now()).Msg("Finished Cron")
|
zerolog.Ctx(ctx).Info().Time("time", time.Now()).Msg("Finished Cron")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start invokes all cron jobs regularly, waiting interval between invocations.
|
// Start invokes all cron jobs regularly, waiting between invocations as specified in configuration.
|
||||||
//
|
//
|
||||||
// The first run is invoked immediatly.
|
// The first run is invoked immediatly.
|
||||||
// The call to Start returns after the first invocation of all cron tasks.
|
// The call to Start returns after the first invocation of all cron tasks.
|
||||||
//
|
//
|
||||||
// The returned channel is closed once no more cron tasks are active.
|
// The returned channel is closed once no more cron tasks are active.
|
||||||
func (control *Cron) Start(ctx context.Context, interval time.Duration, signal <-chan struct{}) <-chan struct{} {
|
func (control *Cron) Start(ctx context.Context, signal <-chan struct{}) <-chan struct{} {
|
||||||
|
zerolog.Ctx(ctx).Info().Dur("interval", control.Config.CronInterval).Msg("Scheduling Cron() tasks")
|
||||||
|
|
||||||
// run runs cron tasks with the configured timeout
|
// run runs cron tasks with the configured timeout
|
||||||
run := func() {
|
run := func() {
|
||||||
ctx, done := context.WithTimeout(ctx, interval)
|
ctx, done := context.WithTimeout(ctx, control.Config.CronInterval)
|
||||||
defer done()
|
defer done()
|
||||||
|
|
||||||
control.Once(ctx)
|
control.Once(ctx)
|
||||||
|
|
@ -117,7 +119,7 @@ func (control *Cron) Start(ctx context.Context, interval time.Duration, signal <
|
||||||
timer := timex.NewTimer()
|
timer := timex.NewTimer()
|
||||||
for {
|
for {
|
||||||
timex.StopTimer(timer)
|
timex.StopTimer(timer)
|
||||||
timer.Reset(interval)
|
timer.Reset(control.Config.CronInterval)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ var knownParsers map[string]Parser[any] = map[string]Parser[any]{
|
||||||
"abspath": asGenericParser(ParseAbspath),
|
"abspath": asGenericParser(ParseAbspath),
|
||||||
"domain": asGenericParser(ParseValidDomain),
|
"domain": asGenericParser(ParseValidDomain),
|
||||||
"domains": asGenericParser(ParseValidDomains),
|
"domains": asGenericParser(ParseValidDomains),
|
||||||
|
"duration": asGenericParser(ParseDuration),
|
||||||
"number": asGenericParser(ParseNumber),
|
"number": asGenericParser(ParseNumber),
|
||||||
"port": asGenericParser(ParsePort),
|
"port": asGenericParser(ParsePort),
|
||||||
"https_url": asGenericParser(ParseHttpsURL),
|
"https_url": asGenericParser(ParseHttpsURL),
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
||||||
|
|
@ -116,3 +117,8 @@ func ParseSlug(env environment.Environment, s string) (string, error) {
|
||||||
}
|
}
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseDuration parses a time.Duration
|
||||||
|
func ParseDuration(env environment.Environment, s string) (time.Duration, error) {
|
||||||
|
return time.ParseDuration(s)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue