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

feat(JobQueue Resource) Refs: 29480 #30303

Merged
merged 32 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
892e5cb
#29480
fabrizzio-dotCMS Oct 2, 2024
69fa89a
#29480
fabrizzio-dotCMS Oct 3, 2024
97b952c
Merge branch 'main' into issue-29480-resource-job-queue-take2
fabrizzio-dotCMS Oct 3, 2024
5559853
#29480
fabrizzio-dotCMS Oct 3, 2024
81172fc
Merge branch 'issue-29480-resource-job-queue-take2' of https://github…
fabrizzio-dotCMS Oct 3, 2024
149c9ef
#29480
fabrizzio-dotCMS Oct 4, 2024
c79a394
#29480
fabrizzio-dotCMS Oct 7, 2024
4550407
#29480
fabrizzio-dotCMS Oct 7, 2024
dc178fb
#29480
fabrizzio-dotCMS Oct 8, 2024
b286d36
Merge branch 'main' into issue-29480-resource-job-queue-take2
fabrizzio-dotCMS Oct 8, 2024
d65bec5
#29480
fabrizzio-dotCMS Oct 8, 2024
9835bd3
Merge branch 'issue-29480-resource-job-queue-take2' of https://github…
fabrizzio-dotCMS Oct 8, 2024
2129726
#29480
fabrizzio-dotCMS Oct 8, 2024
79c9992
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 8, 2024
95a2034
#29480
fabrizzio-dotCMS Oct 9, 2024
c21cf79
Merge branch 'issue-29480-resource-job-queue' of https://github.com/d…
fabrizzio-dotCMS Oct 9, 2024
86ea8d9
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 9, 2024
edfe6b7
#29480
fabrizzio-dotCMS Oct 9, 2024
fb27414
#29480
fabrizzio-dotCMS Oct 9, 2024
dc28e01
#29480
fabrizzio-dotCMS Oct 9, 2024
def2d47
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 9, 2024
4edfb8d
#29480
fabrizzio-dotCMS Oct 9, 2024
0ce7a01
Merge branch 'issue-29480-resource-job-queue' of https://github.com/d…
fabrizzio-dotCMS Oct 9, 2024
8ebc660
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 9, 2024
691e971
29480
fabrizzio-dotCMS Oct 9, 2024
e018540
Merge branch 'issue-29480-resource-job-queue' of https://github.com/d…
fabrizzio-dotCMS Oct 9, 2024
7ff8b72
#29480
fabrizzio-dotCMS Oct 9, 2024
ddc4ac3
#29480
fabrizzio-dotCMS Oct 9, 2024
de0ed34
#29480
fabrizzio-dotCMS Oct 10, 2024
21871c6
#29480
fabrizzio-dotCMS Oct 10, 2024
cf07d57
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 10, 2024
23a96ff
Merge branch 'main' into issue-29480-resource-job-queue
fabrizzio-dotCMS Oct 10, 2024
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
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);
fabrizzio-dotCMS marked this conversation as resolved.
Show resolved Hide resolved
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