예제 #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):
			}
		}
	}()
}
예제 #2
0
파일: server.go 프로젝트: eklementev/esp
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)
		}
	}
}
예제 #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)
	}
}
예제 #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:
				}
			}
		}
	}()
}