Skip to content
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

add Client to work context, helpers to extract it #145

Merged
merged 1 commit into from
Jan 13, 2024
Merged

Conversation

bgentry
Copy link
Contributor

@bgentry bgentry commented Jan 11, 2024

This change adds the *Client to an unexported context key within the work context when the Client is started, making it available to the context for every job that's worked by that Client.

Additionally, two new helper functions are added to retrieve the Client from the context within a Worker: ClientFromContext and ClientFromContextSafely. The only difference between these is that the first variant panics if the client isn't found in the context, whereas the latter returns an error. However if these are used on the context provided to a Worker's Work() method (or one derived from it) they should never panic/error.

Inspired by #87 and #144.

If we want to move forward with this, it still needs:

  • A changelog entry
  • Website documentation

@bgentry bgentry requested a review from brandur January 11, 2024 15:39
Copy link
Contributor

@brandur brandur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, lgtm.

context_test.go Outdated
require.NoError(t, err)
require.Equal(t, client, result)

require.Panics(t, func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about switching to the use of require.PanicsWithError here? Just makes sure we for sure got the right panic and didn't trigger some other unexpected one by accident.

Copy link
Contributor Author

@bgentry bgentry Jan 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another handy require helper I didn't yet know about, thanks! Changed this test to assert about the specific error throughout, and also added a changelog entry.

This change adds the `*Client` to an unexported context key within the
work context when the Client is started, making it available to the
context for every job that's worked by that Client.

Additionally, two new helper functions are added to retrieve the Client
from the context within a Worker: `ClientFromContext` and
`ClientFromContextSafely`. The only difference between these is that the
first variant panics if the client isn't found in the context, whereas
the latter returns an error. However if these are used on the context
provided to a Worker's `Work()` method (or one derived from it) they
should never panic/error.
@bgentry
Copy link
Contributor Author

bgentry commented Jan 13, 2024

I'm working on a docs PR but no reason not to merge this in the mean time.

@bgentry bgentry merged commit 8cdb5e7 into master Jan 13, 2024
7 checks passed
@bgentry bgentry deleted the bg-context-client branch January 13, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants