Skip to content

Commit a00ce0d

Browse files
moved logic in pc runner
1 parent 12519d9 commit a00ce0d

5 files changed

Lines changed: 266 additions & 90 deletions

File tree

src/api/pc_api.go

Lines changed: 27 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"errors"
45
"net/http"
56
"strconv"
67
"sync"
@@ -222,25 +223,20 @@ func (api *PcApi) StopProcesses(c *gin.Context) {
222223
// @Router /namespace/stop/{name} [patch]
223224
func (api *PcApi) StopNamespace(c *gin.Context) {
224225
ns := c.Param("name")
225-
states, err := api.project.GetProcessesState()
226+
227+
stopped, err := api.project.StopNamespace(ns)
226228
if err != nil {
227-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
228-
return
229-
}
230-
processNames := make([]string, 0)
231-
for _, state := range states.States {
232-
if state.Namespace == ns {
233-
processNames = append(processNames, state.Name)
229+
if errors.Is(err, app.ErrNamespaceNotFound) {
230+
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + ns})
231+
return
234232
}
235-
}
236-
if len(processNames) == 0 {
237-
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + ns})
233+
if len(stopped) > 0 {
234+
c.JSON(http.StatusBadRequest, stopped)
235+
return
236+
}
237+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
238238
return
239239
}
240-
stopped, err := api.project.StopProcesses(processNames)
241-
if err != nil {
242-
c.JSON(http.StatusBadRequest, stopped)
243-
}
244240
c.JSON(http.StatusOK, stopped)
245241
}
246242

