Skip to content

A Go library that allows applications to restart themselves automatically in a safe and elegant way. Unlike other solutions like `go-selfupdate`, this library focuses specifically on **automatic process restart**, offering fine-grained control over the application lifecycle.

License

Notifications You must be signed in to change notification settings

kubex-ecosystem/selfrestart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

SelfRestart Banner

Build Go Version License Go Report Card

πŸ‡§πŸ‡· Read this documentation in Portuguese

SelfRestart is a Go library that allows applications to restart themselves automatically in a safe and elegant way. Unlike other solutions like go-selfupdate, this library focuses specifically on automatic process restart, offering fine-grained control over the application lifecycle.

🎯 Features

  • βœ… Automatic Restart: Restarts the application preserving arguments and environment
  • βœ… Platform Detection: Support for Linux, macOS and Windows
  • βœ… Automatic Go Installation: Installs Go automatically if needed
  • βœ… Process Management: Complete control over PIDs and signals
  • βœ… Integrated Logging: Logging system with different levels
  • βœ… Modular: Clean and well-organized architecture
  • βœ… Thread-Safe: Safe for use in concurrent applications

πŸ“¦ Installation

go get github.com/rafa-mori/selfrestart

πŸš€ Basic Usage

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    
    "github.com/rafa-mori/selfrestart"
)

func main() {
    // Create a SelfRestart instance
    sr := selfrestart.New()
    
    // Check if Go is installed
    if !sr.IsGolangInstalled() {
        fmt.Println("Go is not installed")
        os.Exit(1)
    }
    
    // Setup signal capture
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGUSR1)
    
    for {
        select {
        case <-sigChan:
            fmt.Println("Restarting application...")
            if err := sr.Restart(); err != nil {
                fmt.Printf("Error restarting: %v\\n", err)
                os.Exit(1)
            }
            os.Exit(0) // Terminate current process
        }
    }
}

πŸ“š API Documentation

Main Structure

type SelfRestart struct {
    // Internal fields (not exposed)
}

Main Methods

New() *SelfRestart

Creates a new SelfRestart instance.

IsGolangInstalled() bool

Checks if Go is installed on the system and offers automatic installation if needed.

Restart() error

Restarts the current process safely.

GetCurrentPID() int

Returns the current process PID.

KillCurrentProcess() error

Terminates the current process gracefully.

IsProcessRunning(pid int) (bool, error)

Checks if a process with the specified PID is running.

InstallGo() (bool, error)

Installs Go automatically on Unix systems.

GetPlatformInfo() platform.PlatformInfo

Returns information about the current platform (OS/Architecture).

πŸ—οΈ Architecture

The project is organized in a modular way:

selfrestart/
β”œβ”€β”€ selfrestart.go          # Main public API
β”œβ”€β”€ example/
β”‚   └── main.go            # Usage example
β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ install/           # Go installation management
β”‚   β”œβ”€β”€ platform/          # Platform detection
β”‚   β”œβ”€β”€ process/           # Process management
β”‚   └── restart/           # Restart logic
└── logger/                # Logging system

Internal Modules

  • install: Responsible for Go detection and automatic installation
  • platform: Manages platform and architecture information
  • process: Controls processes, PIDs and system signals
  • restart: Implements restart logic using temporary scripts
  • logger: Integrated logging system

πŸ”§ Advanced Example

See the example/main.go file for a complete example that demonstrates:

  • System signal capture
  • Automatic restart via SIGUSR1
  • Continuous application monitoring
  • Graceful shutdown management

To run the example:

cd example
go run main.go

In another terminal, test the restart:

# Get the application PID
ps aux | grep main

# Send restart signal
kill -USR1 <PID>

πŸŽ›οΈ Configuration

Environment Variables

  • PATH: Used to detect Go installation

Command Line Arguments

  • --wait: Waits for the restart script to be executed completely

πŸ§ͺ Testing

# Run all tests
go test ./...

# Run tests with coverage
go test -v -race -coverprofile=coverage.out ./...

# View coverage report
go tool cover -html=coverage.out

πŸ” Troubleshooting

Go not found

ERROR: Go is not installed or not found in PATH

Solution: The library will offer automatic Go installation.

Insufficient permissions

ERROR: Error creating restart script: permission denied

Solution: Run with adequate permissions or check if /tmp is writable.

Process doesn't restart

ERROR: Process did not terminate after interrupt signal

Solution: Check if there are no blocks in the application that prevent graceful shutdown.

🀝 Contributing

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“‹ Roadmap

  • Windows Service support
  • systemd integration (Linux)
  • Automatic backup before restart
  • Webhooks for notifications
  • Web interface for monitoring
  • Metrics and monitoring
  • Docker container support

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Inspired by the go-selfupdate project
  • Built with Go community best practices
  • Special thanks to contributors

πŸ“ž Support

If you have any questions, suggestions or issues, feel free to reach out:


⭐ If this project was useful to you, consider giving it a star on GitHub!

About

A Go library that allows applications to restart themselves automatically in a safe and elegant way. Unlike other solutions like `go-selfupdate`, this library focuses specifically on **automatic process restart**, offering fine-grained control over the application lifecycle.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •