Exemplo n.º 1
0
func TestScheduler_RunAt(t *testing.T) {
	counter = 0

	start := time.Now()
	s := scheduler.NewScheduler()

	cbjoin := make(chan struct{})
	cb := func(v *scheduler.JobView) {
		if v.State != scheduler.JobFinished {
			t.Errorf("wrong state")
		}
		if v.CreatedAt.Before(start) {
			t.Errorf("wrong CreatedAt")
		}
		if v.ScheduledAt.Before(v.CreatedAt) {
			t.Errorf("wrong ScheduledAt")
		}
		if v.Result.(HogeResult).counterSnapshot != 1 {
			t.Errorf("wrong result")
		}
		cbjoin <- struct{}{}
	}
	id := s.RunAt(HogeTask{}, time.Now().Add(100*time.Millisecond), cb)
	if id != scheduler.ID(1) {
		t.Errorf("first task not ID 1 but %d", id)
	}

	v := s.Query(id)
	if v == nil {
		t.Errorf("id should be queryable immediately after RunAt return")
		return
	}

	if v.ID != id {
		t.Errorf("ID mismatch")
	}
	if v.State != scheduler.JobScheduled {
		t.Errorf("wrong state")
	}
	if v.Result != nil {
		t.Errorf("result non-nil before run")
	}

	s.RunAllAndStop()

	if counter != 1 {
		t.Errorf("err")
	}
	<-cbjoin
}
Exemplo n.º 2
0
func Install(srv *mgmt.Server, s *scheduler.Scheduler) {
	rtr := srv.APIRouter().PathPrefix("/scheduler").Subrouter()

	rtr.HandleFunc("/stats", mgmt.JSONHandler(func(req *http.Request) interface{} {
		return s.GetStats()
	}))
	rtr.HandleFunc("/job/all", mgmt.JSONHandler(func(req *http.Request) interface{} {
		return s.QueryAll()
	}))
	rtr.HandleFunc("/job/{id:[0-9]+}", mgmt.JSONHandler(func(req *http.Request) interface{} {
		vars := mux.Vars(req)
		nid, err := strconv.ParseUint(vars["id"], 10, 32)
		if err != nil {
			return err
		}
		return s.Query(scheduler.ID(nid))
	}))
}