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

@Profile annotation is not working on @WorkflowImpl with workers-auto-discovery #2121

Open
hedonihilist opened this issue Jun 21, 2024 · 4 comments

Comments

@hedonihilist
Copy link

hedonihilist commented Jun 21, 2024

Expected Behavior

When workers-auto-discovery is configured and @Profile annotation is used on top of @WorkflowImpl, if the profile condition doesn't match, the workflow should not be used.

Actual Behavior

Regardless of the condition in @Profile all classes annotated with @WorkflowImpl will be used.

Steps to Reproduce the Problem

  1. Create a workflow and its implementation SomeWorkflowImpl
  2. Annotate SomeWorkflowImpl with @WorkflowImpl and @Profile("!local")
  3. Run the application with local spring profile. The SomeWorkflowImpl can still be discovered and workers will be created.

Specifications

  • Version: temporal-spring-boot-autoconfigure-alpha-1.18.2.jar
  • Platform: mac

Some investigations

ClassPathScanningCandidateComponentProvider is used to scan the package to find workflows.

  private Collection<Class<?>> autoDiscoverWorkflowImplementations() {
    ClassPathScanningCandidateComponentProvider scanner =
        new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(WorkflowImpl.class));
    Set<Class<?>> implementations = new HashSet<>();
    for (String pckg : properties.getWorkersAutoDiscovery().getPackages()) {
      Set<BeanDefinition> candidateComponents = scanner.findCandidateComponents(pckg);
      for (BeanDefinition beanDefinition : candidateComponents) {
        try {
          implementations.add(Class.forName(beanDefinition.getBeanClassName()));
        } catch (ClassNotFoundException e) {
          throw new BeanDefinitionValidationException(
              "Fail loading class for bean definition " + beanDefinition, e);
        }
      }
    }
    return implementations;
  }

But it seems the Environment is not passed in. According to spring doc
The Environment is need to evaluate @Conditional annotations.
image

@Quinn-With-Two-Ns
Copy link
Contributor

Profile effects what beans are included, technically workflow classes are not beans by intention, but I think it would be resonable here to respect Profile. Are you aware of any other spring boot projects that apply Profile to non beans?

@hedonihilist
Copy link
Author

Are you aware of any other spring boot projects that apply Profile to non beans?

No I haven't know whether there're such projects. I am not saying we should do that but there seems to be missing some Spring-aware configurations to dynamically disable some task queue/workers. Maybe This can be achieved by some configuration beans before starting the workers?

What I am doing now is set the auto start worker to false, and manually register the conditional workers to worker factory. Maybe this kind of operations can be configured by some beans?

@hedonihilist
Copy link
Author

Or could you give me some advice here on what's the recommend way to conditionally enable/disable workflows?

@Quinn-With-Two-Ns
Copy link
Contributor

Your options currently are to use explicit configuration or put them in separate packages

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

No branches or pull requests

2 participants