Package titan.tasks
Class ProcessRegistry
java.lang.Object
titan.tasks.ProcessRegistry
A registry for managing and cleaning up processes, particularly those that might become
"zombied" due to abrupt worker node shutdowns or crashes. This class provides mechanisms
to register running processes, unregister them, and on startup, load previously registered
processes from a persistent file to check for and forcibly terminate any lingering zombie processes.
The registry maintains an in-memory map of service IDs to process IDs (PIDs) and persists this information to a local file to ensure state can be recovered across restarts.
-
Field Summary
FieldsModifier and TypeFieldDescriptionAn in-memory concurrent map that stores the mapping from a service identifier to its process ID (PID).private static final StringThe name of the file used for persisting the process registry data to disk. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidLoads the process registry from the persistent file, checks for any processes that are still running (potential zombies), and forcibly terminates them.static voidRegisters a new process with the registry.private static voidPersists the current state of the in-memory process registry to the designated file.static voidunregister(String serviceId) Unregisters a process from the registry.
-
Field Details
-
persistentMap
An in-memory concurrent map that stores the mapping from a service identifier to its process ID (PID). This map is used to keep track of active processes that should be persisted to disk. -
PROCESS_REGISTRY_FILE
The name of the file used for persisting the process registry data to disk. This file stores service ID to PID mappings, allowing the system to recover and clean up processes after a restart.- See Also:
-
-
Constructor Details
-
ProcessRegistry
public ProcessRegistry()
-
-
Method Details
-
loadAndCleanUpProcesses
public static void loadAndCleanUpProcesses()Loads the process registry from the persistent file, checks for any processes that are still running (potential zombies), and forcibly terminates them. After checking and cleaning up, the persistent file is cleared to ensure a fresh start.This method is typically called during application startup to handle cases where processes might have been left running due to an unexpected shutdown.
Any errors encountered during file reading or process termination are logged but do not prevent the application from starting. -
register
Registers a new process with the registry. The service ID and its corresponding PID are stored in the in-memory map and immediately persisted to disk.- Parameters:
serviceId- The unique identifier for the service or task.pid- The process ID (PID) of the running process associated with the service.
-
unregister
Unregisters a process from the registry. The entry corresponding to the given service ID is removed from the in-memory map, and the updated state is immediately persisted to disk.- Parameters:
serviceId- The unique identifier of the service or task to unregister.
-
saveToDisk
private static void saveToDisk()Persists the current state of the in-memory process registry to the designated file. Each entry (service ID and PID) is written as a comma-separated line. This method is synchronized to prevent concurrent writes to the file.
-