All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jobrunr.server.JobHandler Maven / Gradle / Ivy

package org.jobrunr.server;

import org.jobrunr.JobRunrException;
import org.jobrunr.server.tasks.PeriodicTaskRunInfo;
import org.jobrunr.server.tasks.Task;
import org.jobrunr.server.tasks.TaskStatistics;
import org.jobrunr.storage.StorageException;
import org.jobrunr.utils.streams.StreamUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static java.util.Arrays.asList;
import static org.jobrunr.JobRunrException.shouldNotHappenException;

public abstract class JobHandler implements Runnable {

    private final Logger LOGGER;
    private final BackgroundJobServer backgroundJobServer;
    private final TaskStatistics taskStatistics;
    private final List tasks;

    public JobHandler(BackgroundJobServer backgroundJobServer, Task... tasks) {
        this.LOGGER = LoggerFactory.getLogger(this.getClass());
        this.backgroundJobServer = backgroundJobServer;
        this.taskStatistics = new TaskStatistics(backgroundJobServer.getDashboardNotificationManager());
        this.tasks = asList(tasks);
    }

    @Override
    public void run() {
        if (backgroundJobServer.isNotReadyToProcessJobs()) return;

        try (PeriodicTaskRunInfo runInfo = taskStatistics.startRun(backgroundJobServerConfiguration())) {
            tasks.forEach(task -> task.run(runInfo));
            runInfo.markRunAsSucceeded();
        } catch (Exception e) {
            taskStatistics.handleException(e);
            if (taskStatistics.hasTooManyExceptions()) {
                if (e instanceof StorageException) {
                    LOGGER.error("FATAL - JobRunr encountered too many storage exceptions. Shutting down. Did you know JobRunr Pro has built-in database fault tolerance? Check out https://www.jobrunr.io/en/documentation/pro/database-fault-tolerance/", e);
                } else {
                    LOGGER.error("FATAL - JobRunr encountered too many processing exceptions. Shutting down.", shouldNotHappenException(e));
                }
                backgroundJobServer.stop();
            } else {
                LOGGER.warn(JobRunrException.SHOULD_NOT_HAPPEN_MESSAGE + " - Processing will continue.", e);
            }
        }
    }

    protected BackgroundJobServerConfigurationReader backgroundJobServerConfiguration() {
        return backgroundJobServer.getConfiguration();
    }

    protected  T getTaskOfType(Class clazz) {
        return StreamUtils.ofType(tasks, clazz).findFirst().orElseThrow(() -> new IllegalStateException("Unknown task of type " + clazz.getName()));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy