// PostWorker accepts a request to allocate a new // worker to the pool. // // POST /sudo/api/workers // func PostWorker(c web.C, w http.ResponseWriter, r *http.Request) { ctx := context.FromC(c) pool := pool.FromContext(ctx) server := resource.Server{} // read the worker data from the body defer r.Body.Close() if err := json.NewDecoder(r.Body).Decode(&server); err != nil { println(err.Error()) w.WriteHeader(http.StatusBadRequest) return } // add the worker to the database err := datastore.PutServer(ctx, &server) if err != nil { println(err.Error()) w.WriteHeader(http.StatusInternalServerError) return } // create a new worker from the Docker client client, err := docker.NewCert(server.Host, []byte(server.Cert), []byte(server.Key)) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } // append user-friendly data to the host client.Host = client.Host pool.Allocate(client) w.WriteHeader(http.StatusOK) }
// Delete accepts a request to delete a worker // from the pool. // // DELETE /sudo/api/workers/:id // func DelWorker(c web.C, w http.ResponseWriter, r *http.Request) { ctx := context.FromC(c) pool := pool.FromContext(ctx) uuid := c.URLParams["id"] server, err := datastore.GetServer(ctx, uuid) if err != nil { w.WriteHeader(http.StatusNotFound) return } err = datastore.DelServer(ctx, server) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } for _, worker := range pool.List() { if worker.(*docker.Docker).UUID == uuid { pool.Deallocate(worker) w.WriteHeader(http.StatusNoContent) return } } w.WriteHeader(http.StatusNotFound) }
// do is a blocking function that waits for an // available worker to process work. func (d *Director) do(c context.Context, work *worker.Work) { d.markPending(work) var pool = pool.FromContext(c) var worker = <-pool.Reserve() // var worker worker.Worker // // // waits for an available worker. This is a blocking // // operation and will reject any nil workers to avoid // // a potential panic. // select { // case worker = <-pool.Reserve(): // if worker != nil { // break // } // } d.markStarted(work, worker) worker.Do(c, work) d.markComplete(work) pool.Release(worker) }
// GetWorkers accepts a request to retrieve the list // of registered workers and return the results // in JSON format. // // GET /api/workers // func GetWorkers(c web.C, w http.ResponseWriter, r *http.Request) { ctx := context.FromC(c) workers := pool.FromContext(ctx).List() json.NewEncoder(w).Encode(workers) }