How to access gocron.Job #796
Unanswered
justpoypoy
asked this question in
Q&A
Replies: 2 comments 4 replies
-
hi @justpoypoy Have you tried setting the job object no the call to NewJob and then referencing it? repo := repositories.New(r.di)
services := usecases.New(repo)
handle := handler.New(services)
job, err := s.NewJob(
gocron.DailyJob(
1,
gocron.NewAtTimes(
gocron.NewAtTime(17, 27, 30),
gocron.NewAtTime(17, 27, 40),
gocron.NewAtTime(17, 27, 50),
),
),
gocron.NewTask(handle.GetDailyTransaction(job.ID())),
gocron.WithSingletonMode(gocron.LimitModeWait),
) |
Beta Was this translation helpful? Give feedback.
3 replies
-
@justpoypoy you CAN do something like this, but it's not really that helpful as last run is going to be the same as time.Now package main
import (
"log/slog"
"time"
"github.com/go-co-op/gocron/v2"
)
func main() {
scheduler, err := gocron.NewScheduler()
if err != nil {
panic(err)
}
var job gocron.Job
job, err = scheduler.NewJob(
gocron.DurationJob(time.Second),
gocron.NewTask(func() {
lastRun, err := job.LastRun()
if err != nil {
slog.Error("job run", slog.String("error", err.Error()))
} else {
slog.Info("job run", slog.String("last_run", lastRun.String()))
}
}),
)
if err != nil {
panic(err)
}
scheduler.Start()
for {
}
} If I understand your request, you want to be able to take some action based on the last time the job ran inside of your task function? Could you help me understand the use case a little better? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How to access job interfaces eg. job.LastRun(), job.ID() inside task
this my example code
and how to access unique id or last run inside function GetDailyTransaction
Beta Was this translation helpful? Give feedback.
All reactions