Skip to content

Commit

Permalink
feat: Add public jobs.WithJobContext()
Browse files Browse the repository at this point in the history
  • Loading branch information
acaloiaro authored and pconstantinou committed Jun 26, 2024
1 parent e3d6237 commit f5e7ee8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 18 deletions.
7 changes: 1 addition & 6 deletions backends/memory/memory_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,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 @@ -391,8 +391,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 @@ -939,7 +939,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.RunAfter.After(time.Now()) {
Expand Down Expand Up @@ -1131,11 +1131,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 @@ -253,7 +253,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 @@ -360,8 +360,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 @@ -77,6 +77,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

0 comments on commit f5e7ee8

Please sign in to comment.