-
Notifications
You must be signed in to change notification settings - Fork 111
Add new HookWorkEnd
interface that runs after workers finish
#863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brandur
added a commit
to riverqueue/rivercontrib
that referenced
this pull request
Apr 26, 2025
…terfaces This one's aimed at helping people debug problems like the one raised here [1]. There's a common mistake in Go where a nil error-compliant struct is wrapped in a non-nil error interface and unexpectedly caught by a `if err != nil { ... }` check. For example: type CustomError { InternalErr error } type JobWorker struct { river.WorkerDefaults[workers.JobWorkerArgs } func (jw *JobWorker) Work(ctx context.Context, job *river.Job[workers.JobWorkerArgs]) error { var customErr *CustomError return customErr } This is such a common problem in Go that the language maintains a long FAQ on the topic: https://go.dev/doc/faq#nil_error We probably don't want to bake a solution for this right into core River because there'd be huge room for disagreement for what to do about it. Purists would not want the case handled because technically Go is behaving the way that it's supposed to. Beyond that, in case a problem is found, it's debatable whether an error should be emitted (so it can be easily caught in tests) or just logged. Here, we add a hook in `rivercontrib` as a nice compromise. People who want extra checks on this problem can easily configure it, and everyone else can ignore it. This requires a new `HookWorkEnd` interface in core River to work [2]. It could've been added as a middleware instead, but it seems to me that a lighter check that doesn't go on the stack in a hook is the better approach. [1] riverqueue/river#806 [2] riverqueue/river#863
de63d92
to
61f2272
Compare
@bgentry |
ce33430
to
1c2173f
Compare
Okay, actually I ended up putting some tests in. |
Here, add a new complimentary pair for `HookWorkBegin`: `HookWorkEnd`, which runs after workers finish, taking in an error result. `HookWorkEnd` hooks may or may not modify the error result, choosing to suppress an error on pass it along the stack unchanged. This is driven by trying to add a new `nilerror` contrib package [1] that helps detect nil error-compliant structs that return non-nil error interfaces, which is a common footgun in Go [2]. [1] riverqueue/rivercontrib#25 [2] https://go.dev/doc/faq#nil_error
1c2173f
to
bc8d166
Compare
bgentry
approved these changes
Apr 28, 2025
ty ty. |
brandur
added a commit
to riverqueue/rivercontrib
that referenced
this pull request
May 3, 2025
…terfaces This one's aimed at helping people debug problems like the one raised here [1]. There's a common mistake in Go where a nil error-compliant struct is wrapped in a non-nil error interface and unexpectedly caught by a `if err != nil { ... }` check. For example: type CustomError { InternalErr error } type JobWorker struct { river.WorkerDefaults[workers.JobWorkerArgs } func (jw *JobWorker) Work(ctx context.Context, job *river.Job[workers.JobWorkerArgs]) error { var customErr *CustomError return customErr } This is such a common problem in Go that the language maintains a long FAQ on the topic: https://go.dev/doc/faq#nil_error We probably don't want to bake a solution for this right into core River because there'd be huge room for disagreement for what to do about it. Purists would not want the case handled because technically Go is behaving the way that it's supposed to. Beyond that, in case a problem is found, it's debatable whether an error should be emitted (so it can be easily caught in tests) or just logged. Here, we add a hook in `rivercontrib` as a nice compromise. People who want extra checks on this problem can easily configure it, and everyone else can ignore it. This requires a new `HookWorkEnd` interface in core River to work [2]. It could've been added as a middleware instead, but it seems to me that a lighter check that doesn't go on the stack in a hook is the better approach. [1] riverqueue/river#806 [2] riverqueue/river#863
brandur
added a commit
to riverqueue/rivercontrib
that referenced
this pull request
May 3, 2025
…terfaces This one's aimed at helping people debug problems like the one raised here [1]. There's a common mistake in Go where a nil error-compliant struct is wrapped in a non-nil error interface and unexpectedly caught by a `if err != nil { ... }` check. For example: type CustomError { InternalErr error } type JobWorker struct { river.WorkerDefaults[workers.JobWorkerArgs } func (jw *JobWorker) Work(ctx context.Context, job *river.Job[workers.JobWorkerArgs]) error { var customErr *CustomError return customErr } This is such a common problem in Go that the language maintains a long FAQ on the topic: https://go.dev/doc/faq#nil_error We probably don't want to bake a solution for this right into core River because there'd be huge room for disagreement for what to do about it. Purists would not want the case handled because technically Go is behaving the way that it's supposed to. Beyond that, in case a problem is found, it's debatable whether an error should be emitted (so it can be easily caught in tests) or just logged. Here, we add a hook in `rivercontrib` as a nice compromise. People who want extra checks on this problem can easily configure it, and everyone else can ignore it. This requires a new `HookWorkEnd` interface in core River to work [2]. It could've been added as a middleware instead, but it seems to me that a lighter check that doesn't go on the stack in a hook is the better approach. [1] riverqueue/river#806 [2] riverqueue/river#863
brandur
added a commit
to riverqueue/rivercontrib
that referenced
this pull request
May 3, 2025
…terfaces (#25) This one's aimed at helping people debug problems like the one raised here [1]. There's a common mistake in Go where a nil error-compliant struct is wrapped in a non-nil error interface and unexpectedly caught by a `if err != nil { ... }` check. For example: type CustomError { InternalErr error } type JobWorker struct { river.WorkerDefaults[workers.JobWorkerArgs } func (jw *JobWorker) Work(ctx context.Context, job *river.Job[workers.JobWorkerArgs]) error { var customErr *CustomError return customErr } This is such a common problem in Go that the language maintains a long FAQ on the topic: https://go.dev/doc/faq#nil_error We probably don't want to bake a solution for this right into core River because there'd be huge room for disagreement for what to do about it. Purists would not want the case handled because technically Go is behaving the way that it's supposed to. Beyond that, in case a problem is found, it's debatable whether an error should be emitted (so it can be easily caught in tests) or just logged. Here, we add a hook in `rivercontrib` as a nice compromise. People who want extra checks on this problem can easily configure it, and everyone else can ignore it. This requires a new `HookWorkEnd` interface in core River to work [2]. It could've been added as a middleware instead, but it seems to me that a lighter check that doesn't go on the stack in a hook is the better approach. [1] riverqueue/river#806 [2] riverqueue/river#863
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Here, add a new complimentary pair for
HookWorkBegin
:HookWorkEnd
,which runs after workers finish, taking in an error result.
HookWorkEnd
hooks may or may not modify the error result, choosing to suppress an
error on pass it along the stack unchanged.
This is driven by trying to add a new
nilerror
contrib package [1]that helps detect nil error-compliant structs that return non-nil error
interfaces, which is a common footgun in Go [2].
[1] riverqueue/rivercontrib#25
[2] https://go.dev/doc/faq#nil_error