Exemplo n.º 1
0
func (scheduler *fixedDelayScheduler) Schedule(task Task) {
	scheduler.wg.Add(1)
	go func() {
		defer scheduler.wg.Done()

		select {
		case <-scheduler.doneCh:
			return
		case <-time.After(scheduler.initialDelay):
		}

		for {
			err := task()
			if err != nil {
				registry.ErrorNotify()(err)
				return
			}

			select {
			case <-scheduler.doneCh:
				return
			case <-time.After(scheduler.delay):
			}
		}
	}()
}
Exemplo n.º 2
0
func (s *server) wrapHandler(handler Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		s.wg.Add(1)
		defer s.wg.Done()

		defer r.Body.Close()

		w.Header().Set(contentTypeHeaderName, contentTypeApplicationJSON)

		err := handler(w, r)
		if err != nil {
			registry.ErrorNotify()(err)
		}
	}
}
Exemplo n.º 3
0
func TestError(t *testing.T) {
	registry.DefaultServiceRegistry = registry.NewServiceRegistry()

	srv := NewService()
	expected := errors.New("runtime fail")

	err := srv.Forever(func() error {
		go func() {
			time.Sleep(300 * time.Millisecond)
			registry.ErrorNotify()(expected)
		}()
		return nil
	})

	if err != expected {
		t.Error(err)
	}
}
Exemplo n.º 4
0
func (scheduler *fixedPeriodScheduler) Schedule(task Task) {
	scheduler.wg.Add(1)
	go func() {
		defer scheduler.wg.Done()

		select {
		case <-scheduler.doneCh:
			return
		case <-time.After(scheduler.initialDelay):
		}

		for {
			t0 := time.Now()
			err := task()
			if err != nil {
				registry.ErrorNotify()(err)
				return
			}
			executionDuration := time.Now().Sub(t0)

			if executionDuration < scheduler.period {
				select {
				case <-scheduler.doneCh:
					return
				case <-time.After(scheduler.period - executionDuration):
				}
			} else {
				select {
				case <-scheduler.doneCh:
					return
				default:
				}
			}
		}
	}()
}