Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
fix(cron): set cron timeout, add cron at least validator
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcodelf authored and gitlawr committed Sep 10, 2023
1 parent 9f3e5de commit ed21adc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/scheduler/catalog/template_sync_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (in *TemplateSyncTask) Process(ctx context.Context, args ...any) error {
serr := pkgcatalog.SyncTemplates(ctx, in.modelClient, c)

uerr = pkgcatalog.UpdateStatusWithSyncErr(
ctx,
context.Background(), // Make sure status will be updated, in case of task timeout.
in.modelClient,
c,
serr,
Expand Down
29 changes: 29 additions & 0 deletions pkg/settings/modifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"time"

imgdistref "github.com/distribution/distribution/reference"

Expand Down Expand Up @@ -144,6 +145,34 @@ func cronExpression(ctx context.Context, name, oldVal, newVal string) (bool, err
return err == nil, err
}

// cronAtLeast implements the modifyValidator stereotype,
// which means the value can be modified if it's cron expression and at least the given duration.
// This modifier allows blank new value,
// if not allowed, combine with notBlank.
func cronAtLeast(d time.Duration) modifyValidator {
return func(ctx context.Context, name, oldVal, newVal string) (bool, error) {
// Allow blank,
// combine with notBlank if disallowed.
if newVal == "" {
return true, nil
}

expr, err := cron.ParseCronExpr(newVal, false)
if err != nil {
return false, err
}

next := expr.Next(time.Now())

duration := expr.Next(next).Sub(next)
if duration < d {
return false, fmt.Errorf("cron expression %q is too short, at least %v", newVal, d)
}

return true, nil
}
}

// containerImageReference implements the modifyValidator stereotype,
// which means the value can be modified if it's container image reference.
// This modifier allows blank new value,
Expand Down
6 changes: 4 additions & 2 deletions pkg/settings/settings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package settings

import (
"time"

"k8s.io/utils/pointer"

"github.com/seal-io/walrus/pkg/casdoor"
Expand Down Expand Up @@ -204,12 +206,12 @@ var (
modifyWith(notBlank, cronExpression),
)
// CatalogTemplateSyncCronExpr indicates the cron expression of catalog template synchronization event,
// default cron expression means sync at 1 o'clock evey day.
// default cron expression means sync at 1 o'clock evey day, and new cron must be at least 30 minutes.
CatalogTemplateSyncCronExpr = newValue(
"CatalogTemplateSyncCronExpr",
private,
initializeFrom("0 0 1 * * *"),
modifyWith(notBlank, cronExpression),
modifyWith(notBlank, cronExpression, cronAtLeast(30*time.Minute)),
)
)

Expand Down
4 changes: 1 addition & 3 deletions pkg/templates/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -104,8 +103,7 @@ func (s schemaSyncer) Do(_ context.Context, message template.BusMessage) error {
logger := log.WithName("template")

gopool.Go(func() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
ctx := context.Background()

m := message.Refer
logger.Debugf("syncing schema for template %s", m.ID)
Expand Down
18 changes: 11 additions & 7 deletions staging/utils/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,23 @@ func (in *scheduler) Schedule(jobName string, cron Expr, task Task, taskArgs ...
return
}

const atLeast = 5 * time.Minute
const (
// The minimum timeout is 5 minutes.
atLeast = 5 * time.Minute
// The maximum timeout is 6 hours.
atMost = 6 * time.Hour
)

var (
now = time.Now()
next = ceParsed.Next(now).Sub(now)
next = ceParsed.Next(now).Sub(now) * 3
)

if next > atLeast {
next >>= 1
}

if next < atLeast {
switch {
case next < atLeast:
next = atLeast
case next > atMost:
next = atMost
}

tt := timeoutTask{
Expand Down

0 comments on commit ed21adc

Please sign in to comment.