contribsys / faktory_worker_go

Faktory workers for Go
Mozilla Public License 2.0
242 stars 43 forks source link

Context doesn't allow WithValue() #38

Closed mperham closed 4 years ago

mperham commented 4 years ago

fwg's middleware is meant to provide a Context that the user can use to setup contextual data for the given job:

func SetupContext(ctx worker.Context, job *faktory.Job) error {
  aid := job.GetCustom("account_id")
  uid := job.GetCustom("user_id")
  ctx.WithValue("aid", aid).WithValue("uid", uid)
  return nil
}

mgr.Use(SetupContext)

But WithValue returns a copy of the context and it's impossible to pass on the new copy to the job execution. We'll likely need to adjust the Middleware API to something like this:

func SetupContext(ctx worker.Context, job *faktory.Job) (Context, error) {
  ...
  return ctx.WithValue("aid", aid).WithValue("uid", uid), nil
}

so that modified Contexts can be passed along the chain.

cocktailer commented 4 years ago

Now I can use context to set the value after https://github.com/contribsys/faktory_worker_go/commit/490cb2a66d8cdc6b8f2aafb7d0a88d6edbd7a82d.

We can close this. @mperham Thanks