Documentation ¶
Overview ¶
Package daemon provides functionalities for handling process identifiers (PID).
Index ¶
Constants ¶
const Name = "daemon"
Constant representing the name of the daemon.
Variables ¶
This section is empty.
Functions ¶
func IsProcessRunning ¶
IsProcessRunning checks if the process with the given PID is running. It verifies the existence of a process by its PID.
Parameters: - pid: int The PID of the process to check.
Returns: - bool: true if the process is running, false otherwise.
Types ¶
type DaemonHandler ¶
type DaemonHandler struct { PIDHandler *PIDHandler // PIDHandler is used to manage the PID file of the daemon. // contains filtered or unexported fields }
DaemonHandler manages the lifecycle and signal handling of the daemon.
func New ¶
func New() *DaemonHandler
New creates a new instance of DaemonHandler. It initializes the PIDHandler and channels for signal and completion handling.
func (*DaemonHandler) Start ¶
func (d *DaemonHandler) Start(handlers ...*Handler)
Start begins the daemon execution. It starts the specified handlers and listens for system signals to handle graceful shutdown. Parameters: - handlers: ...*Handler A variadic list of handlers to be executed by the daemon.
func (*DaemonHandler) Stop ¶
func (d *DaemonHandler) Stop()
Stop gracefully stops the daemon. It sends a SIGTERM signal to the daemon.
type Handler ¶
type Handler struct { Name string // Name of the handler. Call func() error // Call is the function to execute the handler's task. }
Handler struct defines a structure for handling specific daemon tasks.
type PIDHandler ¶
type PIDHandler struct {
// contains filtered or unexported fields
}
PIDHandler handles the creation, retrieval, and deletion of PID files. It is used to manage PID files for processes, typically in a Unix-like environment.
func NewPIDHandler ¶
func NewPIDHandler(processName, pathRun string) *PIDHandler
NewPIDHandler creates and returns a new instance of PIDHandler for a given process name and path. It initializes a PIDHandler with a specified process name and the path for the PID file.
Parameters: - processName: string The name of the process. - pathRun: string The path to the directory containing the PID file.
Returns: - *PIDHandler: A new instance of PIDHandler.
func (*PIDHandler) ClearPID ¶
func (p *PIDHandler) ClearPID() error
ClearPID deletes the PID file associated with the process. It removes the PID file, indicating that the process is no longer running.
Returns: - error: An error if the file operation fails.
func (*PIDHandler) GetPID ¶
func (p *PIDHandler) GetPID() (int, error)
GetPID retrieves the PID from the PID file. If the associated process is running, it returns the PID and an error. If the PID file exists but the process isn't running, it deletes the PID file and returns 0.
Returns: - int: The PID of the running process or 0 if not running. - error: An error if the process is already running or if there's an issue reading the PID file.
func (*PIDHandler) SetPID ¶
func (p *PIDHandler) SetPID(pid int) error
SetPID creates or updates the PID file with the provided PID. It writes the given PID to the PID file for the process.
Parameters: - pid: int The process ID to set in the PID file.
Returns: - error: An error if the file operation fails.