Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into remove-nix
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
  • Loading branch information
artursouza committed Jul 22, 2024
2 parents 1c3defb + 7d28278 commit b96b46d
Show file tree
Hide file tree
Showing 32 changed files with 1,108 additions and 540 deletions.
3 changes: 3 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ linters:
- gci
- funlen
- maintidx
linters-settings:
goimports:
local-prefixes: github.com/diagridio
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Leadership keys are associated with an ETCD lease of 20s TTL to prevent stale le
## Counter

An associated `counters` key is used to track the current state of a job that is scheduled.
It includes the last trigger time (if triggered), the number of times the job has been triggered, and the UUID of the associated job with the same name.
It includes the last trigger time (if triggered), the number of times the job has been triggered, and the Partition ID of the associated job with the same name.
Counters are lazily deleted in bulk by a garbage collector that runs every 180s in an effort to reduce pressure of jobs triggering.

The scheduler will never miss triggering jobs.
Expand Down
26 changes: 15 additions & 11 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"fmt"
"strings"

"github.com/diagridio/go-etcd-cron/api"
"google.golang.org/protobuf/proto"
"k8s.io/apimachinery/pkg/util/validation"

"github.com/diagridio/go-etcd-cron/api"
)

// Add adds a new cron job to the cron instance.
Expand All @@ -25,7 +27,7 @@ func (c *cron) Add(ctx context.Context, name string, job *api.Job) error {
return ctx.Err()
}

if err := validateName(name); err != nil {
if err := c.validateName(name); err != nil {
return err
}

Expand All @@ -44,7 +46,6 @@ func (c *cron) Add(ctx context.Context, name string, job *api.Job) error {
}

_, err = c.client.Put(ctx, c.key.JobKey(name), string(b))

return err
}

Expand All @@ -58,7 +59,7 @@ func (c *cron) Get(ctx context.Context, name string) (*api.Job, error) {
return nil, ctx.Err()
}

if err := validateName(name); err != nil {
if err := c.validateName(name); err != nil {
return nil, err
}

Expand Down Expand Up @@ -90,24 +91,27 @@ func (c *cron) Delete(ctx context.Context, name string) error {
return ctx.Err()
}

if err := validateName(name); err != nil {
if err := c.validateName(name); err != nil {
return err
}

qerr := c.queue.Dequeue(name)
_, err := c.client.Delete(ctx, c.key.JobKey(name))
if _, err := c.client.Delete(ctx, c.key.JobKey(name)); err != nil {
return err
}

return errors.Join(err, qerr)
return c.queue.Dequeue(c.key.JobKey(name))
}

// validateName validates the name of a job.
func validateName(name string) error {
func (c *cron) validateName(name string) error {
if len(name) == 0 {
return errors.New("job name cannot be empty")
}

if strings.Contains(name, "/") || strings.Contains(name, ".") {
return fmt.Errorf("job name cannot contain '/' or '.': %s", name)
for _, segment := range strings.Split(strings.ToLower(c.validateNameReplacer.Replace(name)), "||") {
if errs := validation.IsDNS1123Subdomain(segment); len(errs) > 0 {
return fmt.Errorf("job name is invalid %q: %s", name, strings.Join(errs, ", "))
}
}

return nil
Expand Down
19 changes: 11 additions & 8 deletions api/counter.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 103 additions & 53 deletions api/job.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b96b46d

Please sign in to comment.