snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
19
server/pkg/scheduler/healthcheck.go
Normal file
19
server/pkg/scheduler/healthcheck.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Healtcheck for (global) scheduler
|
||||
func Healthcheck(ctx context.Context) error {
|
||||
if gScheduler == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if gScheduler.ticker() == nil {
|
||||
return fmt.Errorf("stopped")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
83
server/pkg/scheduler/helpers.go
Normal file
83
server/pkg/scheduler/helpers.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/logger"
|
||||
"github.com/getsentry/sentry-go"
|
||||
"github.com/gorhill/cronexpr"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// OnInterval parses all given strings as crontab expressions (ii) and returns true if any of them matches current time
|
||||
func OnInterval(ii ...string) bool {
|
||||
match, err := onInterval(now(), ii...)
|
||||
if err != nil {
|
||||
logger.Default().Error("failed to parse interval value", zap.Strings("value", ii), zap.Error(err))
|
||||
sentry.CaptureException(err)
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
func onInterval(now time.Time, ii ...string) (bool, error) {
|
||||
var (
|
||||
// This function will likely to be called exactly on a minute (00 sec) or a few milliseconds after
|
||||
// Round it up to the smallest unit that cronexpr package supports
|
||||
currTime = now.Truncate(time.Second)
|
||||
|
||||
// For cron expression reference we need to subtract 1ns
|
||||
// this will cause Next() fn to include next nanosecond (if it matches)
|
||||
cronRef = currTime.Add(-time.Nanosecond)
|
||||
)
|
||||
|
||||
// At least one of the given expressions should match
|
||||
for _, i := range ii {
|
||||
if len(i) == 0 {
|
||||
// skip empty values
|
||||
continue
|
||||
}
|
||||
|
||||
exp, err := cronexpr.Parse(i)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return currTime.Equal(exp.Next(cronRef)), nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// OnTimestamp parses all given strings as RFC3339 timestamps and returns true if any of them matches current time
|
||||
func OnTimestamp(tt ...string) bool {
|
||||
match, err := onTimestamp(now(), tt...)
|
||||
if err != nil {
|
||||
logger.Default().Error("failed to parse timestamp value", zap.Strings("value", tt), zap.Error(err))
|
||||
sentry.CaptureException(err)
|
||||
}
|
||||
return match
|
||||
}
|
||||
|
||||
func onTimestamp(now time.Time, tt ...string) (bool, error) {
|
||||
var (
|
||||
// This function will likely to be called exactly on a minute (00 sec) or a few milliseconds after
|
||||
// Round it up to the smallest unit that cronexpr package supports
|
||||
currTime = now.Truncate(time.Second)
|
||||
)
|
||||
|
||||
for _, t := range tt {
|
||||
if len(t) == 0 {
|
||||
// skip empty values
|
||||
continue
|
||||
}
|
||||
|
||||
ts, err := time.Parse(time.RFC3339, t)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return currTime.Equal(ts.Round(time.Second)), nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
141
server/pkg/scheduler/helpers_test.go
Normal file
141
server/pkg/scheduler/helpers_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestOnInterval(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
now string
|
||||
ii []string
|
||||
match bool
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
"empty",
|
||||
time.Now().Format(time.RFC3339),
|
||||
[]string{},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil",
|
||||
time.Now().Format(time.RFC3339),
|
||||
nil,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"next minute",
|
||||
"2019-10-10T10:11:00Z",
|
||||
[]string{"* * * * *"},
|
||||
true,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"every 5 minutes",
|
||||
"2019-10-10T10:15:00Z",
|
||||
[]string{"*/5 * * * *"},
|
||||
true,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not full minute",
|
||||
"2019-10-10T10:15:50Z",
|
||||
[]string{"* * * * *"},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid format",
|
||||
"2019-10-10T10:15:50Z",
|
||||
[]string{":P"},
|
||||
false,
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
now, err := time.Parse(time.RFC3339, c.now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
match, err := onInterval(now, c.ii...)
|
||||
if !c.err {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, c.match, match)
|
||||
})
|
||||
}
|
||||
|
||||
// touch exported func
|
||||
OnInterval("* * * * *")
|
||||
OnInterval(":P")
|
||||
}
|
||||
|
||||
func TestOnTimestamp(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
now string
|
||||
ii []string
|
||||
match bool
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
"empty",
|
||||
time.Now().Format(time.RFC3339),
|
||||
[]string{},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil",
|
||||
time.Now().Format(time.RFC3339),
|
||||
nil,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"must match",
|
||||
"2019-10-10T10:11:00Z",
|
||||
[]string{"2019-10-10T10:11:00Z"},
|
||||
true,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"must not match",
|
||||
"2019-10-10T10:15:00Z",
|
||||
[]string{"2020-10-10T10:15:00Z"},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid format",
|
||||
"2019-10-10T10:15:00Z",
|
||||
[]string{":P"},
|
||||
false,
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
now, err := time.Parse(time.RFC3339, c.now)
|
||||
assert.NoError(t, err)
|
||||
|
||||
match, err := onTimestamp(now, c.ii...)
|
||||
if !c.err {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, c.match, match)
|
||||
})
|
||||
}
|
||||
|
||||
// touch exported func
|
||||
OnTimestamp("2019-10-10T10:15:00Z")
|
||||
OnTimestamp(":P")
|
||||
}
|
||||
181
server/pkg/scheduler/service.go
Normal file
181
server/pkg/scheduler/service.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/eventbus"
|
||||
"github.com/cortezaproject/corteza/server/pkg/sentry"
|
||||
)
|
||||
|
||||
type (
|
||||
service struct {
|
||||
log *zap.Logger
|
||||
events []eventbus.Event
|
||||
interval time.Duration
|
||||
dispatcher dispatcher
|
||||
|
||||
// Read & write locking
|
||||
l sync.RWMutex
|
||||
|
||||
// Simple chan to control if service is running or not
|
||||
t *time.Ticker
|
||||
}
|
||||
|
||||
dispatcher interface {
|
||||
WaitFor(ctx context.Context, ev eventbus.Event) error
|
||||
}
|
||||
)
|
||||
|
||||
const (
|
||||
defaultInterval = time.Minute
|
||||
|
||||
// There should not be more than 2 per each service (<no of services> * 2 [interval + timestamp])
|
||||
maxEvents = 16
|
||||
)
|
||||
|
||||
var (
|
||||
now = func() time.Time { return time.Now() }
|
||||
|
||||
// Global scheduler
|
||||
gScheduler *service
|
||||
)
|
||||
|
||||
// Setup configures global scheduling service
|
||||
func Setup(log *zap.Logger, d dispatcher, interval time.Duration) {
|
||||
if gScheduler != nil {
|
||||
// shut down current global scheduler
|
||||
gScheduler.Stop()
|
||||
}
|
||||
|
||||
gScheduler = NewService(log, d, interval)
|
||||
}
|
||||
|
||||
func Service() *service {
|
||||
return gScheduler
|
||||
}
|
||||
|
||||
func NewService(log *zap.Logger, d dispatcher, interval time.Duration) *service {
|
||||
// Fix interval to positive number
|
||||
if interval == 0 {
|
||||
interval = defaultInterval
|
||||
}
|
||||
|
||||
var svc = &service{
|
||||
l: sync.RWMutex{},
|
||||
log: log.Named("scheduler"),
|
||||
interval: interval,
|
||||
dispatcher: d,
|
||||
events: make([]eventbus.Event, 0, maxEvents),
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
// Register all events that should fire on tick (interval)
|
||||
func (svc *service) OnTick(events ...eventbus.Event) {
|
||||
svc.l.Lock()
|
||||
defer svc.l.Unlock()
|
||||
svc.events = append(svc.events, events...)
|
||||
}
|
||||
|
||||
func (svc *service) Stop() {
|
||||
svc.l.Lock()
|
||||
defer svc.l.Unlock()
|
||||
|
||||
if svc.t == nil {
|
||||
return
|
||||
}
|
||||
|
||||
svc.log.Debug("stopping")
|
||||
svc.t.Stop()
|
||||
svc.t = nil
|
||||
}
|
||||
|
||||
// Run starts event scheduler service
|
||||
func (svc *service) Start(ctx context.Context) {
|
||||
if svc.ticker() != nil {
|
||||
svc.log.Debug("already started")
|
||||
return
|
||||
}
|
||||
|
||||
svc.l.Lock()
|
||||
defer svc.l.Unlock()
|
||||
// setting un-configured ticker to mark scheduler service as stated
|
||||
svc.t = &time.Ticker{}
|
||||
|
||||
go func() {
|
||||
defer sentry.Recover()
|
||||
|
||||
// Calculate how much time we need to wait until next tick
|
||||
delay := now().Truncate(svc.interval).Add(svc.interval).Sub(now())
|
||||
|
||||
svc.log.Debug(
|
||||
"starting",
|
||||
zap.Duration("delay", delay),
|
||||
zap.Duration("interval", svc.interval),
|
||||
)
|
||||
|
||||
// Wait until start of the next interval
|
||||
time.Sleep(delay)
|
||||
|
||||
svc.l.Lock()
|
||||
defer svc.l.Unlock()
|
||||
svc.t = time.NewTicker(svc.interval)
|
||||
|
||||
svc.log.Debug("started")
|
||||
|
||||
go svc.watch(ctx)
|
||||
}()
|
||||
}
|
||||
|
||||
func (svc *service) watch(ctx context.Context) {
|
||||
defer sentry.Recover()
|
||||
defer svc.Stop()
|
||||
|
||||
// start with first interval
|
||||
svc.dispatch(ctx)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-svc.ticker().C:
|
||||
svc.dispatch(ctx)
|
||||
|
||||
case <-ctx.Done():
|
||||
svc.log.Debug("done")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (svc *service) ticker() *time.Ticker {
|
||||
svc.l.RLock()
|
||||
defer svc.l.RUnlock()
|
||||
return svc.t
|
||||
}
|
||||
|
||||
func (svc *service) Started() (started bool) {
|
||||
return svc.ticker() != nil
|
||||
}
|
||||
|
||||
func (svc *service) dispatch(ctx context.Context) {
|
||||
svc.l.RLock()
|
||||
defer svc.l.RUnlock()
|
||||
|
||||
ee := make([]eventbus.Event, len(svc.events))
|
||||
for e := range svc.events {
|
||||
ee[e] = svc.events[e]
|
||||
}
|
||||
|
||||
for _, ev := range ee {
|
||||
go func(ev eventbus.Event) {
|
||||
err := svc.dispatcher.WaitFor(ctx, ev)
|
||||
if err != nil {
|
||||
svc.log.Warn("failed to execute scheduled trigger", zap.Error(err))
|
||||
}
|
||||
}(ev)
|
||||
}
|
||||
}
|
||||
62
server/pkg/scheduler/service_test.go
Normal file
62
server/pkg/scheduler/service_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/eventbus"
|
||||
)
|
||||
|
||||
type (
|
||||
mockEvent struct {
|
||||
rType string
|
||||
eType string
|
||||
match func(matcher eventbus.ConstraintMatcher) bool
|
||||
}
|
||||
)
|
||||
|
||||
func (e mockEvent) ResourceType() string {
|
||||
return e.rType
|
||||
}
|
||||
|
||||
func (e mockEvent) EventType() string {
|
||||
return e.eType
|
||||
}
|
||||
|
||||
func (e mockEvent) Match(matcher eventbus.ConstraintMatcher) bool {
|
||||
if e.match == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
return e.match(matcher)
|
||||
}
|
||||
|
||||
func TestMainServiceFunctions(t *testing.T) {
|
||||
r := require.New(t)
|
||||
|
||||
const (
|
||||
loopInterval = time.Millisecond * 100
|
||||
actionWait = loopInterval * 10
|
||||
)
|
||||
|
||||
if gScheduler != nil {
|
||||
gScheduler.Stop()
|
||||
gScheduler = nil
|
||||
}
|
||||
|
||||
Setup(zap.NewNop(), eventbus.New(), loopInterval)
|
||||
r.NotNil(gScheduler)
|
||||
r.False(gScheduler.Started())
|
||||
r.Equal(gScheduler, Service())
|
||||
Service().Start(context.Background())
|
||||
Service().OnTick(&mockEvent{}, &mockEvent{}, &mockEvent{}, &mockEvent{})
|
||||
time.Sleep(actionWait)
|
||||
r.True(gScheduler.Started())
|
||||
gScheduler.Stop()
|
||||
time.Sleep(actionWait)
|
||||
r.False(gScheduler.Started())
|
||||
}
|
||||
Reference in New Issue
Block a user