Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add public jobs.WithJobContext() #131

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions backends/memory/memory_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (m *MemBackend) scheduleFutureJobs(ctx context.Context) {
}

func (m *MemBackend) handleJob(ctx context.Context, job *jobs.Job, h handler.Handler) (err error) {
ctx = withJobContext(ctx, job)
ctx = jobs.WithJobContext(ctx, job)

m.logger.Debug(
"handling job",
Expand Down Expand Up @@ -381,8 +381,3 @@ func (m *MemBackend) removeFutureJob(jobID int64) {
m.futureJobs.Delete(job.ID)
}
}

// withJobContext creates a new context with the Job set
func withJobContext(ctx context.Context, j *jobs.Job) context.Context {
return context.WithValue(ctx, internal.JobCtxVarKey, j)
}
7 changes: 1 addition & 6 deletions backends/postgres/postgres_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ func (p *PgBackend) handleJob(ctx context.Context, jobID string) (err error) {
return
}

ctx = withJobContext(ctx, job)
ctx = jobs.WithJobContext(ctx, job)
ctx = context.WithValue(ctx, txCtxVarKey, tx)

if job.Deadline != nil && job.Deadline.Before(time.Now().UTC()) {
Expand Down Expand Up @@ -1068,11 +1068,6 @@ func (p *PgBackend) acquire(ctx context.Context) (conn *pgxpool.Conn, err error)
}
}

// withJobContext creates a new context with the Job set
func withJobContext(ctx context.Context, j *jobs.Job) context.Context {
return context.WithValue(ctx, internal.JobCtxVarKey, j)
}

func GetPQConnectionString(connectionString string) (string, error) {
pgxCfg, err := pgx.ParseConfig(connectionString)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions backends/redis/redis_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (b *RedisBackend) Start(_ context.Context, h handler.Handler) (err error) {
RunAfter: ti.NextProcessAt,
}

ctx = withJobContext(ctx, job)
ctx = jobs.WithJobContext(ctx, job)
err = handler.Exec(ctx, h)
if err != nil {
b.logger.Error("error handling job", slog.Any("error", err))
Expand Down Expand Up @@ -349,8 +349,3 @@ func (b *RedisBackend) Shutdown(_ context.Context) {
b.client.Close()
b.server.Shutdown()
}

// withJobContext creates a new context with the Job set
func withJobContext(ctx context.Context, j *jobs.Job) context.Context {
return context.WithValue(ctx, internal.JobCtxVarKey, j)
}
5 changes: 5 additions & 0 deletions jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func FingerprintJob(j *Job) (err error) {
return
}

// WithJobContext adds a job to the provided context
func WithJobContext(ctx context.Context, job *Job) context.Context {
return context.WithValue(ctx, internal.JobCtxVarKey, job)
}

// FromContext fetches the job from a context if the job context variable is set
func FromContext(ctx context.Context) (j *Job, err error) {
var ok bool
Expand Down
Loading