daemon

package module
v0.0.0-...-2b8a368 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

README

A package for creating applications and implementing plug-ins (e.g. supported by other system processes or remote services) for it, without directly using the application API.

Forking is not supported in Windows OS

Example

An example application located in the example/ folder that implements graceful reload and graceful stop http server

Run example

go run example/example.go

Reloading the application without losing connected clients
curl http://localhost:8080/reload
Graceful stop
curl http://localhost:8080/stop

Documentation

Index

Constants

View Source
const (
	FINISH_APP_GRACEFULLY Command = iota
	RELOAD_APP
	UseSignalParam      = "signal"
	UseSignalParamTrue  = "1"
	UseSignalParamFalse = ""
)
View Source
const (
	// only we can read, modify and save
	PIDS_STORAGE_PATH_PERMS = 0700
	// only we can read and modify
	PIDS_STORAGE_FILE_PERMS = 0600

	EnvWorkerName      = "DAEMON_WORKER"
	EnvWorkerNameValue = "1"
)

Variables

This section is empty.

Functions

func CreateListener

func CreateListener(addr string) (net.Listener, error)

func CreateOrImportListener

func CreateOrImportListener(addr string) (net.Listener, error)

func DoReloadAppGraceful

func DoReloadAppGraceful()

DoReloadAppGraceful block accepting new connections and wait until all current connections are completed, then reload the application Deprecated: use Reload instead

func FireTrigger

func FireTrigger(name string, value interface{}) error

func FireTriggerWithContext

func FireTriggerWithContext(name string, ctx context.Context, value interface{}) error

func ForkChild

func ForkChild(addr string, ln net.Listener) (*os.Process, error)

There is one problem with forking - when you do this, the program will detach from the console and the other process will, in fact, the program will run in the background and spit on the console, so in such a situation, when you use it, you should:

1. Fork the first program you run as well 2. Save logs in the logs folder

func GetWaitingGroup

func GetWaitingGroup() *sync.WaitGroup

func ImportListener

func ImportListener(addr string) (net.Listener, error)

func IsCriticalError

func IsCriticalError(err error) bool

func IsFallingDown

func IsFallingDown() bool

IsFallingDown is the application reloading? If so, you should not accept new connections to your server in this situation

func IsFileExistsAndIsDir

func IsFileExistsAndIsDir(filename string) bool

func IsFileExistsAndIsFile

func IsFileExistsAndIsFile(filename string) bool

func Message

func Message(cmd Command)

func MessageWithArgs

func MessageWithArgs(cmd Command, args map[string]string)

func NewCriticalError

func NewCriticalError(message string) error

func RegisterListener

func RegisterListener(name string, listener EventHandler)

RegisterListener Registering a listener ListenerHandler should not return an error, and if it does at all, we should not interrupt the application because of it! this should be done by the plugin itself if necessary

func Reload

func Reload()

Reload Anyone can do this to make the server reload functions for end applications

func Run

func Run(pathForPids string, handlerAfterFinish func())

Run @pathForPids - where to save pids - create if it does not exist @handlerAfterFinish called at the end

func Stop

func Stop()

Stop Anyone can do this to stop the server as a function for end applications

Types

type ChannelMessage

type ChannelMessage struct {
	Command Command
	Args    map[string]string
}

func (ChannelMessage) Arg

func (self ChannelMessage) Arg(name string, dfault string) string

type Command

type Command int

type CriticalError

type CriticalError struct {
	Message string
}

func (CriticalError) Error

func (t CriticalError) Error() string

type EventHandler

type EventHandler func(ctx context.Context, event string, value interface{}) error

type TcpKeepAliveListener

type TcpKeepAliveListener struct {
	*net.TCPListener
}

TcpKeepAliveListener sets TCP keep-alive timeouts on accepted connections. It's used by ListenAndServe and ListenAndServeTLS so dead TCP connections (e.g. closing laptop mid-download) eventually go away.

func NewListener

func NewListener(address string) (*TcpKeepAliveListener, bool, error)

func (TcpKeepAliveListener) Accept

func (ln TcpKeepAliveListener) Accept() (c net.Conn, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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