Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
458 changes: 395 additions & 63 deletions .golangci.yml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ fmt:
@golangci-lint fmt

lint:
@golangci-lint --version
@golangci-lint run -c .golangci.yml
@golangci-lint version
@golangci-lint config verify
@golangci-lint run

run:
@echo "Compiling"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Run `curl -H 'Accept: application/json' http://localhost:2112/debug/cron` for js
## `WithMetrics` Middleware

* `app_cron_evaluated_total` – total processed jobs by state.
* `app_cron_active_count` – active running jobs.
* `app_cron_active` – active running jobs.
* `app_cron_evaluated_duration_seconds` – summary metric with durations.

## Example
Expand Down
6 changes: 3 additions & 3 deletions cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (cm *Manager) Run(ctx context.Context) error {
// register main functions in cron library
id, err := cm.cron.AddFunc(j.schedule.String(), func() { _ = cronFnCtx(ctx) })
if err != nil {
return fmt.Errorf("add cron=%v failed: %v", j.name, err)
return fmt.Errorf("add cron=%v failed: %w", j.name, err)
}

// set ID
Expand All @@ -189,7 +189,7 @@ func (cm *Manager) Stop() context.Context {
return cm.cron.Stop()
}

// updateState set
// updateState set.
func (cm *Manager) updateState(idx int, state cronState, err error) {
cm.muState.Lock()
defer cm.muState.Unlock()
Expand Down Expand Up @@ -223,7 +223,7 @@ func (cm *Manager) updateID(idx int, id cron.EntryID, funcJob Func) {
cm.jobs[idx].cronFn = funcJob
}

// Use adds middleware for cron job
// Use adds middleware for cron job.
func (cm *Manager) Use(m ...MiddlewareFunc) {
cm.middleware = append(cm.middleware, m...)
}
Expand Down
2 changes: 1 addition & 1 deletion cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestManager_Validate(t *testing.T) {

func TestManager_Run(t *testing.T) {
Convey("Test validate function", t, func() {
ctx := context.Background()
ctx := t.Context()
m := NewManager()
m.Use(
WithDevel(false),
Expand Down
3 changes: 2 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"os"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/vmkteam/cron"

"github.com/prometheus/client_golang/prometheus/promhttp"
)

// newTask creates new random task with random sleep (0-70s) and with random error or panic.
Expand Down
6 changes: 3 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type State struct {

type States []State

// LogValue реализует интерфейс slog.LogValuer
// LogValue implements slog.LogValuer.
func (s States) LogValue() slog.Value {
attrs := make([]slog.Attr, len(s))
for i, state := range s {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (cm *Manager) Handler(w http.ResponseWriter, r *http.Request) {

startID := r.URL.Query().Get("start")
if startID != "" {
go cm.ManualRun(context.WithoutCancel(r.Context()), startID)
go func() { _ = cm.ManualRun(context.WithoutCancel(r.Context()), startID) }()
http.Redirect(w, r, r.URL.Path, http.StatusFound)
return
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func (printer) text(state []State, w io.Writer) {

fmt.Fprintf(wr, tableRow("cron=%s%s", "%s", "%s", "%s"), st.Name, maintenance, st.Schedule, next, st.LastState)
}
wr.Flush()
_ = wr.Flush()
}

// tableRow is a helper for tab separated strings.
Expand Down
10 changes: 5 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ func WithSLog(lg Logger) MiddlewareFunc {
err := next(ctx)

d, name := time.Since(start), NameFromContext(ctx)
if errors.Is(err, ErrSkipped) {
switch {
case errors.Is(err, ErrSkipped):
lg.Print(ctx, "cron job skipped", "job", name, "duration", d)
} else if err != nil {
case err != nil:
lg.Error(ctx, "cron job failed", "job", name, "duration", d, "err", err)
} else {
default:
lg.Print(ctx, "cron job finished", "job", name, "duration", d)
}

Expand Down Expand Up @@ -188,7 +189,6 @@ func WithMaintenance(p LogPrintf) MiddlewareFunc {
pf("cron getting maintenance lock=%v", name)
mutex.Lock()
pf("cron got maintenance lock=%v", name)

} else {
mutex.RLock()
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func WithMetrics(app string) MiddlewareFunc {
statActive := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "app",
Subsystem: "cron",
Name: "active_count",
Name: "active",
Help: "Track current status of cron.",
}, []string{"app", "cron"})

Expand Down
Loading