diff --git a/job.go b/job.go index d43b7d1..1bed199 100644 --- a/job.go +++ b/job.go @@ -995,6 +995,7 @@ type Job interface { // Tags returns the job's string tags. Tags() []string Lock() Lock + Context() context.Context } var _ Job = (*job)(nil) @@ -1016,7 +1017,7 @@ func (j job) ID() uuid.UUID { } func (j job) LastRun() (time.Time, error) { - ij := requestJob(j.id, j.jobOutRequest) + ij := requestJob(j.id, j.jobOutRequest, true) if ij == nil || ij.id == uuid.Nil { return time.Time{}, ErrJobNotFound } @@ -1028,7 +1029,7 @@ func (j job) Name() string { } func (j job) NextRun() (time.Time, error) { - ij := requestJob(j.id, j.jobOutRequest) + ij := requestJob(j.id, j.jobOutRequest, true) if ij == nil || ij.id == uuid.Nil { return time.Time{}, ErrJobNotFound } @@ -1041,7 +1042,7 @@ func (j job) NextRun() (time.Time, error) { } func (j job) NextRuns(count int) ([]time.Time, error) { - ij := requestJob(j.id, j.jobOutRequest) + ij := requestJob(j.id, j.jobOutRequest, true) if ij == nil || ij.id == uuid.Nil { return nil, ErrJobNotFound } @@ -1095,7 +1096,13 @@ func (j job) RunNow() error { } func (j job) Lock() Lock { - ij := requestJob(j.id, j.jobOutRequest) + ij := requestJob(j.id, j.jobOutRequest, true) return ij.lastLock } + +func (j job) Context() context.Context { + ij := requestJob(j.id, j.jobOutRequest, false) + + return ij.ctx +} diff --git a/util.go b/util.go index a4e5b6f..65511e7 100644 --- a/util.go +++ b/util.go @@ -36,9 +36,15 @@ func callJobFuncWithParams(jobFunc any, params ...any) error { return nil } -func requestJob(id uuid.UUID, ch chan jobOutRequest) *internalJob { - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() +func requestJob(id uuid.UUID, ch chan jobOutRequest, timeout bool) *internalJob { + var cancel context.CancelFunc + ctx := context.Background() + + if timeout { + ctx, cancel = context.WithTimeout(context.Background(), time.Second) + defer cancel() + } + return requestJobCtx(ctx, id, ch) }