@@ -9,31 +9,58 @@ import (
99type JobOption func (job * innerJob )
1010
1111// BeforeFunc represents the function could be called before Run.
12+ // Deprecated: Use BeforeContextFunc instead, it will be ignored if BeforeContextFunc is set.
1213type BeforeFunc func (task Task ) (skip bool )
1314
15+ // BeforeContextFunc represents the function could be called before Run with the given Task.
16+ type BeforeContextFunc func (ctx context.Context , task Task ) (skip bool )
17+
1418// RunFunc represents the function could be called by a cron.
1519type RunFunc func (ctx context.Context ) error
1620
1721// AfterFunc represents the function could be called after Run.
22+ // Deprecated: Use AfterContextFunc instead, it will be ignored if AfterContextFunc is set.
1823type AfterFunc func (task Task )
1924
25+ // AfterContextFunc represents the function could be called after Run with the given Task.
26+ type AfterContextFunc func (ctx context.Context , task Task )
27+
2028// RetryInterval indicates how long should delay before retrying when run failed `triedTimes` times.
2129type RetryInterval func (triedTimes int ) time.Duration
2230
31+ // DeriveContext indicates how to derive a new context from the job's base context and the current Task.
32+ type DeriveContext func (ctx context.Context , task Task ) context.Context
33+
2334// WithBeforeFunc specifies what to do before Run.
35+ // Deprecated: Use WithBeforeContextFunc instead.
2436func WithBeforeFunc (before BeforeFunc ) JobOption {
2537 return func (job * innerJob ) {
2638 job .before = before
2739 }
2840}
2941
42+ // WithBeforeContextFunc specifies what to do before Run with the given Task.
43+ func WithBeforeContextFunc (before BeforeContextFunc ) JobOption {
44+ return func (job * innerJob ) {
45+ job .ctxBefore = before
46+ }
47+ }
48+
3049// WithAfterFunc specifies what to do after Run.
50+ // Deprecated: Use WithAfterContextFunc instead.
3151func WithAfterFunc (after AfterFunc ) JobOption {
3252 return func (job * innerJob ) {
3353 job .after = after
3454 }
3555}
3656
57+ // WithAfterContextFunc specifies what to do after Run with the given Task.
58+ func WithAfterContextFunc (after AfterContextFunc ) JobOption {
59+ return func (job * innerJob ) {
60+ job .ctxAfter = after
61+ }
62+ }
63+
3764// WithRetryTimes specifies max times to retry,
3865// retryTimes will be set as 1 if it is less than 1.
3966func WithRetryTimes (retryTimes int ) JobOption {
@@ -63,3 +90,13 @@ func WithGroup(group Group) JobOption {
6390 job .group = group
6491 }
6592}
93+
94+ // WithDeriveContext specifies how to derive a new context for the entire job execution, including
95+ // before/after hooks, Run, and retry logic. The returned context must derive from the provided ctx
96+ // to preserve the deadline, cancellation signal, and the embedded Task value (accessible via TaskFromContext).
97+ // Returning a detached context (e.g., context.Background()) will break deadline enforcement and retry timeout logic.
98+ func WithDeriveContext (deriveContext DeriveContext ) JobOption {
99+ return func (job * innerJob ) {
100+ job .deriveContext = deriveContext
101+ }
102+ }
0 commit comments