Inject worker metadata to pilot + testable job args#996
Conversation
| } | ||
| }) | ||
|
|
||
| AddWorkerArgs(bundle.config.Workers, jobArgs, WorkFunc(func(ctx context.Context, job *Job[JobArgsWithHooksFunc]) error { |
There was a problem hiding this comment.
See here and the red below for how AddWorkerArgs helps to remove boilerplate and bring implementation closer to the specific test case, but the more critical piece is that unless a hook has some "hack" workaround available like being able to add information to metadata (like this test case was doing), I couldn't find any other way to test hooks that are specific to only some job args.
The trouble is that AddWorker always implicitly constructs a JobArgs like:
func AddWorkerSafely[T JobArgs](workers *Workers, worker Worker[T]) error {
var jobArgs T
return workers.add(jobArgs, &workUnitFactoryWrapper[T]{worker: worker})
}Which makes attaching a specific Hooks() implementation to it for a single test case impossible.
By bringing in something like AddWorkerArgs which is implemented like:
func AddWorkerArgs[T JobArgs](workers *Workers, jobArgs T, worker Worker[T]) {
if err := workers.add(jobArgs, &workUnitFactoryWrapper[T]{worker: worker}); err != nil {
panic(err)
}
}It lets us do what you see above, with a closure hook implementation that can set a local variable and be tested against.
The only other way I could think to do it is to have a job args specific to every test case and which sets a global variable, which sucks. So the only options I'm able to come up with are:
AddWorkerArgsas you see here.- Job args for each test case that set a global (or use a workaround like middleware if that's available).
- Don't test the hooks on specific job args case.
There was a problem hiding this comment.
I should note also that I'm not married to this at all, but for the life of me could not find a better way.
There was a problem hiding this comment.
I don't have a better idea unfortunately, annoying problem to have to hack around 😕
There was a problem hiding this comment.
K thx. And yeah :( We'll have a bit of time ahead of release probably. I'll keep mulling it over.
It'd be nice if Go allowed you to mark some API is "pretty internal, you probably don't want to use this man" or something of that nature even and at least hide it from Godoc or something. Couldn't think of any tricks here though unfortunately.
| // AddWorkerArgs is the same as AddWorker except that it lets args be passed | ||
| // explicitly rather than being instantiated implicitly. This function is | ||
| // largely for internal use only and shoudl be used only in tests. | ||
| func AddWorkerArgs[T JobArgs](workers *Workers, jobArgs T, worker Worker[T]) { |
There was a problem hiding this comment.
Didn't put in a *Safely variant — I figure if we're going to add this variant that's not really suitable for normal use anyway, there's no point in multiplicatively expanding the API, especially when *Safely is very likely rarely ever used anyway.
84dfa87 to
f39e9e7
Compare
| } | ||
| }) | ||
|
|
||
| AddWorkerArgs(bundle.config.Workers, jobArgs, WorkFunc(func(ctx context.Context, job *Job[JobArgsWithHooksFunc]) error { |
There was a problem hiding this comment.
I don't have a better idea unfortunately, annoying problem to have to hack around 😕
Here, add a mechanism that has the client inject worker metadata into pilots. This gives pilots information about what jobs/workers are registered, and helps in cases where say we want pilots to be able to examine things like `Hooks()` implementations on each worker. I also ended up adding a function for `river.AddWorkerArgs` that lets a concrete args object be injected into the registration process rather than one that's implicitly allocated. I'm a little partial on this one because it'd be better if it wasn't necessary, but it cleans up tests somewhat that are trying to test specific things on args like `Hooks()`, and make tests on features like that _possible_ when there's no workaround available like storing information that'll be persisted to a job's metadata (there's otherwise no good way to carry them out, with the only way I can think of a global variable)
2bbc58b to
1dd8817
Compare
|
Pushed a small change to add a cursory test for |
|
Thanks! |
Here, add a mechanism that has the client inject worker metadata into
pilots. This gives pilots information about what jobs/workers are
registered, and helps in cases where say we want pilots to be able to
examine things like
Hooks()implementations on each worker.I also ended up adding a function for
river.AddWorkerArgsthat lets aconcrete args object be injected into the registration process rather
than one that's implicitly allocated. I'm a little partial on this one
because it'd be better if it wasn't necessary, but it cleans up tests
somewhat that are trying to test specific things on args like
Hooks(),and make tests on features like that possible when there's no
workaround available like storing information that'll be persisted to a
job's metadata (there's otherwise no good way to carry them out, with
the only way I can think of a global variable)