Compare commits
No commits in common. "9425f1b3a9a083c804282f723cefcf59b7142237" and "816c3e1fc3d87c1a0e4594b5fe638e1caac14630" have entirely different histories.
9425f1b3a9
...
816c3e1fc3
@ -1,114 +0,0 @@
|
|||||||
// Example routines demonstrates the use of routines and a routine manager.
|
|
||||||
package main
|
|
||||||
|
|
||||||
import "log"
|
|
||||||
import "time"
|
|
||||||
import "context"
|
|
||||||
import "math/rand"
|
|
||||||
import "git.tebibyte.media/sashakoshka/go-service/daemon"
|
|
||||||
import "git.tebibyte.media/sashakoshka/go-service/routines"
|
|
||||||
|
|
||||||
func main () {
|
|
||||||
cow := cow { name: "cow" }
|
|
||||||
sheep := sheep { name: "sheep" }
|
|
||||||
horse := horse {
|
|
||||||
name: "horse",
|
|
||||||
sheep: &sheep,
|
|
||||||
}
|
|
||||||
|
|
||||||
manager := routines.Manager {
|
|
||||||
Routines: []routines.Routine {
|
|
||||||
&cow,
|
|
||||||
&sheep,
|
|
||||||
&horse,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 12)
|
|
||||||
daemon.OnSigint(cancel)
|
|
||||||
err := manager.Run(ctx)
|
|
||||||
if err != nil { log.Fatalln(err) }
|
|
||||||
}
|
|
||||||
|
|
||||||
type cow struct {
|
|
||||||
name string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *cow) Init (ctx context.Context) error {
|
|
||||||
log.Printf("%s: waking up\n", this.name)
|
|
||||||
defer log.Printf("%s: woke up\n", this.name)
|
|
||||||
time.Sleep(time.Duration(float64(time.Second) * 4 * rand.Float64()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *cow) Run (ctx context.Context) error {
|
|
||||||
log.Printf("%s: running\n", this.name)
|
|
||||||
defer log.Printf("%s: going to sleep\n", this.name)
|
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(float64(time.Second) * 4 * rand.Float64()))
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <- ctx.Done(): return ctx.Err()
|
|
||||||
case <- ticker.C: log.Printf("%s: moo\n", this.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type sheep struct {
|
|
||||||
name string
|
|
||||||
poke chan struct { }
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *sheep) Init (ctx context.Context) error {
|
|
||||||
log.Printf("%s: waking up\n", this.name)
|
|
||||||
defer log.Printf("%s: woke up\n", this.name)
|
|
||||||
this.poke = make(chan struct { })
|
|
||||||
time.Sleep(time.Duration(float64(time.Second) * 4 * rand.Float64()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *sheep) Run (ctx context.Context) error {
|
|
||||||
log.Printf("%s: running\n", this.name)
|
|
||||||
defer log.Printf("%s: going to sleep\n", this.name)
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <- ctx.Done(): return ctx.Err()
|
|
||||||
case <- this.poke: log.Printf("%s: baa\n", this.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
type horse struct {
|
|
||||||
name string
|
|
||||||
sheep *sheep
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *horse) Init (ctx context.Context) error {
|
|
||||||
log.Printf("%s: waking up\n", this.name)
|
|
||||||
defer log.Printf("%s: woke up\n", this.name)
|
|
||||||
time.Sleep(time.Duration(float64(time.Second) * 4 * rand.Float64()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (this *horse) Run (ctx context.Context) error {
|
|
||||||
log.Printf("%s: running\n", this.name)
|
|
||||||
defer log.Printf("%s: going to sleep\n", this.name)
|
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(float64(time.Second) * 4 * rand.Float64()))
|
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <- ctx.Done(): return ctx.Err()
|
|
||||||
case <- ticker.C:
|
|
||||||
if this.sheep != nil {
|
|
||||||
log.Printf("%s: poking %s\n", this.name, this.sheep.name)
|
|
||||||
this.sheep.poke <- struct { } { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -46,7 +46,6 @@ type Manager struct {
|
|||||||
// RestartDeadline specifies the amount of time a routine has to be
|
// RestartDeadline specifies the amount of time a routine has to be
|
||||||
// running before failing to be restarted. This is to prevent routines
|
// running before failing to be restarted. This is to prevent routines
|
||||||
// that immediately fail from just being restarted over and over again.
|
// that immediately fail from just being restarted over and over again.
|
||||||
// Defaults to 32 seconds if not set.
|
|
||||||
RestartDeadline time.Duration
|
RestartDeadline time.Duration
|
||||||
|
|
||||||
// Logger, if non-nil, is where log messages will be written to. If it
|
// Logger, if non-nil, is where log messages will be written to. If it
|
||||||
@ -124,11 +123,6 @@ func (this *Manager) initRoutine (routine Routine, group *sync.WaitGroup) {
|
|||||||
func (this *Manager) runRoutine (routine Routine, group *sync.WaitGroup) {
|
func (this *Manager) runRoutine (routine Routine, group *sync.WaitGroup) {
|
||||||
defer group.Done()
|
defer group.Done()
|
||||||
|
|
||||||
restartDeadline := this.RestartDeadline
|
|
||||||
if restartDeadline == 0 {
|
|
||||||
restartDeadline = 32 * time.Second
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
lastStart := time.Now()
|
lastStart := time.Now()
|
||||||
err := panicWrap(routine.Run, this.ctx)
|
err := panicWrap(routine.Run, this.ctx)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user