daemon

package
v0.0.0-...-48d87d0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2015 License: GPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TaskRunning   = "running"
	TaskSucceeded = "succeeded"
	TaskFailed    = "failed"
)

A task can be in one of three states

Variables

standard error responses

Functions

func FormatTime

func FormatTime(t time.Time) string

FormatTime outputs the given time as microseconds since the epoch UTC, formatted as a decimal string

Types

type Command

type Command struct {
	Path string
	//
	GET    ResponseFunc
	PUT    ResponseFunc
	POST   ResponseFunc
	DELETE ResponseFunc
	// can guest GET?
	GuestOK bool
	// can non-admin GET?
	UserOK bool
	// contains filtered or unexported fields
}

A Command routes a request to an individual per-verb ResponseFUnc

func (*Command) ServeHTTP

func (c *Command) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Daemon

type Daemon struct {
	sync.RWMutex // for concurrent access to the tasks map
	// contains filtered or unexported fields
}

A Daemon listens for requests and routes them to the right command

func New

func New() *Daemon

New Daemon

func (*Daemon) AddTask

func (d *Daemon) AddTask(f func() interface{}) *Task

AddTask runs the given function as a task

func (*Daemon) DeleteTask

func (d *Daemon) DeleteTask(uuid string) error

DeleteTask removes a task from the tasks map, by uuid.

func (*Daemon) Dying

func (d *Daemon) Dying() <-chan struct{}

Dying is a tomb-ish thing

func (*Daemon) GetTask

func (d *Daemon) GetTask(uuid string) *Task

GetTask retrieves a task from the tasks map, by uuid.

func (*Daemon) Init

func (d *Daemon) Init() error

Init sets up the Daemon's internal workings. Don't call more than once.

func (*Daemon) Start

func (d *Daemon) Start()

Start the Daemon

func (*Daemon) Stop

func (d *Daemon) Stop() error

Stop shuts down the Daemon

type ErrorResponseFunc

type ErrorResponseFunc func(error, string, ...interface{}) Response

ErrorResponseFunc is a callable error Response. So you can return e.g. InternalError, or InternalError(err, "something broke"), etc.

func ErrorResponse

func ErrorResponse(status int) ErrorResponseFunc

ErrorResponse builds an "error" response from the given error status.

func (ErrorResponseFunc) Self

Self returns (a copy of) this same response; mostly for convenience.

func (ErrorResponseFunc) ServeHTTP

func (f ErrorResponseFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

type FileResponse

type FileResponse string

A FileResponse 's ServeHTTP method serves the file

func (FileResponse) Self

Self from the Response interface

func (FileResponse) ServeHTTP

func (f FileResponse) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP from the Response interface

type Response

type Response interface {
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	Self(*Command, *http.Request) Response // has the same arity as ResponseFunc for convenience
}

Response knows how to serve itself, and how to find itself

func AsyncResponse

func AsyncResponse(result map[string]interface{}) Response

AsyncResponse builds an "async" response from the given *Task

func SyncResponse

func SyncResponse(result interface{}) Response

SyncResponse builds a "sync" response from the given result.

type ResponseFunc

type ResponseFunc func(*Command, *http.Request) Response

A ResponseFunc handles one of the individual verbs for a method

type ResponseType

type ResponseType string

ResponseType is the response type

const (
	ResponseTypeSync  ResponseType = "sync"
	ResponseTypeAsync ResponseType = "async"
	ResponseTypeError ResponseType = "error"
)

“there are three standard return types: Standard return value, Background operation, Error”, each returning a JSON object with the following “type” field:

type Task

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

A Task encapsulates an asynchronous operation.

func RunTask

func RunTask(f func() interface{}) *Task

RunTask creates a Task for the given function and runs it.

func (*Task) CreatedAt

func (t *Task) CreatedAt() time.Time

CreatedAt returns the timestamp at which the task was created

func (*Task) Location

func (t *Task) Location(route *mux.Route) string

Location of the task, based on the given route.

If the route can't build a URL for this task, returns the empty string.

func (*Task) Map

func (t *Task) Map(route *mux.Route) map[string]interface{}

Map the task onto a map[string]interface{}, using the given route for the Location()

func (*Task) Output

func (t *Task) Output() interface{}

Output of this task. If the task is still running this will be nil.

TODO: output can and should go changing as the task progresses

func (*Task) State

func (t *Task) State() string

State of the task

func (*Task) UUID

func (t *Task) UUID() string

UUID of the task

func (*Task) UpdatedAt

func (t *Task) UpdatedAt() time.Time

UpdatedAt returns the timestamp at which the task was updated

type UUID

type UUID [2]uint64

An UUID is a universally unique identifier

func UUID4

func UUID4() UUID

UUID4 are random-based UUIDs

func (UUID) String

func (u UUID) String() string

Jump to

Keyboard shortcuts

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