Skip to content

Commit

Permalink
Merge pull request #47 from agorapulse/feature/default-executor
Browse files Browse the repository at this point in the history
ability to set default executor
  • Loading branch information
musketyr authored Nov 29, 2024
2 parents fe7b451 + 1ce4c67 commit 15c126b
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 17 deletions.
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

0 comments on commit 15c126b

Please sign in to comment.