workclient

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

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

Go to latest
Published: Aug 10, 2015 License: MIT Imports: 12 Imported by: 0

README

workclient

workclient is a simple worker library for handling boilerplate things such as pipelining, graceful shutdowns, attaching to signals, logging events, sending heartbeat checks to etcd (when configured), sending internal metrics to statsd/influx/graphite and general execution.

View the docs.

Installation

$ go get github.com/nyxtom/workclient

Example

import (
	"io"
	"net/http"
	"time"

	"github.com/nyxtom/workclient"
)

type SimpleService struct {
	workclient.WorkClient
	exit chan error
	ticker *time.Ticker
}

func NewSimpleService(config *workclient.Config) *SimpleService {
	service := new(SimpleService)
	service.Configure(config, service.execute, service.stopExecute)
	service.exit = make(chan error)
	service.ticker = time.NewTicker(time.Second)
	return service
}

func (s *SimpleService) execute() {
	for {
		select {
		case <-s.exit:
			s.ticker.Stop()
			return
		case <-s.ticker.C:
			fmt.Println("Running...")
		}
	}
}

func (s *SimpleService) stopExecute() {
	s.exit <-nil
}

func main() {
	cfg := new(workclient.Config)
	service := NewSimpleService(cfg)

	sc := make(chan os.Signal, 1)
	signal.Notify(sc,
		syscall.SIGHUP,
		syscall.SIGINT,
		syscall.SIGTERM,
		syscall.SIGQUIT,
		syscall.SIGKILL,
		os.Interrupt)

	go func() {
		<-sc
		service.Close()
	}()

	service.Run()
}

LICENSE

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Standard configuration values
	StatsdAddr     string `toml:"statsd_addr"`
	StatsdInterval int    `toml:"statsd_interval"`
	StatsdPrefix   string `toml:"statsd_prefix"`

	StdErrLogFile string `toml:"stderr_logfile"`

	GraphiteAddr   string `toml:"graphite_addr"`
	GraphitePrefix string `toml:"graphite_prefix"`

	EtcdAddr         string `toml:"etcd_addr"`
	EtcdCaCert       string `toml:"etcd_cacert"`
	EtcdTlsKey       string `toml:"etcd_tlskey"`
	EtcdTlsCert      string `toml:"etcd_tlscert"`
	EtcdPrefixKey    string `toml:"etcd_prefix_key"`
	EtcdHeartbeatTtl int    `toml:"etcd_heartbeat_ttl"`

	ServiceName string `toml:"service_name"`
	Hostname    string `toml:"hostname"`

	// web configuration
	WebAddr        string        `toml:"web_addr" default:"0.0.0.0:5000"`
	ReadTimeout    time.Duration `toml:"web_read_timeout" default:"10s"`
	WriteTimeout   time.Duration `toml:"web_write_timeout" default:"10s"`
	MaxHeaderBytes int           `toml:"web_max_header_bytes" default:"65536"`
}

Config represents a simple work service configuration

type LogEvent

type LogEvent struct {
	Level   string // level represents a string used for the event level or type
	Message string // message associated with the event occurring
	Err     error  // error potentially associated with this event
	Buf     []byte // buffer of a potential runtime stack
}

LogEvent represents a simple construct for when 'things' occur in the application on certain levels

type Stats

type Stats struct {
	Time int64 `json:"time"`
	// runtime
	GoVersion    string `json:"go_version"`
	GoOs         string `json:"go_os"`
	GoArch       string `json:"go_arch"`
	CpuNum       int    `json:"cpu_num"`
	GoroutineNum int    `json:"goroutine_num"`
	Gomaxprocs   int    `json:"gomaxprocs"`
	CgoCallNum   int64  `json:"cgo_call_num"`
	ProcessId    int    `json:"process_id"`
	Hostname     string `json:"hostname"`
	// memory
	MemoryAlloc      uint64 `json:"memory_alloc"`
	MemoryTotalAlloc uint64 `json:"memory_total_alloc"`
	MemorySys        uint64 `json:"memory_sys"`
	MemoryLookups    uint64 `json:"memory_lookups"`
	MemoryMallocs    uint64 `json:"memory_mallocs"`
	MemoryFrees      uint64 `json:"memory_frees"`
	// stack
	StackInUse uint64 `json:"memory_stack"`
	// heap
	HeapAlloc    uint64 `json:"heap_alloc"`
	HeapSys      uint64 `json:"heap_sys"`
	HeapIdle     uint64 `json:"heap_idle"`
	HeapInuse    uint64 `json:"heap_inuse"`
	HeapReleased uint64 `json:"heap_released"`
	HeapObjects  uint64 `json:"heap_objects"`
	// garbage collection
	GcNext           uint64    `json:"gc_next"`
	GcLast           uint64    `json:"gc_last"`
	GcNum            uint32    `json:"gc_num"`
	GcPerSecond      float64   `json:"gc_per_second"`
	GcPausePerSecond float64   `json:"gc_pause_per_second"`
	GcPause          []float64 `json:"gc_pause"`
}

func GetRuntimeStats

func GetRuntimeStats() (*Stats, map[string]interface{}, error)

type WorkClient

type WorkClient struct {
	Config  Config
	Header  string
	Version string
	// contains filtered or unexported fields
}

func (*WorkClient) Close

func (c *WorkClient) Close()

Close the running client appropriately

func (*WorkClient) Configure

func (client *WorkClient) Configure(config Config, executeWorkFn, closeExecuteWorkFn func())

Configure the client according to the given configuration and initialize state

func (*WorkClient) Log

func (c *WorkClient) Log(level, msg string, err error)

Log will log the msg and level

func (*WorkClient) LogErr

func (c *WorkClient) LogErr(err error)

LogErr will log a simple error level event

func (*WorkClient) LogF

func (c *WorkClient) LogF(level, format string, vals ...interface{})

LogF will log the given level and formatted values

func (*WorkClient) LogInfo

func (c *WorkClient) LogInfo(msg string)

LogInfo will log the message as an info level

func (*WorkClient) LogInfoF

func (c *WorkClient) LogInfoF(format string, vals ...interface{})

func (*WorkClient) NewGauge

func (c *WorkClient) NewGauge(metric string) metrics.Gauge

NewGauge will create a gauge formatted with the service name and metric and register it with go-metrics

func (*WorkClient) NewTimer

func (c *WorkClient) NewTimer(metric string) metrics.Timer

NewTimer will create a timer formatted with the service name and metric and register it with go-metrics

func (*WorkClient) Run

func (c *WorkClient) Run()

Runs all goroutines for signals, closing, connect routines..etc

Jump to

Keyboard shortcuts

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