Skip to content

Commit d3d2113

Browse files
committed
feat: add Context() method and update job retrieval, closes #762
- Add Context() method to Job interface and implement it in job struct - Update requestJob function to include a timeout flag - Modify existing methods to use the new requestJob signature
1 parent 8f6eb53 commit d3d2113

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

job.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ type Job interface {
10201020
// Tags returns the job's string tags.
10211021
Tags() []string
10221022
Lock() Lock
1023+
Context() context.Context
10231024
}
10241025

10251026
var _ Job = (*job)(nil)
@@ -1041,7 +1042,7 @@ func (j job) ID() uuid.UUID {
10411042
}
10421043

10431044
func (j job) LastRun() (time.Time, error) {
1044-
ij := requestJob(j.id, j.jobOutRequest)
1045+
ij := requestJob(j.id, j.jobOutRequest, true)
10451046
if ij == nil || ij.id == uuid.Nil {
10461047
return time.Time{}, ErrJobNotFound
10471048
}
@@ -1053,7 +1054,7 @@ func (j job) Name() string {
10531054
}
10541055

10551056
func (j job) NextRun() (time.Time, error) {
1056-
ij := requestJob(j.id, j.jobOutRequest)
1057+
ij := requestJob(j.id, j.jobOutRequest, true)
10571058
if ij == nil || ij.id == uuid.Nil {
10581059
return time.Time{}, ErrJobNotFound
10591060
}
@@ -1066,7 +1067,7 @@ func (j job) NextRun() (time.Time, error) {
10661067
}
10671068

10681069
func (j job) NextRuns(count int) ([]time.Time, error) {
1069-
ij := requestJob(j.id, j.jobOutRequest)
1070+
ij := requestJob(j.id, j.jobOutRequest, true)
10701071
if ij == nil || ij.id == uuid.Nil {
10711072
return nil, ErrJobNotFound
10721073
}
@@ -1120,7 +1121,13 @@ func (j job) RunNow() error {
11201121
}
11211122

11221123
func (j job) Lock() Lock {
1123-
ij := requestJob(j.id, j.jobOutRequest)
1124+
ij := requestJob(j.id, j.jobOutRequest, true)
11241125

11251126
return ij.lastLock
11261127
}
1128+
1129+
func (j job) Context() context.Context {
1130+
ij := requestJob(j.id, j.jobOutRequest, false)
1131+
1132+
return ij.ctx
1133+
}

util.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ func callJobFuncWithParams(jobFunc any, params ...any) error {
3636
return nil
3737
}
3838

39-
func requestJob(id uuid.UUID, ch chan jobOutRequest) *internalJob {
40-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
41-
defer cancel()
39+
func requestJob(id uuid.UUID, ch chan jobOutRequest, timeout bool) *internalJob {
40+
var cancel context.CancelFunc
41+
ctx := context.Background()
42+
43+
if timeout {
44+
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
45+
defer cancel()
46+
}
47+
4248
return requestJobCtx(ctx, id, ch)
4349
}
4450

0 commit comments

Comments
 (0)