-
Notifications
You must be signed in to change notification settings - Fork 158
Add helper for job metadata updates #1269
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
Open
peter941221
wants to merge
12
commits into
riverqueue:master
Choose a base branch
from
peter941221:peter/1218-set-metadata-helper
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8929dfc
Clarify worker cancellation requirements for stuck jobs
peter941221 3c16aab
Tighten stuck-job cancellation docs wording
peter941221 b2019c8
Trim broad stuck-job doc additions
peter941221 1f45624
Add helper for job metadata updates
peter941221 4df8313
Add middleware coverage for SetMetadata
peter941221 c250670
Relax stop test timeout under race
peter941221 8d180ba
Add CLI custom schema migration coverage
peter941221 d2ca142
Align CLI custom schema test with repo rules
peter941221 afae714
Narrow SetMetadata PR scope
peter941221 2ad8369
Merge remote-tracking branch 'upstream/master' into peter/1218-set-me…
peter941221 8db022b
Trigger CI rerun
peter941221 f81b143
Cover HookWorkBegin metadata usage
peter941221 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package river | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
|
|
||
| "github.com/riverqueue/river/internal/jobexecutor" | ||
| ) | ||
|
|
||
| var errMetadataNotSettable = errors.New("SetMetadata must be called within a worker, worker middleware, or work hook") | ||
|
|
||
| // SetMetadata records a metadata value to be merged into the job's metadata | ||
| // when the current work attempt finishes. | ||
| // | ||
| // This function is only valid from a worker, worker middleware, or work hook | ||
| // like rivertype.HookWorkBegin or rivertype.HookWorkEnd. | ||
| // | ||
| // Metadata updates are stored on the work context and merged into the job row | ||
| // when the current work attempt finishes, whether the attempt succeeds or | ||
| // errors. Values must be JSON marshalable because metadata is stored in a | ||
| // jsonb column, and setting a key replaces any existing value at that key. | ||
| // | ||
| // Keys prefixed with `river:` are reserved for internal use and may not be set | ||
| // by user code. | ||
| func SetMetadata(ctx context.Context, key string, value any) error { | ||
| if strings.HasPrefix(key, "river:") { | ||
| return errors.New("SetMetadata cannot be used with keys prefixed with `river:`") | ||
| } | ||
|
|
||
| metadataUpdates, ok := jobexecutor.MetadataUpdatesFromWorkContext(ctx) | ||
| if !ok { | ||
| return errMetadataNotSettable | ||
| } | ||
|
|
||
| metadataUpdates[key] = value | ||
| return nil | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package river | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/riverqueue/river/internal/jobexecutor" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestSetMetadata(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| t.Run("RejectsReservedPrefix", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := context.WithValue(context.Background(), jobexecutor.ContextKeyMetadataUpdates, map[string]any{}) | ||
|
|
||
| err := SetMetadata(ctx, "river:reserved", "value") | ||
| require.EqualError(t, err, "SetMetadata cannot be used with keys prefixed with `river:`") | ||
| }) | ||
|
|
||
| t.Run("RequiresWorkContext", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| err := SetMetadata(context.Background(), "key", "value") | ||
| require.EqualError(t, err, "SetMetadata must be called within a worker, worker middleware, or work hook") | ||
| }) | ||
|
|
||
| t.Run("SetsValueOnWorkContext", func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| metadataUpdates := map[string]any{} | ||
| ctx := context.WithValue(context.Background(), jobexecutor.ContextKeyMetadataUpdates, metadataUpdates) | ||
|
|
||
| err := SetMetadata(ctx, "key", "value") | ||
| require.NoError(t, err) | ||
| require.Equal(t, "value", metadataUpdates["key"]) | ||
| }) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.