@@ -257,41 +253,17 @@ func (api *PcApi) StopNamespace(c *gin.Context) {
257253
// @Router /namespace/disable/{name} [patch]
258254
func (api *PcApi) DisableNamespace(c *gin.Context) {
259255
name := c.Param("name")
260-
states, err := api.project.GetProcessesState()
256+
results, err := api.project.DisableNamespace(name)
261257
if err != nil {
262-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
263-
return
264-
}
265-
names := make([]string, 0)
266-
for _, state := range states.States {
267-
if state.Namespace == name {
268-
names = append(names, state.Name)
269-
}
270-
}
271-
if len(names) == 0 {
272-
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + name})
273-
return
274-
}
275-
276-
results := make(map[string]string)
277-
failures := 0
278-
for _, processName := range names {
279-
cfg, err := api.project.GetProcessInfo(processName)
280-
if err != nil {
281-
results[processName] = err.Error()
282-
failures++
283-
continue
258+
if errors.Is(err, app.ErrNamespaceNotFound) {
259+
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + name})
260+
return
284261
}
285-
cfg.Disabled = true
286-
if err := api.project.UpdateProcess(cfg); err != nil {
287-
results[processName] = err.Error()
288-
failures++
289-
} else {
290-
results[processName] = "ok"
262+
if len(results) > 0 {
263+
c.JSON(http.StatusBadRequest, results)
264+
return
291265
}
292-
}
293-
if failures > 0 {
294-
c.JSON(http.StatusBadRequest, results)
266+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
295267
return
296268
}
297269
c.JSON(http.StatusOK, results)
@@ -310,41 +282,17 @@ func (api *PcApi) DisableNamespace(c *gin.Context) {
310282
// @Router /namespace/enable/{name} [patch]
311283
func (api *PcApi) EnableNamespace(c *gin.Context) {
312284
ns := c.Param("name")
313-
states, err := api.project.GetProcessesState()
285+
results, err := api.project.EnableNamespace(ns)
314286
if err != nil {
315-
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
316-
return
317-
}
318-
names := make([]string, 0)
319-
for _, st := range states.States {
320-
if st.Namespace == ns {
321-
names = append(names, st.Name)
287+
if errors.Is(err, app.ErrNamespaceNotFound) {
288+
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + ns})
289+
return
322290
}
323-
}
324-
if len(names) == 0 {
325-
c.JSON(http.StatusNotFound, gin.H{"error": "no processes in namespace: " + ns})
326-
return
327-
}
328-
329-
results := make(map[string]string)
330-
failures := 0
331-
for _, name := range names {
332-
cfg, err := api.project.GetProcessInfo(name)
333-
if err != nil {
334-
results[name] = err.Error()
335-
failures++
336-
continue
291+
if len(results) > 0 {
292+
c.JSON(http.StatusBadRequest, results)
293+
return
337294
}
338-
cfg.Disabled = false
339-
if err := api.project.UpdateProcess(cfg); err != nil {
340-
results[name] = err.Error()
341-
failures++
342-
} else {
343-
results[name] = "ok"
344-
}
345-
}
346-
if failures > 0 {
347-
c.JSON(http.StatusBadRequest, results)
295+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
348296
return
349297
}
350298
c.JSON(http.StatusOK, results)

src/app/errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app
2+
3+
import "errors"
4+
5+
// ErrNamespaceNotFound indicates there are no processes in the given namespace
6+
var ErrNamespaceNotFound = errors.New("namespace not found")
7+

src/app/project_interface.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ type IProject interface {
2727
// Value in map is `ok` on success, else on error to stop specific process.
2828
// Iterates all processes (best effort).
2929
// If all proceses were stopped, error is nil.
30-
StopProcesses(names []string) (map[string]string, error)
30+
StopProcesses(names []string) (map[string]string, error)
31+
// StopNamespace stops all processes in the given namespace.
32+
// Returns a map of process name -> result ("ok" or error string).
33+
// If namespace has no processes, returns ErrNamespaceNotFound.
34+
StopNamespace(name string) (map[string]string, error)
35+
// DisableNamespace disables all processes in the given namespace.
36+
// Returns a map of process name -> result ("ok" or error string).
37+
// If namespace has no processes, returns ErrNamespaceNotFound.
38+
DisableNamespace(name string) (map[string]string, error)
39+
// EnableNamespace enables all processes in the given namespace.
40+
// Returns a map of process name -> result ("ok" or error string).
41+
// If namespace has no processes, returns ErrNamespaceNotFound.
42+
EnableNamespace(name string) (map[string]string, error)
3143
StartProcess(name string) error
3244
RestartProcess(name string) error
3345
ScaleProcess(name string, scale int) error

src/app/project_runner.go

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,15 +1140,15 @@ func (p *ProjectRunner) UpdateProcess(updated *types.ProcessConfig) error {
11401140
}
11411141

11421142
func (p *ProjectRunner) UpdateProcesses(processes *types.Processes) (map[string]string, error) {
1143-
status := make(map[string]string)
1144-
if processes == nil || len(*processes) == 0 {
1145-
return status, fmt.Errorf("no processes provided")
1146-
}
1147-
1148-
var errs []error
1149-
for _, process := range *processes {
1150-
if err := p.UpdateProcess(&process); err != nil {
1151-
status[process.ReplicaName] = err.Error()
1143+
status := make(map[string]string)
1144+
if processes == nil || len(*processes) == 0 {
1145+
return status, fmt.Errorf("no processes provided")
1146+
}
1147+
1148+
var errs []error
1149+
for _, process := range *processes {
1150+
if err := p.UpdateProcess(&process); err != nil {
1151+
status[process.ReplicaName] = err.Error()
11521152
errs = append(errs, err)
11531153
continue
11541154
}
@@ -1158,7 +1158,109 @@ func (p *ProjectRunner) UpdateProcesses(processes *types.Processes) (map[string]
11581158
if len(errs) == len(*processes) {
11591159
return nil, errors.Join(errs...)
11601160
}
1161-
return status, nil
1161+
return status, nil
1162+
}
1163+
1164+
// StopNamespace stops all processes in a given namespace.
1165+
// Returns map of process name -> result ("ok" or error).
1166+
// If namespace contains no processes, returns ErrNamespaceNotFound.
1167+
func (p *ProjectRunner) StopNamespace(namespace string) (map[string]string, error) {
1168+
states, err := p.GetProcessesState()
1169+
if err != nil {
1170+
return nil, err
1171+
}
1172+
1173+
names := make([]string, 0)
1174+
for _, state := range states.States {
1175+
if state.Namespace == namespace {
1176+
names = append(names, state.Name)
1177+
}
1178+
}
1179+
if len(names) == 0 {
1180+
return nil, ErrNamespaceNotFound
1181+
}
1182+
1183+
return p.StopProcesses(names)
1184+
}
1185+
1186+
// DisableNamespace sets Disabled=true for all processes in the namespace.
1187+
// Returns per-process results map and error if any failures occurred.
1188+
func (p *ProjectRunner) DisableNamespace(namespace string) (map[string]string, error) {
1189+
states, err := p.GetProcessesState()
1190+
if err != nil {
1191+
return nil, err
1192+
}
1193+
names := make([]string, 0)
1194+
for _, st := range states.States {
1195+
if st.Namespace == namespace {
1196+
names = append(names, st.Name)
1197+
}
1198+
}
1199+
if len(names) == 0 {
1200+
return nil, ErrNamespaceNotFound
1201+
}
1202+
1203+
results := make(map[string]string)
1204+
failures := 0
1205+
for _, name := range names {
1206+
cfg, err := p.GetProcessInfo(name)
1207+
if err != nil {
1208+
results[name] = err.Error()
1209+
failures++
1210+
continue
1211+
}
1212+
cfg.Disabled = true
1213+
if err := p.UpdateProcess(cfg); err != nil {
1214+
results[name] = err.Error()
1215+
failures++
1216+
} else {
1217+
results[name] = "ok"
1218+
}
1219+
}
1220+
if failures > 0 {
1221+
return results, errors.New("failed to disable some processes")
1222+
}
1223+
return results, nil
1224+
}
1225+
1226+
// EnableNamespace sets Disabled=false for all processes in the namespace.
1227+
// Returns per-process results map and error if any failures occurred.
1228+
func (p *ProjectRunner) EnableNamespace(namespace string) (map[string]string, error) {
1229+
states, err := p.GetProcessesState()
1230+
if err != nil {
1231+
return nil, err
1232+
}
1233+
names := make([]string, 0)
1234+
for _, st := range states.States {
1235+
if st.Namespace == namespace {
1236+
names = append(names, st.Name)
1237+
}
1238+
}
1239+
if len(names) == 0 {
1240+
return nil, ErrNamespaceNotFound
1241+
}
1242+
1243+
results := make(map[string]string)
1244+
failures := 0
1245+
for _, name := range names {
1246+
cfg, err := p.GetProcessInfo(name)
1247+
if err != nil {
1248+
results[name] = err.Error()
1249+
failures++
1250+
continue
1251+
}
1252+
cfg.Disabled = false
1253+
if err := p.UpdateProcess(cfg); err != nil {
1254+
results[name] = err.Error()
1255+
failures++
1256+
} else {
1257+
results[name] = "ok"
1258+
}
1259+
}
1260+
if failures > 0 {
1261+
return results, errors.New("failed to enable some processes")
1262+
}
1263+
return results, nil
11621264
}
11631265

11641266
func (p *ProjectRunner) RemoveNamespace(namespace string) (map[string]string, error) {

0 commit comments

Comments
 (0)