// NewCrontab creates a cron server. func NewCrontab(freq time.Duration) *Crontab { c := &Crontab{ jobs: make(map[string]Job), commandChan: make(chan *command), ticker: time.NewTicker(freq), } c.loop = loop.GoRecoverable(c.backendLoop, c.checkRecovering) return c }
// newSystemMonitor starts the system monitor. func newSystemMonitor() *systemMonitor { m := &systemMonitor{ measuringChan: make(chan *Measuring, 1000), ssvChangeChan: make(chan *ssvChange, 1000), retrieverRegistrationChan: make(chan *retrieverRegistration, 10), commandChan: make(chan *command), } m.backend = loop.GoRecoverable(m.backendLoop, m.checkRecovering) return m }
// Test error inside a recovered loop. func TestRecoveringsError(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) done := false count := 0 l := loop.GoRecoverable(generateErrorPanicBackend(&done, &count), catchTimeout) time.Sleep(longDelay) assert.ErrorMatch(l.Stop(), "timed out", "error has to be 'timed out'") assert.True(done, "backend has done") status, _ := l.Error() assert.Equal(loop.Stopped, status, "loop is stopped") }
// Test regular stop of a recovered loop. func TestStopRecoverings(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) done := false count := 0 l := loop.GoRecoverable(generateSimplePanicBackend(&done, &count), ignorePanics) time.Sleep(longDelay) assert.Nil(l.Stop(), "no error after simple stop") assert.True(done, "backend has done") status, _ := l.Error() assert.Equal(loop.Stopped, status, "loop is stopped") }
// Test recoverings after panics. func TestRecoverings(t *testing.T) { assert := asserts.NewTestingAssertion(t, true) done := false count := 0 l := loop.GoRecoverable(generateSimplePanicBackend(&done, &count), checkRecovering) time.Sleep(veryLongDelay) assert.ErrorMatch(l.Stop(), "too many panics", "error has to be 'too many panics'") assert.True(done, "backend has done") assert.Equal(count, 5, "loop has to be restarted 5 times") status, _ := l.Error() assert.Equal(loop.Stopped, status, "loop is stopped") }
func ExampleRecoverFunc() { printChan := make(chan string) loopFunc := func(l loop.Loop) error { for { select { case <-l.ShallStop(): return nil case str := <-printChan: println(str) } } } recoverFunc := func(rs loop.Recoverings) (loop.Recoverings, error) { if len(rs) >= 5 { return nil, errors.New("too many panics") } return rs, nil } loop.GoRecoverable(loopFunc, recoverFunc) }