You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
idleshut/idleshut.go

203 lines
5.0 KiB

// Package idleshut provides a lifecycle and idletime management tool.
//
// Copyright 2017 Stephen Searles
//
// 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.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// 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 HOLDER 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.
package idleshut
import (
"sync"
"time"
)
// Config are the options for creating a new process
type Config struct {
// Start is called by Process.Start. If an error is returned,
// starting the process is aborted.
Start func() error
// Stop is called by Process.Stop. If an error is returned,
// stopping the process is aborted.
Stop func() error
// IdleTick is called by Process for every iteration of Tick. If
// an error is returned, it is sent to IdleProcessError. IdleTick
// and IdleProcessError must both be set or unset; mixed set-edness
// will result in a panic.
IdleTick func() error
IdleProcessError func(error)
Tick time.Duration
// MaxIdleTicks, if nonzero and ticking is configured, will cause
// the process to stop once the maximum number of ticks has been
// reached.
MaxIdleTicks uint
}
func (cfg Config) validate() {
allSet := cfg.IdleTick == nil && cfg.IdleProcessError == nil && cfg.Tick == 0
noneSet := cfg.IdleTick != nil && cfg.IdleProcessError != nil && cfg.Tick != 0
if !allSet && !noneSet {
panic("idleshut: Config.IdleTick, config.IdleProcessError, and config.Tick must all be set or all be unset")
}
}
// New returns a new Process with the given configuration.
func New(cfg Config) *Process {
cfg.validate()
return &Process{cfg: cfg}
}
// Process is a lifecycle and idle process monitor.
type Process struct {
cfg Config
mtx sync.Mutex
started time.Time
stopped time.Time
running bool
touched bool
idleTicks uint
currentGeneration uint
}
// Touch resets the idle timer to zero.
func (p *Process) Touch() bool {
p.mtx.Lock()
defer p.mtx.Unlock()
p.idleTicks = 0
p.touched = true
return p.running
}
// Start initiates the process.
func (p *Process) Start() error {
p.mtx.Lock()
defer p.mtx.Unlock()
if !p.running {
var err error
if p.cfg.Start != nil {
err = p.cfg.Start()
}
if err != nil {
return err
}
p.running = true
p.started = time.Now()
p.currentGeneration++
go p.startTicker()
}
return nil
}
// Running returns true if the process is currently running.
func (p *Process) Running() bool {
p.mtx.Lock()
defer p.mtx.Unlock()
return p.running
}
func (p *Process) generation() uint {
p.mtx.Lock()
defer p.mtx.Unlock()
return p.currentGeneration
}
func (p *Process) startTicker() {
if p.cfg.Tick == 0 || p.cfg.IdleTick == nil {
return
}
startingGen := p.generation()
tk := time.NewTicker(p.cfg.Tick)
for {
<-tk.C
// if we stopped running, or we started again and thus the geneneration has advanced, return
if !p.Running() || p.generation() != startingGen {
return
}
func() {
p.mtx.Lock()
defer p.mtx.Unlock()
if p.touched {
p.idleTicks = 0
p.touched = false //reset
} else {
p.idleTicks++
if p.cfg.IdleTick != nil {
p.idleProcessError(p.cfg.IdleTick())
}
if p.idleTicks >= p.cfg.MaxIdleTicks {
p.idleProcessError(p.stopWithLock())
}
}
}()
}
}
func (p *Process) idleProcessError(err error) {
if err != nil && p.cfg.IdleProcessError != nil {
go p.cfg.IdleProcessError(err)
}
}
// Stop halts the process.
func (p *Process) Stop() error {
p.mtx.Lock()
defer p.mtx.Unlock()
if p.running {
return p.stopWithLock()
}
return nil
}
func (p *Process) stopWithLock() error {
var err error
if p.cfg.Stop != nil {
err = p.cfg.Stop()
}
if err != nil {
return err
}
p.idleTicks = 0
p.running = false
p.stopped = time.Now()
return nil
}