ability to pause and resume build queue

This commit is contained in:
Brad Rydzewski 2019-03-17 14:31:07 -07:00
parent 680afa1db1
commit bd23f21ff4
9 changed files with 94 additions and 46 deletions

View file

@ -44,6 +44,14 @@ type Scheduler interface {
// returns true if the build has been cancelled.
Cancelled(context.Context, int64) (bool, error)
// Pause pauses the scheduler and prevents new pipelines
// from being scheduled for execution.
Pause(context.Context) error
// Resume unpauses the scheduler, allowing new pipelines
// to be scheduled for execution.
Resume(context.Context) error
// Stats provides statistics for underlying scheduler. The
// data format is scheduler-specific.
Stats(context.Context) (interface{}, error)

View file

@ -249,8 +249,8 @@ func (s Server) Handler() http.Handler {
r.Route("/queue", func(r chi.Router) {
r.Use(acl.AuthorizeAdmin)
r.Get("/", queue.HandleItems(s.Stages))
// r.Post("/", queue.HandleResume(s.Queue))
// r.Delete("/", queue.HandlePause(s.Queue))
r.Post("/", queue.HandleResume(s.Scheduler))
r.Delete("/", queue.HandlePause(s.Scheduler))
})
r.Route("/user", func(r chi.Router) {

View file

@ -30,3 +30,11 @@ var notImplemented = func(w http.ResponseWriter, r *http.Request) {
func HandleItems(store core.StageStore) http.HandlerFunc {
return notImplemented
}
func HandlePause(core.Scheduler) http.HandlerFunc {
return notImplemented
}
func HandleResume(core.Scheduler) http.HandlerFunc {
return notImplemented
}

View file

@ -6,26 +6,26 @@
package queue
// import (
// "net/http"
import (
"net/http"
// "github.com/drone/drone/core"
// "github.com/drone/drone/handler/api/render"
// "github.com/drone/drone/logger"
// )
"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/logger"
)
// // HandlePause returns an http.HandlerFunc that processes
// // an http.Request to pause the queue.
// func HandlePause(queue core.Queue) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// ctx := r.Context()
// err := queue.Pause(ctx)
// if err != nil {
// render.InternalError(w, err)
// logger.FromRequest(r).WithError(err).
// Errorln("api: cannot pause queue")
// return
// }
// w.WriteHeader(http.StatusNoContent)
// }
// }
// HandlePause returns an http.HandlerFunc that processes
// an http.Request to pause the scheduler.
func HandlePause(scheduler core.Scheduler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
err := scheduler.Pause(ctx)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).WithError(err).
Errorln("api: cannot pause scheduler")
return
}
w.WriteHeader(http.StatusNoContent)
}
}

View file

@ -6,26 +6,26 @@
package queue
// import (
// "net/http"
import (
"net/http"
// "github.com/drone/drone/core"
// "github.com/drone/drone/handler/api/render"
// "github.com/drone/drone/logger"
// )
"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/logger"
)
// // HandleResume returns an http.HandlerFunc that processes
// // an http.Request to pause the queue.
// func HandleResume(queue core.Queue) http.HandlerFunc {
// return func(w http.ResponseWriter, r *http.Request) {
// ctx := r.Context()
// err := queue.Resume(ctx)
// if err != nil {
// render.InternalError(w, err)
// logger.FromRequest(r).WithError(err).
// Errorln("api: cannot resume queue")
// return
// }
// w.WriteHeader(http.StatusNoContent)
// }
// }
// HandleResume returns an http.HandlerFunc that processes
// an http.Request to pause the scheduler.
func HandleResume(scheduler core.Scheduler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
err := scheduler.Resume(ctx)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).WithError(err).
Errorln("api: cannot resume scheduler")
return
}
w.WriteHeader(http.StatusNoContent)
}
}

View file

@ -109,7 +109,7 @@ func (s *kubeScheduler) Schedule(ctx context.Context, stage *core.Stage) error {
GenerateName: name,
Namespace: s.namespace(),
Annotations: map[string]string{
"io.drone": "true",
"io.drone": "true",
"io.drone.stage.created": time.Unix(stage.Created, 0).String(),
"io.drone.stage.scheduled": time.Now().String(),
"io.drone.stage.id": fmt.Sprint(stage.ID),
@ -201,6 +201,14 @@ func (s *kubeScheduler) Stats(_ context.Context) (interface{}, error) {
return nil, errors.New("not implemented")
}
func (s *kubeScheduler) Pause(context.Context) error {
return errors.New("not implemented")
}
func (s *kubeScheduler) Resume(context.Context) error {
return errors.New("not implemented")
}
func (s *kubeScheduler) namespace() string {
namespace := s.config.Namespace
if namespace == "" {

View file

@ -48,3 +48,11 @@ func (noop) Cancelled(context.Context, int64) (bool, error) {
func (noop) Stats(context.Context) (interface{}, error) {
return nil, nil
}
func (noop) Pause(context.Context) error {
return nil
}
func (noop) Resume(context.Context) error {
return nil
}

View file

@ -111,7 +111,7 @@ func (s *nomadScheduler) Schedule(ctx context.Context, stage *core.Stage) error
},
},
Meta: map[string]string{
"io.drone": "true",
"io.drone": "true",
"io.drone.stage.created": time.Unix(stage.Created, 0).String(),
"io.drone.stage.scheduled": time.Now().String(),
"io.drone.stage.id": fmt.Sprint(stage.ID),
@ -206,6 +206,14 @@ func (s *nomadScheduler) Stats(context.Context) (interface{}, error) {
return nil, errors.New("not implemented")
}
func (s *nomadScheduler) Pause(context.Context) error {
return errors.New("not implemented")
}
func (s *nomadScheduler) Resume(context.Context) error {
return errors.New("not implemented")
}
// stringToPtr returns the pointer to a string
func stringToPtr(str string) *string {
return &str

View file

@ -48,3 +48,11 @@ func (noop) Cancelled(context.Context, int64) (bool, error) {
func (noop) Stats(context.Context) (interface{}, error) {
return nil, nil
}
func (noop) Pause(context.Context) error {
return nil
}
func (noop) Resume(context.Context) error {
return nil
}