-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): worker resource dependency injection #340
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aceb655
feat(java): worker resources (dependency injection)
pratyush618 ccf9c32
test(java): cover worker resources
pratyush618 52b308a
fix(java): keep the 3-arg Worker.builder overload
pratyush618 8daed75
fix(java): give each worker its own resource runtime
pratyush618 882c718
fix(java): reject duplicate resource registration
pratyush618 33e0dea
fix(java): fail fast on circular resource dependencies
pratyush618 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
18 changes: 18 additions & 0 deletions
18
sdks/java/src/main/java/org/byteveda/taskito/errors/ResourceException.java
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,18 @@ | ||
| package org.byteveda.taskito.errors; | ||
|
|
||
| import org.byteveda.taskito.TaskitoException; | ||
|
|
||
| /** | ||
| * A worker resource could not be built, resolved, or disposed — e.g. a factory | ||
| * threw, an unknown resource name was requested, or {@code Resources.use} was | ||
| * called outside a task handler. | ||
| */ | ||
| public class ResourceException extends TaskitoException { | ||
| public ResourceException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| public ResourceException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
sdks/java/src/main/java/org/byteveda/taskito/internal/ScopeContext.java
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,31 @@ | ||
| package org.byteveda.taskito.internal; | ||
|
|
||
| /** | ||
| * A single seam for per-task context propagation. Backed by a {@link ThreadLocal} | ||
| * today (the SDK targets Java 17); if the floor later rises to a JDK with a | ||
| * stable {@code ScopedValue}, only this class changes — callers are unaffected. | ||
| * | ||
| * <p>Each task runs on a pooled worker thread, so callers must {@link #set} on | ||
| * entry and {@link #clear} in a {@code finally} on exit to avoid leaking context | ||
| * into the next task scheduled on that thread. | ||
| * | ||
| * @param <T> the value carried for the duration of a task | ||
| */ | ||
| public final class ScopeContext<T> { | ||
| private final ThreadLocal<T> holder = new ThreadLocal<>(); | ||
|
|
||
| /** The value bound on the current thread, or {@code null} if none. */ | ||
| public T get() { | ||
| return holder.get(); | ||
| } | ||
|
|
||
| /** Bind {@code value} on the current thread. */ | ||
| public void set(T value) { | ||
| holder.set(value); | ||
| } | ||
|
|
||
| /** Unbind any value on the current thread. */ | ||
| public void clear() { | ||
| holder.remove(); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceContext.java
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,14 @@ | ||
| package org.byteveda.taskito.resources; | ||
|
|
||
| /** | ||
| * Handed to a resource factory so it can depend on other resources. A | ||
| * {@code WORKER} factory may only {@link #use} other worker resources; a | ||
| * {@code TASK} factory may use either. | ||
| */ | ||
| public interface ResourceContext { | ||
| /** The scope the factory is building for. */ | ||
| ResourceScope scope(); | ||
|
|
||
| /** Resolve another resource by name (building it if needed). */ | ||
| <T> T use(String name); | ||
| } |
34 changes: 34 additions & 0 deletions
34
sdks/java/src/main/java/org/byteveda/taskito/resources/ResourceDefinition.java
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,34 @@ | ||
| package org.byteveda.taskito.resources; | ||
|
|
||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * How to build (and optionally dispose) a resource. | ||
| * | ||
| * @param factory builds the resource, possibly using others via the context | ||
| * @param scope the resource's lifetime (defaults to {@link ResourceScope#WORKER}) | ||
| * @param dispose optional cleanup run when the scope ends ({@code null} for none) | ||
| */ | ||
| public record ResourceDefinition( | ||
| Function<ResourceContext, Object> factory, ResourceScope scope, Consumer<Object> dispose) { | ||
|
|
||
| public ResourceDefinition { | ||
| if (factory == null) { | ||
| throw new IllegalArgumentException("resource factory must not be null"); | ||
| } | ||
| if (scope == null) { | ||
| scope = ResourceScope.WORKER; | ||
| } | ||
| } | ||
|
|
||
| /** A worker-scoped resource with no disposer. */ | ||
| public static ResourceDefinition worker(Function<ResourceContext, Object> factory) { | ||
| return new ResourceDefinition(factory, ResourceScope.WORKER, null); | ||
| } | ||
|
|
||
| /** A task-scoped resource with no disposer. */ | ||
| public static ResourceDefinition task(Function<ResourceContext, Object> factory) { | ||
| return new ResourceDefinition(factory, ResourceScope.TASK, null); | ||
| } | ||
| } |
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.