util

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2019 License: LGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyStarted is returned when somebody tries to start an already
	// running service.
	ErrAlreadyStarted = errors.New("already started")
	// ErrAlreadyStopped is returned when somebody tries to stop an already
	// stopped service (without resetting it).
	ErrAlreadyStopped = errors.New("already stopped")
	// ErrNotStarted is returned when somebody tries to stop a not running
	// service.
	ErrNotStarted = errors.New("not started")
)
View Source
var TimeLayout = "2006-01-02" //this represents YYYY-MM-DD

TimeLayout helps to parse a date string of the format YYYY-MM-DD

  Intended to be used with the following function:
	 time.Parse(TimeLayout, date)

Functions

func ChWorkDir

func ChWorkDir()

ChWorkDir switch back to working directory

func ConvertToUnixPathSeparator

func ConvertToUnixPathSeparator(p string) string

ConvertToUnixPathSeparator convert windows directory separator to unix

func Executable

func Executable() string

Executable get the real directory or real relative path where the program is located

func ExecutablePath

func ExecutablePath() string

ExecutablePath get the directory where the program is located

func ExecutablePathJoin

func ExecutablePathJoin(subPath string) string

ExecutablePathJoin returns a subdirectory of the directory where the program is located

func ExecuteFuncWithTimeout

func ExecuteFuncWithTimeout(f func(), t time.Duration)

func FileExist

func FileExist(filePath string) bool

func GetCurPcIp

func GetCurPcIp(matcher string) string

GetCurPcIp get the local ip according to the matching conditions

func GetUniqueId

func GetUniqueId() string

GetUniqueId get the unique id associated with the timestamp

func HomeDir

func HomeDir() string

get home dir

func InterfaceIsNil

func InterfaceIsNil(i interface{}) bool

determine if the interface is nil

func InterfaceSliceCopy

func InterfaceSliceCopy(to, from interface{})

like: []interface{} -> []*Block, or []*Block -> []interface{}

func IsIPhoneOS

func IsIPhoneOS() bool

IsIPhoneOS whether it is an apple mobile device

func IsTestEnv

func IsTestEnv() bool

determine if it is a test environment

func ParseDateRange

func ParseDateRange(dateRange string) (startDate, endDate time.Time, err error)

ParseDateRange parses a date range string of the format start:end

  where the start and end date are of the format YYYY-MM-DD.
  The parsed dates are time.Time and will return the zero time for
  unbounded dates, ex:
  unbounded start:	:2000-12-31
	 unbounded end: 	2000-12-31:

func ParseJson

func ParseJson(data string, result interface{}) error

ParseJson parsing json strings

func ParseJsonFromBytes

func ParseJsonFromBytes(data []byte, result interface{}) error

ParseJsonFromBytes parsing json bytes

func PathExists

func PathExists(path string) bool

func SetTimeout

func SetTimeout(timeoutFunc func(), dur time.Duration) (finishFunc func())

If the finishFunc is not called outside, the timeout will be triggered.

func StopChanClosed

func StopChanClosed(stop chan struct{}) bool

func StringifyJson

func StringifyJson(data interface{}) string

StringifyJson json to string

func StringifyJsonToBytes

func StringifyJsonToBytes(data interface{}) []byte

json bytes to string

func StringifyJsonToBytesWithErr

func StringifyJsonToBytesWithErr(data interface{}) ([]byte, error)

func WalkDir

func WalkDir(dirPth, suffix string) (files []string, err error)

WalkDir get all the files in the specified directory and all subdirectories, and match the suffix filtering.

func WriteDebugStack

func WriteDebugStack(toFile string) error

Types

type BaseService

type BaseService struct {
	Logger log.Logger
	// contains filtered or unexported fields
}

Classical-inheritance-style service declarations. Services can be started, then stopped, then optionally restarted.

Users can override the OnStart/OnStop methods. In the absence of errors, these methods are guaranteed to be called at most once. If OnStart returns an error, service won't be marked as started, so the user can call Start again.

A call to Reset will panic, unless OnReset is overwritten, allowing OnStart/OnStop to be called again.

The caller must ensure that Start and Stop are not called concurrently.

It is ok to call Stop without calling Start first.

Typical usage:

type FooService struct {
	BaseService
	// private fields
}

func NewFooService() *FooService {
	fs := &FooService{
		// init
	}
	fs.BaseService = *NewBaseService(log, "FooService", fs)
	return fs
}

func (fs *FooService) OnStart() error {
	fs.BaseService.OnStart() // Always call the overridden method.
	// initialize private fields
	// start subroutines, etc.
}

func (fs *FooService) OnStop() error {
	fs.BaseService.OnStop() // Always call the overridden method.
	// close/destroy private fields
	// stop subroutines, etc.
}

func NewBaseService

func NewBaseService(logger log.Logger, name string, impl Service) *BaseService

NewBaseService creates a new BaseService.

func (*BaseService) IsRunning

func (bs *BaseService) IsRunning() bool

IsRunning implements Service by returning true or false depending on the service's state.

func (*BaseService) OnReset

func (bs *BaseService) OnReset() error

OnReset implements Service by panicking.

func (*BaseService) OnStart

func (bs *BaseService) OnStart() error

OnStart implements Service by doing nothing. NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStart()

func (*BaseService) OnStop

func (bs *BaseService) OnStop()

OnStop implements Service by doing nothing. NOTE: Do not put anything in here, that way users don't need to call BaseService.OnStop()

func (*BaseService) Quit

func (bs *BaseService) Quit() <-chan struct{}

Quit Implements Service by returning a quit channel.

func (*BaseService) Reset

func (bs *BaseService) Reset() error

Reset implements Service by calling OnReset callback (if defined). An error will be returned if the service is running.

func (*BaseService) SetLogger

func (bs *BaseService) SetLogger(l log.Logger)

SetLogger implements Service by setting a logger.

func (*BaseService) Start

func (bs *BaseService) Start() error

Start implements Service by calling OnStart (if defined). An error will be returned if the service is already running or stopped. Not to start the stopped service, you need to call Reset.

func (*BaseService) Stop

func (bs *BaseService) Stop()

Stop implements Service by calling OnStop (if defined) and closing quit channel. An error will be returned if the service is already stopped.

func (*BaseService) String

func (bs *BaseService) String() string

String implements Servce by returning a string representation of the service.

func (*BaseService) Wait

func (bs *BaseService) Wait()

Wait blocks until the service is stopped.

type Service

type Service interface {
	// Start the service.
	// If it's already started or stopped, will return an error.
	// If OnStart() returns an error, it's returned by Start()
	Start() error
	OnStart() error

	// Stop the service.
	// If it's already stopped, will return an error.
	// OnStop must never error.
	Stop()
	OnStop()

	// Reset the service.
	// Panics by default - must be overwritten to enable reset.
	Reset() error
	OnReset() error

	// Return true if the service is running
	IsRunning() bool

	// Quit returns a channel, which is closed once service is stopped.
	Quit() <-chan struct{}

	// String representation of the service
	String() string

	// SetLogger sets a logger.
	SetLogger(logger log.Logger)
}

Service defines a service that can be started, stopped, and reset.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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