godaemon

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 10 Imported by: 0

README

godaemon

A simple daemon library for Go.

Features:

  • provide a daemon process
  • can use command to start/stop/restart the daemon

Usage:

import (
	"time"

	"git.kingecg.top/kingecg/godaemon"
	"git.kingecg.top/kingecg/gologger"
)

var daemon *godaemon.GoDaemon

func main() {
	gologger.Configure(gologger.LoggersConfig{
		Appenders: map[string]gologger.LogAppenderConfig{
			"flog": {
				Type: "console",
			},
		},
		Categories: map[string]gologger.LogConfig{
			"default": {
				Appenders: []string{"flog"},
				Level:     "debug",
			},
		},
	})
	daemon := godaemon.NewGoDaemon(start, stop)
	daemon.Start()
}

func start(g *godaemon.GoDaemon) {
	l := gologger.GetLogger("task")
	for {
		time.Sleep(time.Second * 1)
		l.Debug("task running:", g.GetPid())
	}
}

func stop(g *godaemon.GoDaemon) {
	l := gologger.GetLogger("task")
	l.Debug("called stop")
	if g == nil {
		l.Debug("Daemon is nil")
		return
	}
	if g.Running == nil {
		l.Debug("task is nil")
		return
	}

}

命令:

  • without arguments: start the daemon
  • -s quit: stop the daemon
  • -s restart: restart the daemon

Documentation

Overview

Package godaemon provides a simple daemon library for Go applications. It allows applications to run as daemon processes with start/stop/restart capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDaemon

func IsDaemon() bool

IsDaemon checks if the current process is the daemon process

Returns:

  • bool: true if this is the daemon process, false otherwise

func IsDaemonTask

func IsDaemonTask() bool

IsDaemonTask checks if the current process is the task process

Returns:

  • bool: true if this is the task process, false otherwise

func IsMaster

func IsMaster() bool

IsMaster checks if the current process is the master process

Returns:

  • bool: true if this is the master process, false otherwise

Types

type GoDaemon

type GoDaemon struct {
	*gologger.Logger           // Embedded logger for logging
	Running          *exec.Cmd // Currently running task process

	StartFn func(*GoDaemon) // Function called when task starts
	StopFn  func(*GoDaemon) // Function called when task stops
	// contains filtered or unexported fields
}

GoDaemon represents a daemon process manager

func NewGoDaemon

func NewGoDaemon(start, stop func(*GoDaemon)) *GoDaemon

NewGoDaemon creates a new GoDaemon instance

Parameters:

  • start: function to be called when the task process starts
  • stop: function to be called when the task process stops

Returns:

  • *GoDaemon: initialized GoDaemon instance

func (*GoDaemon) GetPid

func (g *GoDaemon) GetPid() int

GetPid retrieves the daemon process ID from the PID file

Returns:

  • int: process ID if found and valid, 0 otherwise

func (*GoDaemon) GetTaskPid

func (g *GoDaemon) GetTaskPid() int

GetTaskPid retrieves the task process ID from the task PID file

Returns:

  • int: process ID if found and valid, 0 otherwise

func (*GoDaemon) Start

func (g *GoDaemon) Start()

Start begins the daemon process management based on the current process role

Behavior depends on process role:

  • Master: starts the daemon process or sends signals to running daemon
  • Daemon: manages task process lifecycle
  • Task: executes the user-provided StartFn

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL