Skip to content

Commit

Permalink
#29498 Create NoRetryPolicy and DefaultRetryStrategy annotations
Browse files Browse the repository at this point in the history
Added @NoRetryPolicy to explicitly define no-retry behavior for job processors. Introduced @DefaultRetryStrategy for marking the default retry strategy implementation. Updated relevant classes to utilize these annotations for better code readability and maintainability.
  • Loading branch information
jgambarios committed Oct 23, 2024
1 parent e999e20 commit ff5a516
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.dotcms.jobs.business.job.JobState;
import com.dotcms.jobs.business.processor.Cancellable;
import com.dotcms.jobs.business.processor.DefaultProgressTracker;
import com.dotcms.jobs.business.processor.DefaultRetryStrategy;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.processor.ProgressTracker;
import com.dotcms.jobs.business.queue.JobQueue;
Expand Down Expand Up @@ -143,7 +144,7 @@ public class JobQueueManagerAPIImpl implements JobQueueManagerAPI {
public JobQueueManagerAPIImpl(JobQueue jobQueue,
JobQueueConfig jobQueueConfig,
CircuitBreaker circuitBreaker,
RetryStrategy defaultRetryStrategy,
@DefaultRetryStrategy RetryStrategy defaultRetryStrategy,
RealTimeJobMonitor realTimeJobMonitor,
EventProducer eventProducer,
JobProcessorFactory jobProcessorFactory,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.dotcms.jobs.business.error;

import com.dotcms.jobs.business.job.Job;
import java.util.Collections;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;

/**
* Implements a no-retry strategy for job processors that should never retry failed jobs. This
* strategy always returns false for shouldRetry and maintains an empty set of non-retryable
* exceptions since retries are never attempted.
*/
@ApplicationScoped
public class NoRetryStrategy implements RetryStrategy {

@Override
public boolean shouldRetry(Job job, Class<? extends Throwable> exceptionClass) {
return false; // Never retry
}

@Override
public long nextRetryDelay(Job job) {
return 0; // Not used since retries never occur
}

@Override
public int maxRetries() {
return 0;
}

@Override
public boolean isNonRetryableException(Class<? extends Throwable> exceptionClass) {
return true; // All exceptions are considered non-retryable
}

@Override
public void addNonRetryableException(Class<? extends Throwable> exceptionClass) {
// No-op since all exceptions are already non-retryable
}

@Override
public Set<Class<? extends Throwable>> getNonRetryableExceptions() {
return Collections.emptySet(); // No need to track specific exceptions
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.dotcms.jobs.business.processor.ExponentialBackoffRetryPolicy;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.processor.NoRetryPolicy;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

/**
* Processes retry policies for job processors. This class is responsible for interpreting retry
Expand All @@ -14,30 +16,39 @@
@ApplicationScoped
public class RetryPolicyProcessor {

private final NoRetryStrategy noRetryStrategy;

@Inject
public RetryPolicyProcessor(NoRetryStrategy noRetryStrategy) {
this.noRetryStrategy = noRetryStrategy;
}

/**
* Processes the retry policy for a given job processor class.
* <p>
* Currently supports ExponentialBackoffRetryPolicy.
* Currently supports ExponentialBackoffRetryPolicy and NoRetryPolicy.
*
* @param processorClass The class of the job processor to process
* @param processorClass The class of the job processor to process.
* @return A RetryStrategy based on the annotation present on the processor class, or null if no
* supported annotation is found
* supported annotation is found.
*
* @see ExponentialBackoffRetryPolicy
* @see NoRetryPolicy
*/
public RetryStrategy processRetryPolicy(Class<? extends JobProcessor> processorClass) {

// Check for NoRetryPolicy
if (processorClass.isAnnotationPresent(NoRetryPolicy.class)) {
return noRetryStrategy;
}

// Check for ExponentialBackoffRetryPolicy
if (processorClass.isAnnotationPresent(ExponentialBackoffRetryPolicy.class)) {
return processExponentialBackoffPolicy(
processorClass.getAnnotation(ExponentialBackoffRetryPolicy.class)
);
}

// Add checks for other retry policy annotations here in the future
// For example:
// if (processorClass.isAnnotationPresent(SomeOtherRetryPolicy.class)) {
// return processSomeOtherPolicy(processorClass.getAnnotation(SomeOtherRetryPolicy.class));
// }

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotcms.jobs.business.error;

import com.dotcms.jobs.business.processor.DefaultRetryStrategy;
import com.dotmarketing.util.Config;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class RetryStrategyProducer {
* @return An ExponentialBackoffRetryStrategy instance configured with the default values.
*/
@Produces
@DefaultRetryStrategy
public RetryStrategy produceDefaultRetryStrategy() {
return new ExponentialBackoffRetryStrategy(
DEFAULT_RETRY_STRATEGY_INITIAL_DELAY,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dotcms.jobs.business.processor;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;

/**
* Qualifier annotation to identify the default retry strategy implementation.
*/
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
@Documented
public @interface DefaultRetryStrategy {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.dotcms.jobs.business.processor;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation to explicitly specify that a job processor should not retry failed jobs.
* This provides a more semantic way to indicate no-retry behavior compared to setting
* maxRetries=0 in ExponentialBackoffRetryPolicy.
*
* <p>Usage example:</p>
* <pre>
* {@literal @}NoRetryPolicy
* {@literal @}Queue("myQueue")
* public class MyJobProcessor implements JobProcessor {
* // Implementation
* }
* </pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface NoRetryPolicy {
// No elements needed - presence of annotation is sufficient
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.dotcms.jobs.business.processor.Cancellable;
import com.dotcms.jobs.business.processor.ExponentialBackoffRetryPolicy;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.processor.NoRetryPolicy;
import com.dotcms.jobs.business.processor.Queue;
import com.dotcms.jobs.business.util.JobUtil;
import com.dotcms.mock.request.FakeHttpRequest;
Expand Down Expand Up @@ -75,9 +76,7 @@
* @see ExponentialBackoffRetryPolicy
*/
@Queue("importContentlets")
@ExponentialBackoffRetryPolicy(
maxRetries = 0
)
@NoRetryPolicy
public class ImportContentletsProcessor implements JobProcessor, Cancellable {

private static final String PARAMETER_LANGUAGE = "language";
Expand Down

0 comments on commit ff5a516

Please sign in to comment.