-
Notifications
You must be signed in to change notification settings - Fork 93
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
Preinitialize workCancel
function in case StopAndCancel
called before start
#557
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you!
@brandur oh, probably deserves a changelog entry too |
@@ -491,6 +491,7 @@ func NewClient[TTx any](driver riverdriver.Driver[TTx], config *Config) (*Client | |||
uniqueInserter: baseservice.Init(archetype, &dbunique.UniqueInserter{ | |||
AdvisoryLockPrefix: config.AdvisoryLockPrefix, | |||
}), | |||
workCancel: func(cause error) {}, // replaced on start, but here in case StopAndCancel is called before start up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for such usecase I would define a defautWorkCanceler := func(error) {}
Then use it here, it helps to document separately, and using
workCancel: func(cause error) {}, // replaced on start, but here in case StopAndCancel is called before start up | |
workCancel: defautWorkCanceler // replaced on start |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not sure I agree on that one :) You're never going to reuse this function, and I think adding a named variable for it introduces a layer of indirection that in the end just serves to make it less clear what the code's doing.
…fore start This one's related to #549. Although the most proximate problem in that repro code is that there was no error check when calling `Start`, it did reveal a legitimate problem in that the River client will panic in case `StopAndCancel` is called before `Start` because `workCancel` was never set. Here, initialize `workCancel` in the client's constructor. During normal operation this will be overwritten almost immediately on `Start` as the client starts up, but in case `Start` was never called or didn't run successfully, it provides a function for `StopAndCancel` to call so that it doesn't panic. Fixes #549.
4886b73
to
35623ec
Compare
Added. |
This one's related to #549. Although the most proximate problem in that
repro code is that there was no error check when calling
Start
, it didreveal a legitimate problem in that the River client will panic in case
StopAndCancel
is called beforeStart
becauseworkCancel
was neverset.
Here, initialize
workCancel
in the client's constructor. During normaloperation this will be overwritten almost immediately on
Start
as theclient starts up, but in case
Start
was never called or didn't runsuccessfully, it provides a function for
StopAndCancel
to call so thatit doesn't panic.
Fixes #549.