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

SpringBoot - Add registered workflow and activity impl info to workers template #1986

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class WorkersTemplate implements BeanFactoryAware, EnvironmentAware {

private WorkerFactory workerFactory;
private Collection<Worker> workers;
private final Map<String, RegisteredInfo> registeredInfo = new HashMap<>();

public WorkersTemplate(
@Nonnull TemporalProperties properties,
Expand Down Expand Up @@ -111,6 +112,13 @@ public Collection<Worker> getWorkers() {
return workers;
}

public Map<String, RegisteredInfo> getRegisteredInfo() {
Quinn-With-Two-Ns marked this conversation as resolved.
Show resolved Hide resolved
if (workers == null) {
this.workers = createWorkers(getWorkerFactory());
}
return registeredInfo;
}

WorkerFactory createWorkerFactory(WorkflowClient workflowClient) {
if (testWorkflowEnvironment != null) {
return testWorkflowEnvironment.getWorkerFactory();
Expand Down Expand Up @@ -170,7 +178,7 @@ private void configureWorkflowImplementationsByTaskQueue(
worker = createNewWorker(taskQueue, null, workers);
}

configureWorkflowImplementationAutoDiscovery(worker, clazz, null);
configureWorkflowImplementationAutoDiscovery(worker, clazz, null, workers);
}
}
}
Expand All @@ -197,7 +205,7 @@ private void configureActivityBeansByTaskQueue(
}

configureActivityImplementationAutoDiscovery(
worker, bean, beanName, targetClass, null);
worker, bean, beanName, targetClass, null, workers);
}
}
});
Expand All @@ -218,7 +226,7 @@ private void configureWorkflowImplementationsByWorkerName(
+ clazz);
}

configureWorkflowImplementationAutoDiscovery(worker, clazz, workerName);
configureWorkflowImplementationAutoDiscovery(worker, clazz, workerName, workers);
}
}
}
Expand All @@ -241,7 +249,7 @@ private void configureActivityBeansByWorkerName(
}

configureActivityImplementationAutoDiscovery(
worker, bean, beanName, targetClass, workerName);
worker, bean, beanName, targetClass, workerName, workers);
}
}
});
Expand Down Expand Up @@ -303,14 +311,21 @@ private void createWorkerFromAnExplicitConfig(
AopUtils.getTargetClass(bean),
taskQueue);
worker.registerActivitiesImplementations(bean);
addRegisteredActivityImpl(worker, bean.getClass().getName());
});
}
}

private void configureActivityImplementationAutoDiscovery(
Worker worker, Object bean, String beanName, Class<?> targetClass, String byWorkerName) {
Worker worker,
Object bean,
String beanName,
Class<?> targetClass,
String byWorkerName,
Workers workers) {
try {
worker.registerActivitiesImplementations(bean);
addRegisteredActivityImpl(worker, bean.getClass().getName());
if (log.isInfoEnabled()) {
log.info(
"Registering auto-discovered activity bean '{}' of class {} on a worker {}with a task queue '{}'",
Expand All @@ -334,7 +349,7 @@ private void configureActivityImplementationAutoDiscovery(
}

private void configureWorkflowImplementationAutoDiscovery(
Worker worker, Class<?> clazz, String byWorkerName) {
Worker worker, Class<?> clazz, String byWorkerName, Workers workers) {
try {
configureWorkflowImplementation(worker, clazz);
if (log.isInfoEnabled()) {
Expand Down Expand Up @@ -378,6 +393,7 @@ private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz)
(Class<T>) workflowMethod.getWorkflowInterface(),
() -> (T) beanFactory.createBean(clazz),
workflowImplementationOptions);
addRegisteredWorkflowImpl(worker, workflowMethod.getWorkflowInterface().getName());
}
}

Expand Down Expand Up @@ -410,6 +426,47 @@ private Worker createNewWorker(
return worker;
}

private void addRegisteredWorkflowImpl(Worker worker, String workflowClass) {
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
registeredInfo.put(
worker.getTaskQueue(), new RegisteredInfo().addWorkflowInfo(workflowClass));
} else {
registeredInfo.get(worker.getTaskQueue()).getRegisteredWorkflowInfo().add(workflowClass);
}
}

private void addRegisteredActivityImpl(Worker worker, String activityClass) {
if (!registeredInfo.containsKey(worker.getTaskQueue())) {
registeredInfo.put(
worker.getTaskQueue(), new RegisteredInfo().addActivityInfo(activityClass));
} else {
registeredInfo.get(worker.getTaskQueue()).getRegisteredActivityInfo().add(activityClass);
}
}

public static class RegisteredInfo {
private final List<String> registeredActivityInfo = new ArrayList<>();
private final List<String> registeredWorkflowInfo = new ArrayList<>();

public RegisteredInfo addActivityInfo(String activityClass) {
registeredActivityInfo.add(activityClass);
return this;
}

public RegisteredInfo addWorkflowInfo(String workflowClass) {
registeredWorkflowInfo.add(workflowClass);
return this;
}

public List<String> getRegisteredActivityInfo() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think there is other information users may want us to expose in the future about activities or workflows other then their name?

Copy link
Contributor

Choose a reason for hiding this comment

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

May make sense to return a list of objects so we can add new fields if decided to expose more info.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok good point, will update

return registeredActivityInfo;
}

public List<String> getRegisteredWorkflowInfo() {
return registeredWorkflowInfo;
}
}

private static class Workers {
private final Map<String, Worker> workersByName = new HashMap<>();
private final Map<String, Worker> workersByTaskQueue = new HashMap<>();
Expand Down
Loading