(scriptName string, scriptCmd []string)
| 455 | } |
| 456 | |
| 457 | func (this *ControlServer) startScriptInBackground(scriptName string, scriptCmd []string) { |
| 458 | logger := this.logger.WithField("script", scriptName) |
| 459 | |
| 460 | this.customScriptsLock.Lock() |
| 461 | defer this.customScriptsLock.Unlock() |
| 462 | |
| 463 | running := this.customScriptsRunning[scriptName] |
| 464 | if running { |
| 465 | logger.Warn("script already running, ignoring start request") |
| 466 | return |
| 467 | } |
| 468 | |
| 469 | logger.Infof("running custom script %v", scriptCmd) |
| 470 | this.customScriptsStatus[scriptName] = "starting" |
| 471 | this.customScriptsRunning[scriptName] = true |
| 472 | this.customScriptsLogs[scriptName] = "" |
| 473 | |
| 474 | go func() { |
| 475 | cmd := exec.Command(scriptCmd[0], scriptCmd[1:]...) |
| 476 | |
| 477 | this.customScriptsLock.Lock() |
| 478 | this.customScriptsStatus[scriptName] = "running" |
| 479 | this.customScriptsLock.Unlock() |
| 480 | |
| 481 | stdoutStderr, err := cmd.CombinedOutput() |
| 482 | |
| 483 | this.customScriptsLock.Lock() |
| 484 | if err != nil { |
| 485 | this.customScriptsStatus[scriptName] = fmt.Sprintf("exitted with error: %v", err) |
| 486 | |
| 487 | var exitError *exec.ExitError |
| 488 | defaultExitCode := 1 |
| 489 | if errors.As(err, &exitError) { |
| 490 | this.customScriptsExitCode[scriptName] = exitError.ExitCode() |
| 491 | LogWithFields(Fields{ |
| 492 | "scriptName": scriptName, |
| 493 | "error": err.Error(), |
| 494 | "exitCode": exitError.ExitCode(), |
| 495 | }).Error("custom script ran with errors") |
| 496 | } else { |
| 497 | this.customScriptsExitCode[scriptName] = defaultExitCode |
| 498 | LogWithFields(Fields{ |
| 499 | "scriptName": scriptName, |
| 500 | "error": err.Error(), |
| 501 | "exitCode": exitError.ExitCode(), |
| 502 | }).Error("custom script ran with errors but could not determine the exit code") |
| 503 | } |
| 504 | } else { |
| 505 | this.customScriptsStatus[scriptName] = "success" |
| 506 | this.customScriptsExitCode[scriptName] = 0 |
| 507 | logger.Info("custom script ran successfully") |
| 508 | } |
| 509 | this.customScriptsLogs[scriptName] = string(stdoutStderr) |
| 510 | this.customScriptsRunning[scriptName] = false |
| 511 | this.customScriptsLock.Unlock() |
| 512 | }() |
| 513 | } |
no test coverage detected