diff --git a/experiments/cpu-stress/experiment/cpu-stress.go b/experiments/cpu-stress/experiment/cpu-stress.go index cb6f023..3b4ab54 100644 --- a/experiments/cpu-stress/experiment/cpu-stress.go +++ b/experiments/cpu-stress/experiment/cpu-stress.go @@ -26,6 +26,7 @@ import ( "github.com/litmuschaos/m-agent/pkg/cpu" "github.com/litmuschaos/m-agent/pkg/probes" stressng "github.com/litmuschaos/m-agent/pkg/stress-ng" + "github.com/pkg/errors" ) var ( @@ -60,15 +61,7 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { action, reqID, payload, err := messages.ListenForClientMessage(conn) if err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetClientMessageReadErrorPrefix()+err.Error()); err != nil { - clientMessageReadLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - clientMessageReadLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetClientMessageReadErrorPrefix(), err, clientMessageReadLogger) return } @@ -76,26 +69,12 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { case "CHECK_STEADY_STATE": if err := stressng.CheckStressNG(); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetSteadyStateCheckErrorPrefix()+err.Error()); err != nil { - steadyStateCheckErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - steadyStateCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetSteadyStateCheckErrorPrefix(), err, steadyStateCheckErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - steadyStateCheckErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - steadyStateCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, steadyStateCheckErrorLogger) return } @@ -103,51 +82,23 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { cmd, err = cpu.StressCPU(payload, &stdout, &stderr) if err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetExecuteExperimentErrorPrefix()+err.Error()); err != nil { - executeExperimentErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - executeExperimentErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetExecuteExperimentErrorPrefix(), err, executeExperimentErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - executeExperimentErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - executeExperimentErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, executeExperimentErrorLogger) return } case "CHECK_LIVENESS": if err := stressng.CheckStressNGProcessLiveness(cmd.Process.Pid); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetLivenessCheckErrorPrefix()+err.Error()); err != nil { - livenessCheckErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - livenessCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetLivenessCheckErrorPrefix(), err, livenessCheckErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - livenessCheckErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - livenessCheckErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, livenessCheckErrorLogger) return } @@ -157,88 +108,42 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { if err != nil { if err := messages.SendMessageToClient(conn, "PROBE_ERROR", reqID, errorcodes.GetCommandProbeExecutionErrorPrefix()+err.Error()); err != nil { - - commandProbeExecutionErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - - if err := conn.Close(); err != nil { - commandProbeExecutionErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger) return } } else { if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, cmdProbeStdout); err != nil { - - commandProbeExecutionErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - commandProbeExecutionErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger) return } } case "REVERT_CHAOS": if err := stressng.RevertStressNGProcess(cmd, &stderr); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetChaosRevertErrorPrefix()+err.Error()); err != nil { - chaosRevertErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - chaosRevertErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetChaosRevertErrorPrefix(), err, chaosRevertErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, stdout.String()); err != nil { - - chaosRevertErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - chaosRevertErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, chaosRevertErrorLogger) return } case "ABORT_EXPERIMENT": if err := stressng.AbortStressNGProcess(cmd); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetChaosAbortErrorPrefix()+err.Error()); err != nil { - chaosAbortErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetChaosAbortErrorPrefix(), err, chaosAbortErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - chaosAbortErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, chaosAbortErrorLogger) return } if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil { - - chaosAbortErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - chaosAbortErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, chaosAbortErrorLogger) return } @@ -250,14 +155,7 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { case "CLOSE_CONNECTION": if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil { - closeConnectionErrorLogger.Printf("Error occured while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - closeConnectionErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - - return } if err := conn.Close(); err != nil { @@ -267,14 +165,7 @@ func CPUStress(w http.ResponseWriter, r *http.Request) { return default: - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetInvalidActionErrorPrefix()+"Invalid action: "+action); err != nil { - invalidActionErrorLogger.Printf("Error occured while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - invalidActionErrorLogger.Printf("Error occured while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetInvalidActionErrorPrefix(), errors.Errorf("Invalid action: "+action), invalidActionErrorLogger) return } } diff --git a/experiments/process-kill/experiment/process-kill.go b/experiments/process-kill/experiment/process-kill.go index 9b718d5..afdf860 100644 --- a/experiments/process-kill/experiment/process-kill.go +++ b/experiments/process-kill/experiment/process-kill.go @@ -23,6 +23,7 @@ import ( "github.com/litmuschaos/m-agent/internal/m-agent/upgrader" "github.com/litmuschaos/m-agent/pkg/probes" "github.com/litmuschaos/m-agent/pkg/process" + "github.com/pkg/errors" ) // ProcessKill listens for the client actions and executes them as appropriate @@ -49,15 +50,7 @@ func ProcessKill(w http.ResponseWriter, r *http.Request) { action, reqID, payload, err := messages.ListenForClientMessage(conn) if err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetClientMessageReadErrorPrefix()+err.Error()); err != nil { - clientMessageReadLogger.Printf("Error occurred while sending error message to client, %v", err) - } - - if err := conn.Close(); err != nil { - clientMessageReadLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetClientMessageReadErrorPrefix(), err, clientMessageReadLogger) return } @@ -65,105 +58,52 @@ func ProcessKill(w http.ResponseWriter, r *http.Request) { case "CHECK_STEADY_STATE": if err := process.ProcessStateCheck(payload); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetSteadyStateCheckErrorPrefix()+err.Error()); err != nil { - steadyStateCheckErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - steadyStateCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetSteadyStateCheckErrorPrefix(), err, steadyStateCheckErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - steadyStateCheckErrorLogger.Printf("Error occurred while sending feedback message to client, %v", err) - - if err := conn.Close(); err != nil { - steadyStateCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, steadyStateCheckErrorLogger) return } case "EXECUTE_EXPERIMENT": if err := process.KillTargetProcesses(payload); err != nil { - - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetExecuteExperimentErrorPrefix()+err.Error()); err != nil { - executeExperimentErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - executeExperimentErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetExecuteExperimentErrorPrefix(), err, executeExperimentErrorLogger) return } if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - executeExperimentErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - executeExperimentErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, executeExperimentErrorLogger) return } case "EXECUTE_COMMAND": - stdout, err := probes.ExecuteCmdProbeCommand(payload) + cmdProbeStdout, err := probes.ExecuteCmdProbeCommand(payload) if err != nil { if err := messages.SendMessageToClient(conn, "PROBE_ERROR", reqID, errorcodes.GetCommandProbeExecutionErrorPrefix()+err.Error()); err != nil { - - commandProbeExecutionErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err) - - if err := conn.Close(); err != nil { - commandProbeExecutionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger) return } } else { - if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, stdout); err != nil { - - commandProbeExecutionErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - commandProbeExecutionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, cmdProbeStdout); err != nil { + messages.HandleFeedbackTransmissionError(conn, err, commandProbeExecutionErrorLogger) return } } case "CHECK_LIVENESS": if err := messages.SendMessageToClient(conn, "ACTION_SUCCESSFUL", reqID, nil); err != nil { - - livenessCheckErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - livenessCheckErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleFeedbackTransmissionError(conn, err, livenessCheckErrorLogger) return } case "CLOSE_CONNECTION": if err := messages.SendMessageToClient(conn, "CLOSE_CONNECTION", reqID, nil); err != nil { - closeConnectionErrorLogger.Printf("Error occurred while sending feedback message to client, err: %v", err) - - if err := conn.Close(); err != nil { - closeConnectionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - - return } if err := conn.Close(); err != nil { @@ -173,14 +113,7 @@ func ProcessKill(w http.ResponseWriter, r *http.Request) { return default: - if err := messages.SendMessageToClient(conn, "ERROR", reqID, errorcodes.GetInvalidActionErrorPrefix()+"Invalid action: "+action); err != nil { - invalidActionErrorLogger.Printf("Error occurred while sending error message to client, err: %v", err) - } - - if err := conn.Close(); err != nil { - invalidActionErrorLogger.Printf("Error occurred while closing the connection, err: %v", err) - } - + messages.HandleActionExecutionError(conn, reqID, errorcodes.GetInvalidActionErrorPrefix(), errors.Errorf("Invalid action: "+action), invalidActionErrorLogger) return } } diff --git a/internal/m-agent/messages/messages.go b/internal/m-agent/messages/messages.go index de08bdb..e7a0c07 100644 --- a/internal/m-agent/messages/messages.go +++ b/internal/m-agent/messages/messages.go @@ -15,6 +15,7 @@ package messages import ( "encoding/json" + "log" "github.com/gorilla/websocket" ) @@ -47,3 +48,25 @@ func SendMessageToClient(conn *websocket.Conn, action, reqID string, payload int return conn.WriteJSON(Message{action, payload, reqID}) } + +// HandleActionExecutionError handles an error generated while performing the action requested by the client +func HandleActionExecutionError(conn *websocket.Conn, reqID, errorCode string, err error, logger *log.Logger) { + + if err := SendMessageToClient(conn, "ERROR", reqID, errorCode+err.Error()); err != nil { + logger.Printf("Error occured while sending error message to client, err: %v", err) + } + + if err := conn.Close(); err != nil { + logger.Printf("Error occured while closing the connection, err: %v", err) + } +} + +// HandleFeedbackTransmissionError handles an error generated while sending the feedback for an earlier requested action to the client +func HandleFeedbackTransmissionError(conn *websocket.Conn, err error, logger *log.Logger) { + + logger.Printf("Error occured while sending feedback message to client, err: %v", err) + + if err := conn.Close(); err != nil { + logger.Printf("Error occured while closing the connection, err: %v", err) + } +}