executor

package module
v0.0.0-...-f1c4149 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2014 License: BSD-2-Clause Imports: 5 Imported by: 0

README

executor

This will be a simple API that can start, stop, restart, and log output from executables. It will make it easy to run an executable persistently in the background.

License

executor is licensed under the BSD 2-clause license. See LICENSE.

Copyright (c) 2014, Alex Nichol.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer. 
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Documentation

Index

Constants

View Source
const (
	// STATUS_RUNNING indicates that the Job is currently started and running.
	STATUS_RUNNING = iota

	// STATUS_RESTARTING indicates that the Job is not running but will be
	// restarted after a delay.
	STATUS_RESTARTING = iota

	// STATUS_STOPPED indicates that the Job is not running and will not be
	// automatically started again.
	STATUS_STOPPED = iota
)

These are statuses for a running Relauncher or Service.

Variables

View Source
var (
	ErrAlreadyRunning = errors.New("Job already running.")
	ErrNotRunning     = errors.New("Job is not running.")
	ErrNotWaiting     = errors.New("Job is not waiting to relaunch.")
)

These are errors which may be returned from various executable functions.

View Source
var NullLog = nullLog{}

NullLog is a Log which returns no-op writers.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	// Stdout stores the standard output configuration.
	Stdout Log

	// Stderr stores the standard error configuration.
	Stderr Log

	// Directory is the working directory for the command
	Directory string

	// SetUID specifies whether or not the UID field should be used.
	SetUID bool

	// UID is the UID to run the command under.
	UID int

	// SetGID specifies whether or not the GID field should be used.
	SetGID bool

	// GID is the GID to run the command under.
	GID int

	// Arguments is the command-line arguments for the command.
	Arguments []string

	// Environment is a mapping of environment variables for the command.
	Environment map[string]string
}

Cmd is the full configuration for a command-line executable.

func Command

func Command(arguments ...string) *Cmd

Command creates a new Cmd with generic settings given a set of command-line arguments.

func (*Cmd) Clone

func (c *Cmd) Clone() *Cmd

Clone creates a copy of a Cmd. While it does do a completey copy of the Arguments and Environment fields, it cannot copy the Logs.

func (*Cmd) ToJob

func (c *Cmd) ToJob() Job

ToJob creates a Job based on the current configuration in a Cmd. If the receiver is modified after a call to ToJob(), the job will not be modified.

type History

type History struct {
	LastStart time.Time
	LastStop  time.Time
	LastError time.Time
	Error     error
}

History records a brief history of a Relauncher.

type Job

type Job interface {
	// Start starts the job. After this is called, calling Stop() must stop the
	// job.
	// This is not thread-safe.
	Start() error

	// Stop stops the job asynchronously if it is running.
	// This is thread-safe.
	Stop() error

	// Wait waits for the job to finish.
	// This is not thread-safe.
	Wait() error
}

Job is a restartable task which can be run synchronously.

type Log

type Log interface {
	Open() (io.WriteCloser, error)
}

Log is a general log destination for a command's output.

type Relauncher

type Relauncher struct {
	// contains filtered or unexported fields
}

Relauncher automatically relaunches a task at a regular interval.

func Relaunch

func Relaunch(job Job, interval time.Duration) *Relauncher

Relaunch starts a job and continually relaunches it on a given interval.

func (*Relauncher) History

func (t *Relauncher) History() History

History returns the Relauncher's history.

func (*Relauncher) HistoryStatus

func (t *Relauncher) HistoryStatus() (History, Status)

HistoryStatus returns both the Relauncher's history and it's status.

func (*Relauncher) Interval

func (t *Relauncher) Interval() time.Duration

Interval returns the relaunch interval for a given relauncher.

func (*Relauncher) SkipWait

func (t *Relauncher) SkipWait() error

SkipWait skips the current relaunch timeout if applicable. If the job was not relaunching, this returns ErrNotWaiting.

func (*Relauncher) Status

func (t *Relauncher) Status() Status

Status returns the Relauncher's status

func (*Relauncher) Stop

func (t *Relauncher) Stop()

Stop stops the relauncher. This method waits for the background job to terminate if necessary.

func (*Relauncher) Wait

func (t *Relauncher) Wait()

Wait waits for the relauncher to be stopped.

type Service

type Service interface {
	// History returns a brief history of the Service.
	History() History

	// HistoryStatus returns the history and the status of a service.
	HistoryStatus() (History, Status)

	// SkipWait skips a wait if the service is a Relauncher and is waiting.
	SkipWait() error

	// Start starts the service if it is not already running.
	// By the time this returns, Status() must not be STATUS_STOPPED unless an
	// error is returned.
	Start() error

	// Status returns the status of the service.
	Status() Status

	// Stop stops the service synchronously.
	// By the time this returns, Status() must be STATUS_STOPPED unless an error
	// is returned.
	Stop() error

	// Wait waits for the service to stop.
	// Like Stop(), Wait() will only return once Status() has been set to
	// STATUS_STOPPED.
	Wait() error
}

Service is a restartable Job which runs in the background.

func JobService

func JobService(job Job) Service

JobService creates a Service that runs a specified Job.

func RelaunchService

func RelaunchService(job Job, interval time.Duration) Service

RelaunchService creates a service that runs a Relauncher for a specific Job.

type Status

type Status int

Status stores the status of a Relauncher.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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