si

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: MIT Imports: 9 Imported by: 6

README

si

Single Instance: keep just one instance of your program.

Description

si is a Go(Lang) package that helps you to find and communicate with other instances of your program.

Example

package main

import (
	"fmt"
	"github.com/Miguel-Dorta/si"
	"os"
)

func main() {
	// Example 1: Register your instance and exit if there's another one running
	if err := si.Register("my awesome program"); err != nil {
		if err == si.ErrOtherInstanceRunning {
			fmt.Println("There's another instance running, bye!")
			os.Exit(0)
		}
		fmt.Println("Error registering instance: " + err.Error())
		os.Exit(1)
	}

	// Example 2: Find another instance and kill it
	p, err := si.Find("my awesome program")
	if err != nil {
		fmt.Println("Error finding another instance: " + err.Error())
		os.Exit(1)
	}
	if p == nil {
		fmt.Println("No other instance was found, bye!")
		os.Exit(0)
	}
	p.process.Kill()
}
Important

If you want to communicate with your program using si.Process.StdXPipe(), this should have a pipe asigned to its stdX, else it will have /dev/null as default. This makes impossible the communication between processes. See si/test/test.go/createInstance2() to see an example.

Meme

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Dir is the directory where the file that registers an execution will be saved.
	// If you're having problems (usually with older versions of Linux) try setting it to /var/run
	Dir = "/run"

	// ErrOtherInstanceRunning is the error that Register() will return if another instance
	// with the same alias is running.
	ErrOtherInstanceRunning = errors.New("there is another instance of the program running")
)

Functions

func Register

func Register(alias string) error

Register registers the current instance of the program running under the given alias.

Types

type Process

type Process struct {
	*os.Process
}

Process is an extension of os.Process

func Find

func Find(alias string) (*Process, error)

Find looks for a running instance of a process that have the alias provided. It returns nil if no process with that alias was found.

func (*Process) StderrPipe

func (p *Process) StderrPipe() (io.ReadCloser, error)

StderrPipe creates a pipe to the stderr of the process

func (*Process) StdinPipe

func (p *Process) StdinPipe() (io.WriteCloser, error)

StdinPipe creates a pipe to the stdin of the process

func (*Process) StdoutPipe

func (p *Process) StdoutPipe() (io.ReadCloser, error)

StdoutPipe creates a pipe to the stdout of the process

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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