Skip to content

Commit

Permalink
fix: choose index when stopping process
Browse files Browse the repository at this point in the history
  • Loading branch information
dunstorm committed Jun 11, 2022
1 parent 254a23e commit 61a9799
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cmd/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var killCmd = &cobra.Command{
if isRunning {
procs := master.ListProcs()
if len(procs) > 0 {
for _, p := range procs {
for index, p := range procs {
if p.ProcStatus.Status == "online" {
logger.Info().Msgf("Applying action stopProcessId on app [%s](pid: [ %d ])", p.Name, p.Pid)
master.StopProcessByIndex(p.ID)
master.StopProcessByIndex(index)
}
}
} else {
Expand Down
20 changes: 9 additions & 11 deletions rpc/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ import (

type API struct {
logger *zerolog.Logger
database map[int]shared.Process
database []shared.Process
mu sync.Mutex
}

func (api *API) GetDB(empty string, reply *[]shared.Process) error {
database := make([]shared.Process, len(api.database))
for i, v := range api.database {
database[i] = v
}
*reply = database
*reply = api.database
return nil
}

Expand Down Expand Up @@ -64,7 +60,7 @@ func (api *API) AddProcess(process shared.Process, reply *shared.Process) error
process.SetProcess(found)
process.SetToStop(false)
api.mu.Lock()
api.database[len(api.database)] = process
api.database = append(api.database, process)
api.mu.Unlock()
*reply = process
return nil
Expand Down Expand Up @@ -92,9 +88,11 @@ func (api *API) StopProcessByIndex(processIndex int, reply *bool) error {

func (api *API) StopProcess(process shared.Process, reply *bool) error {
var found shared.Process
for _, p := range api.database {
var foundIndex int
for i, p := range api.database {
if p.Name == process.Name || p.ID == process.ID {
found = p
foundIndex = i
break
}
}
Expand Down Expand Up @@ -127,7 +125,7 @@ func (api *API) StopProcess(process shared.Process, reply *bool) error {
found.SetStatus("stopped")

api.mu.Lock()
api.database[found.ID] = found
api.database[foundIndex] = found
api.mu.Unlock()

*reply = true
Expand Down Expand Up @@ -168,12 +166,12 @@ func (api *API) UpdateProcess(newProcess shared.Process, reply *shared.Process)
// DeleteProcess takes a Process type and deletes it from ProcessArray
func (api *API) DeleteProcess(process shared.Process, reply *shared.Process) error {
var deleted shared.Process
for _, v := range api.database {
for i, v := range api.database {
if v.Name == process.Name || v.ID == process.ID {
// Delete Process by appending the items before it and those
// after to the database variable
api.mu.Lock()
delete(api.database, process.ID)
api.database = append(api.database[:i], api.database[i+1:]...)
api.mu.Unlock()
deleted = process
break
Expand Down
2 changes: 1 addition & 1 deletion rpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func New() {
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
handler := &API{
logger: &logger,
database: make(map[int]shared.Process, 0),
database: make([]shared.Process, 0),
}

// Publish the receivers methods
Expand Down

0 comments on commit 61a9799

Please sign in to comment.