Closed koesie10 closed 6 years ago
I don't like the idea of job-specific middleware. Global middleware is easy to understand and grok. Job-specific middleware is better designed into the job itself.
Sorry if that started overly negative. Thank you for this work, this is nice to see.
You are right, job-level middleware don't make too much sense. I've removed them from the commit.
Middleware usually acts on attributes in job.Custom. You will want to expose the job's custom attributes to the middleware (but hide the custom data from the actual perform method, for separation of concerns).
I've made some changes that allow middleware to access the complete Job
struct. The way this middleware is implemented requires every handler function to be the same, so such a handler is constructed in Register
that wraps the Perform
function. I'm not completely happy with this solution, but right now I can't think of a better way to make sure the perform function doesn't have access to complete job information, while allowing middleware the access.
This looks like a great start. Further polish is always welcome.
Thank you!
I added chained middleware support on manager and job type level, so middleware can also be added on an individual job. I also added
context.JobType()
to allow retrieving the job type because it can be useful in middleware.Example usage:
When applied in the
test
package, it will transform this output:Into this output:
Example usage with Logrus and only logging errors: