Class ServiceHandler
- All Implemented Interfaces:
TaskHandler
ServiceHandler is an implementation of TaskHandler responsible for managing the lifecycle
of services (processes) on a worker node. It supports starting and stopping various types of services,
including Java JARs, Python scripts, and other executables.
This handler maintains a registry of currently running services and provides mechanisms for launching them in a detached manner, capturing their output, and streaming logs back to a master server. It also handles the cleanup of resources upon service termination.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringThe specific operation this handler instance is configured to perform (e.g., "START", "STOP").private final RpcWorkerServerA reference to the parentRpcWorkerServerinstance.A static, thread-safe map that stores references to currently runningProcessobjects, keyed by their unique service IDs.private static final StringThe base directory where service files are expected to be located and where service-related logs and temporary files are stored. -
Constructor Summary
ConstructorsConstructorDescriptionServiceHandler(String op, RpcWorkerServer parentServer) Constructs a newServiceHandlerwith a specified operation and a reference to its parent RPC server. -
Method Summary
Modifier and TypeMethodDescriptionExecutes a service management task based on the handler's configured operation and the provided payload.private StringlaunchDetachedProcess(String serviceId, File executionDir, String... command) Launches a generic process in a detached mode, managing its lifecycle and log streaming.private StringstartProcess(String fileName, String serviceId, String port) Initiates the launch of a new service process.private StringstopProcess(String serviceId) Attempts to stop a running service identified by itsserviceId.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface titan.tasks.TaskHandler
setLogListener
-
Field Details
-
runningServices
A static, thread-safe map that stores references to currently runningProcessobjects, keyed by their unique service IDs. This allows for tracking and managing active services. -
WORKSPACE_DIR
The base directory where service files are expected to be located and where service-related logs and temporary files are stored.- See Also:
-
operation
The specific operation this handler instance is configured to perform (e.g., "START", "STOP"). This field determines which internal method (startProcessorstopProcess) will be invoked by theexecute(String)method. -
parentServer
A reference to the parentRpcWorkerServerinstance. This is used to facilitate communication back to the master server, such as streaming logs or notifying about service status changes.
-
-
Constructor Details
-
ServiceHandler
Constructs a newServiceHandlerwith a specified operation and a reference to its parent RPC server.- Parameters:
op- The operation this handler will perform (e.g., "START", "STOP").parentServer- TheRpcWorkerServerinstance that created this handler, used for callbacks.
-
-
Method Details
-
execute
Executes a service management task based on the handler's configured operation and the provided payload. The payload is expected to be a pipe-separated string containing service details.If the operation is "START", it parses the payload for filename, service ID, and port, then attempts to start a new process. If the operation is anything else (implicitly "STOP"), it parses the payload for a service ID and attempts to stop the corresponding process.
- Specified by:
executein interfaceTaskHandler- Parameters:
payload- A string containing service parameters, typically in the format "filename|serviceId|port" for START, or "serviceId" for STOP.- Returns:
- A string indicating the result of the operation, such as "DEPLOYED_SUCCESS", "STOPPED", or an "ERROR" message.
-
startProcess
Initiates the launch of a new service process.This method handles different types of service files:
- If
fileNameis "Worker.jar", it launches it as a Java JAR usingjava -jarwith OS-specific detachment. - If
fileNameends with ".py", it launches it using the "python" interpreter. - For any other file, it attempts to execute it directly.
The process is launched in a detached manner, and its output is redirected to log files.
* @param fileName The name or path of the script/executable file to run.- Parameters:
serviceId- A unique identifier for the service being started.port- The port number to be used by the service, primarily for Java JAR workers.- Returns:
- A status string indicating success or failure of the launch operation.
- If
-
launchDetachedProcess
Launches a generic process in a detached mode, managing its lifecycle and log streaming.This method performs the following steps:
- Checks if a service with the given
serviceIdis already running. - Configures a
ProcessBuilderwith the provided command and redirects standard error/output to a local log file. - Sets the working directory for the process.
- Starts the process.
- Spawns a new thread to continuously read the process's output, write it to a local log file, and batch-stream it to the master server.
- Registers the process in the
runningServicesmap and with theProcessRegistry. - Registers an
onExithook to clean up resources, remove the service from registries, and notify the master when the process terminates.
- Parameters:
executionDir- The directory in which the command should be executed. If null or non-existent,WORKSPACE_DIRis used.command- The command and its arguments to execute.- Returns:
- A status string indicating success or failure, including the service ID and PID if successful.
- Checks if a service with the given
-
stopProcess
Attempts to stop a running service identified by itsserviceId.It retrieves the
Processobject associated with theserviceIdfromrunningServices, then attempts to destroy the process. If the process is found and terminated, it is removed from the map.- Parameters:
serviceId- The unique identifier of the service to stop.- Returns:
- A status string indicating whether the service was stopped or if it was unknown.
-