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

#29480 #30228

Closed
wants to merge 12 commits into from
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.dotcms.jobs.business.api;

import com.dotcms.jobs.business.error.JobProcessorInstantiationException;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotmarketing.util.Logger;
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class JobProcessorFactory {

public JobProcessorFactory() {
// Default constructor for CDI
}

/**
* Creates a new instance of the specified job processor class.
*
* @param processorClass The class of the job processor to create.
* @return An optional containing the new job processor instance, or an empty optional if the
* processor could not be created.
*/
JobProcessor newInstance(
Class<? extends JobProcessor> processorClass) {
try {
return processorClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
Logger.error(this, "Error creating job processor", e);
throw new JobProcessorInstantiationException(processorClass, e);
}
}

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

import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotmarketing.util.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexReader;

/**
* Scans the classpath for classes that implement the JobProcessor interface.
* This class uses Jandex to scan the classpath for classes that implement the JobProcessor interface.
*/
@ApplicationScoped
public class JobProcessorScanner {

/**
* Discovers all classes that implement the JobProcessor interface.
* @return A list of classes that implement the JobProcessor interface.
*/
public List<Class<? extends JobProcessor>> discoverJobProcessors() {
List<Class<? extends JobProcessor>> jobProcessors = new ArrayList<>();
try {

Index index = getJandexIndex();
DotName jobProcessorInterface = DotName.createSimple(JobProcessor.class.getName());

Collection<ClassInfo> implementors = index.getAllKnownImplementors(jobProcessorInterface);

for (ClassInfo classInfo : implementors) {
String className = classInfo.name().toString();

Class<?> clazz = Class.forName(className);
if (JobProcessor.class.isAssignableFrom(clazz)) {
jobProcessors.add((Class<? extends JobProcessor>) clazz);
}
}

} catch (IOException | ClassNotFoundException e) {
Logger.error(JobProcessorScanner.class, "Error discovering JobProcessors", e);

}
return jobProcessors;
}

/**
* Reads the Jandex index file.
* @return The Jandex index.
* @throws IOException If the Jandex index file cannot be read.
*/
private Index getJandexIndex() throws IOException {
InputStream input = getClass().getClassLoader().getResourceAsStream("META-INF/jandex.idx");
if (input == null) {
throw new IOException("Jandex index not found");
}
IndexReader reader = new IndexReader(input);
return reader.read();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import com.dotcms.jobs.business.job.JobPaginatedResult;
import com.dotcms.jobs.business.processor.JobProcessor;
import com.dotcms.jobs.business.queue.JobQueue;
import com.dotcms.jobs.business.queue.error.JobQueueDataException;
import com.dotmarketing.exception.DotDataException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand Down Expand Up @@ -55,7 +57,13 @@ public interface JobQueueManagerAPI {
* @param queueName The name of the queue
* @param processor The job processor to register
*/
void registerProcessor(String queueName, JobProcessor processor);
void registerProcessor(final String queueName, final Class<? extends JobProcessor> processor);

/**
* Retrieves the job processors for all registered queues.
* @return A map of queue names to job processors
*/
Map<String,Class<? extends JobProcessor>> getQueueNames();

/**
* Creates a new job in the specified queue.
Expand Down Expand Up @@ -88,6 +96,25 @@ String createJob(String queueName, Map<String, Object> parameters)
*/
JobPaginatedResult getJobs(int page, int pageSize) throws DotDataException;

/**
* Retrieves a list of active jobs for a specific queue.
* @param queueName The name of the queue
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of active jobs and pagination information.
* @throws JobQueueDataException if there's an error fetching the jobs
*/
JobPaginatedResult getActiveJobs(String queueName, int page, int pageSize) throws JobQueueDataException;

/**
* Retrieves a list of completed jobs for a specific queue within a date range.
* @param page The page number
* @param pageSize The number of jobs per page
* @return A result object containing the list of completed jobs and pagination information.
* @throws JobQueueDataException
*/
JobPaginatedResult getFailedJobs(int page, int pageSize) throws JobQueueDataException;

/**
* Cancels a job.
*
Expand All @@ -112,6 +139,13 @@ String createJob(String queueName, Map<String, Object> parameters)
*/
void setRetryStrategy(String queueName, RetryStrategy retryStrategy);

/**
* Retrieves the retry strategy for a specific queue.
* @param jobId The ID of the job
* @return The processor instance, or an empty optional if not found
*/
Optional<JobProcessor> getInstance(final String jobId);

/**
* @return The CircuitBreaker instance
*/
Expand Down
Loading
Loading