Cron
Schedule a repeating job
cron.go
package main
import (
"context"
"fmt"
"log/slog"
"github.com/go-arrower/arrower"
)
type MyJob struct {
Payload string
}
func main() {
a, _, err := arrower.New()
if err != nil {
panic(err)
}
err = a.DefaultQueue.RegisterJobFunc(func(ctx context.Context, job MyJob) error {
a.Logger.InfoContext(ctx, "process job with data", slog.String("payload", job.Payload))
return nil
})
if err != nil {
panic(err)
}
err = a.DefaultQueue.Schedule("@every 1m", MyJob{Payload: "cron payload"})
if err != nil {
panic(err)
}
a.WebRouter.Logger.Fatal(a.WebRouter.Start(fmt.Sprintf(":%d", a.Config.Web.Port)))
}
Output
time=2024-06-15T19:02:12.311+02:00 level=INFO msg="process job with data" payload="cron payload" traceID=4c37383f62cbbf8224abe785aa487d9f spanID=851d1b9da711fc99 jobID=01J0EDHFTWW6M775MF24HZ7E74
time=2024-06-15T19:03:12.313+02:00 level=INFO msg="process job with data" payload="cron payload" traceID=3a524a0ca29d9028b40be03702998f21 spanID=add68136e1c46162 jobID=01J0EDHFTWW6M775MF25PPE49R
time=2024-06-15T19:04:12.313+02:00 level=INFO msg="process job with data" payload="cron payload" traceID=b2b950cf3a96f3119c32cfd39768fda2 spanID=dd6ad5a42b26124d jobID=01J0EDHFTX833DHQA7YCGP3EM7
...