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

os.failsafe.executor.HeartbeatService Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package os.failsafe.executor;

import os.failsafe.executor.utils.SystemClock;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class HeartbeatService {

    private final SystemClock systemClock;
    private final TaskRepository taskRepository;
    private final FailsafeExecutor failsafeExecutor;
    private final Duration heartbeatInterval;

    private final ConcurrentHashMap lockedTasks = new ConcurrentHashMap<>();

    public HeartbeatService(Duration heartbeatInterval, SystemClock systemClock, TaskRepository taskRepository, FailsafeExecutor failsafeExecutor) {
        this.heartbeatInterval = heartbeatInterval;
        this.systemClock = systemClock;
        this.taskRepository = taskRepository;
        this.failsafeExecutor = failsafeExecutor;
    }

    public void register(Task task) {
        lockedTasks.put(task.getId(), task);
    }

    public void unregister(Task task) {
        lockedTasks.remove(task.getId());
    }

    void heartbeat() {
        try {
            List toUpdate = findAllOutdatedLocks();
            if (toUpdate.isEmpty()) {
                return;
            }

            List updated = taskRepository.updateLockTime(toUpdate);

            for (Task task : updated) {
                lockedTasks.computeIfPresent(task.getId(), (k, v) -> task);
            }

            failsafeExecutor.clearException();

        } catch (Exception e) {
            failsafeExecutor.storeException(e);
        }
    }

    private List findAllOutdatedLocks() {
        List toUpdate = new ArrayList<>();
        LocalDateTime threshold = systemClock.now().minus(heartbeatInterval);

        for (String taskId : lockedTasks.keySet()) {
            Task task = lockedTasks.get(taskId);
            if (task != null && task.getLockTime().isBefore(threshold)) {
                toUpdate.add(task);
            }
        }
        return toUpdate;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy