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

ability to set default executor #47

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions docs/guide/src/docs/asciidoc/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ You can set the default queue type using the `worker.queue-type` configuration p
include::{root-dir}/libs/micronaut-worker/src/test/resources/application-local.yml[]
----

You can override the default scheduler (`TaskExecutors.SCHEDULED`) by setting the `worker.scheduler` property.

[source,yaml]
.Setting the Default Scheduler
----
include::{root-dir}/libs/micronaut-worker/src/test/resources/application-virtual.yml[]
----

TIP: You can let your jobs executed using virtual threads by using `virtual` executor.

=== Job Configuration

Anything you can configure using annotations can be configured externally.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
*/
package com.agorapulse.worker;

import io.micronaut.scheduling.TaskExecutors;

public interface WorkerConfiguration {

String DEFAULT_SCHEDULER = TaskExecutors.SCHEDULED;

WorkerConfiguration ENABLED = new WorkerConfiguration() {
@Override
public boolean isEnabled() {
Expand All @@ -30,6 +34,11 @@ public String getQueueType() {
return null;
}

@Override
public String getScheduler() {
return DEFAULT_SCHEDULER;
}

};

boolean isEnabled();
Expand All @@ -39,4 +48,6 @@ public String getQueueType() {
*/
String getQueueType();

String getScheduler();

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package com.agorapulse.worker.annotation;

import com.agorapulse.worker.WorkerConfiguration;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand Down Expand Up @@ -49,6 +49,6 @@
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
@AliasFor(annotation = Job.class, member = "scheduler")
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package com.agorapulse.worker.annotation;

import com.agorapulse.worker.WorkerConfiguration;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand Down Expand Up @@ -49,6 +49,6 @@
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
@AliasFor(annotation = Job.class, member = "scheduler")
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package com.agorapulse.worker.annotation;

import com.agorapulse.worker.WorkerConfiguration;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand Down Expand Up @@ -49,6 +49,6 @@
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
@AliasFor(annotation = Job.class, member = "scheduler")
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
package com.agorapulse.worker.annotation;

import com.agorapulse.worker.WorkerConfiguration;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand Down Expand Up @@ -49,6 +49,6 @@
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
@AliasFor(annotation = Job.class, member = "scheduler")
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
*/
package com.agorapulse.worker.annotation;

import com.agorapulse.worker.WorkerConfiguration;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.context.annotation.Executable;
import io.micronaut.context.annotation.Parallel;
import io.micronaut.core.annotation.EntryPoint;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand All @@ -45,7 +45,7 @@
/**
* Allows to override the default name of the job which is <code>JobClassName</code> if there is only one executable
* method (e.g. job definition) in the class or <code>JobClassName-methodName</code> if there is more then one executable method in the class.
*
* <p>
* Either the job name specified here or the default name is converted using {@link io.micronaut.core.naming.NameUtils#hyphenate(String)}.
*
* @return the name of the job used for configuration
Expand Down Expand Up @@ -86,6 +86,6 @@
* @return The name of a {@link jakarta.inject.Named} bean that is a
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.core.util.StringUtils;
import io.micronaut.scheduling.TaskExecutors;

import jakarta.annotation.Nullable;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -133,7 +132,7 @@ public void mergeWith(ConsumerQueueConfiguration overrides) {
@Nullable private Duration fixedDelay;
@Nullable private Duration initialDelay;
@Nullable private Duration fixedRate;
@NotBlank private String scheduler = TaskExecutors.SCHEDULED;
@NotBlank private String scheduler;

@Positive private int fork = 1;

Expand All @@ -147,6 +146,7 @@ public DefaultJobConfiguration(@Parameter String name, WorkerConfiguration worke
this.enabled = workerConfiguration.isEnabled();
this.consumer.setQueueType(workerConfiguration.getQueueType());
this.producer.setQueueType(workerConfiguration.getQueueType());
this.scheduler = workerConfiguration.getScheduler();
this.name = name;
}

Expand Down Expand Up @@ -324,7 +324,7 @@ public JobConfiguration mergeWith(JobConfiguration overrides) {
this.initialDelay = overrides.getInitialDelay();
}

if (overrides.getScheduler() != null && !overrides.getScheduler().equals(TaskExecutors.SCHEDULED)) {
if (overrides.getScheduler() != null && !overrides.getScheduler().equals(WorkerConfiguration.DEFAULT_SCHEDULER)) {
this.scheduler = overrides.getScheduler();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class DefaultWorkerConfiguration implements WorkerConfiguration {

private boolean enabled;
private String queueType;
private String scheduler = WorkerConfiguration.DEFAULT_SCHEDULER;

public DefaultWorkerConfiguration(Environment env) {
// disable for tests and functions
Expand All @@ -51,4 +52,14 @@ public String getQueueType() {
public void setQueueType(String queueType) {
this.queueType = queueType;
}

@Override
public String getScheduler() {
return scheduler;
}

public void setScheduler(String scheduler) {
this.scheduler = scheduler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
*/
package com.agorapulse.worker.convention;

import com.agorapulse.worker.WorkerConfiguration;
import com.agorapulse.worker.annotation.Job;
import com.agorapulse.worker.annotation.Produces;
import io.micronaut.context.annotation.AliasFor;
import io.micronaut.scheduling.TaskExecutors;
import jakarta.inject.Named;

import java.lang.annotation.Documented;
Expand All @@ -46,7 +46,7 @@
/**
* Allows to override the default name of the job which is <code>JobClassName</code> if there is only one executable
* method (e.g. job definition) in the class or <code>JobClassName-methodName</code> if there is more then one executable method in the class.
*
* <p>
* Either the job name specified here or the default name is converted using {@link io.micronaut.core.naming.NameUtils#hyphenate(String)}.
*
* @return the name of the job used for configuration
Expand Down Expand Up @@ -98,6 +98,6 @@
* {@link java.util.concurrent.ScheduledExecutorService} to use to schedule the task
*/
@AliasFor(annotation = Job.class, member = "scheduler")
String scheduler() default TaskExecutors.SCHEDULED;
String scheduler() default WorkerConfiguration.DEFAULT_SCHEDULER;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
worker:
scheduler: virtual