mirror of
https://github.com/acaloiaro/neoq
synced 2026-07-21 10:12:18 +00:00
feat: Add public jobs.WithJobContext()
This commit is contained in:
parent
37b6732ad0
commit
a6f7fbcd36
4 changed files with 8 additions and 18 deletions
|
|
@ -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",
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()) {
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue