Merge pull request #4262 from dokku/4259-better-restore-logging

Improve ps:restore logging
This commit is contained in:
Jose Diaz-Gonzalez
2020-12-20 13:51:10 -05:00
committed by GitHub
4 changed files with 29 additions and 86 deletions

View File

@@ -6,25 +6,20 @@ import (
"sync"
)
// ParallelCommand is a type that declares functions
// parallelCommand is a type that declares functions
// the ps plugins can execute in parallel
type ParallelCommand func(string) error
type parallelCommand func(string) error
// ParallelCommandRun contains all the arguments
// necessary for a parallel command run
type ParallelCommandRun struct {
Name string
}
// ParallelCommandResult is the result of a parallel
// parallelCommandResult is the result of a parallel
// command run
type ParallelCommandResult struct {
Name string
Error error
type parallelCommandResult struct {
Name string
CommandName string
Error error
}
// RunCommandAgainstAllApps runs a given ParallelCommand against all apps
func RunCommandAgainstAllApps(command ParallelCommand, commandName string, parallelCount int) error {
// RunCommandAgainstAllApps runs a given parallelCommand against all apps
func RunCommandAgainstAllApps(command parallelCommand, commandName string, parallelCount int) error {
runInSerial := false
if parallelCount < -1 {
@@ -49,8 +44,8 @@ func RunCommandAgainstAllApps(command ParallelCommand, commandName string, paral
return RunCommandAgainstAllAppsInParallel(command, commandName, parallelCount)
}
// RunCommandAgainstAllAppsInParallel runs a given ParallelCommand against all apps in parallel
func RunCommandAgainstAllAppsInParallel(command ParallelCommand, commandName string, parallelCount int) error {
// RunCommandAgainstAllAppsInParallel runs a given parallelCommand against all apps in parallel
func RunCommandAgainstAllAppsInParallel(command parallelCommand, commandName string, parallelCount int) error {
apps, err := DokkuApps()
if err != nil {
LogWarn(err.Error())
@@ -58,19 +53,19 @@ func RunCommandAgainstAllAppsInParallel(command ParallelCommand, commandName str
}
jobs := make(chan string, parallelCount)
results := make(chan ParallelCommandResult, len(apps))
results := make(chan parallelCommandResult, len(apps))
go allocateJobs(apps, jobs)
done := make(chan error)
go aggregateResults(results, done)
createParallelWorkerPool(jobs, results, command, parallelCount)
createParallelWorkerPool(jobs, results, command, commandName, parallelCount)
err = <-done
return err
}
// RunCommandAgainstAllAppsSerially runs a given ParallelCommand against all apps serially
func RunCommandAgainstAllAppsSerially(command ParallelCommand, commandName string) error {
// RunCommandAgainstAllAppsSerially runs a given parallelCommand against all apps serially
func RunCommandAgainstAllAppsSerially(command parallelCommand, commandName string) error {
apps, err := DokkuApps()
if err != nil {
LogWarn(err.Error())
@@ -81,6 +76,7 @@ func RunCommandAgainstAllAppsSerially(command ParallelCommand, commandName strin
for _, appName := range apps {
LogInfo1(fmt.Sprintf("Running %s against app %s", commandName, appName))
if err = command(appName); err != nil {
LogWarn(fmt.Sprintf("Error running %s against app %s: %s", commandName, appName, err.Error()))
errorCount++
}
}
@@ -99,12 +95,12 @@ func allocateJobs(input []string, jobs chan string) {
close(jobs)
}
func aggregateResults(results chan ParallelCommandResult, done chan error) {
func aggregateResults(results chan parallelCommandResult, done chan error) {
var parallelError error
errorCount := 0
for result := range results {
if result.Error != nil {
LogWarn(fmt.Sprintf("Error running command against %s", result.Name))
LogWarn(fmt.Sprintf("Error running %s against %s: %s", result.CommandName, result.Name, result.Error.Error()))
errorCount++
}
}
@@ -114,23 +110,24 @@ func aggregateResults(results chan ParallelCommandResult, done chan error) {
done <- parallelError
}
func createParallelWorker(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, wg *sync.WaitGroup, workerID int) {
func createParallelWorker(jobs chan string, results chan parallelCommandResult, command parallelCommand, commandName string, wg *sync.WaitGroup, workerID int) {
for job := range jobs {
LogInfo1(fmt.Sprintf("Running command against %s", job))
output := ParallelCommandResult{
Name: job,
Error: command(job),
output := parallelCommandResult{
Name: job,
CommandName: commandName,
Error: command(job),
}
results <- output
}
wg.Done()
}
func createParallelWorkerPool(jobs chan string, results chan ParallelCommandResult, command ParallelCommand, numberOfWorkers int) {
func createParallelWorkerPool(jobs chan string, results chan parallelCommandResult, command parallelCommand, commandName string, numberOfWorkers int) {
var wg sync.WaitGroup
for i := 0; i < numberOfWorkers; i++ {
wg.Add(1)
go createParallelWorker(jobs, results, command, &wg, i)
go createParallelWorker(jobs, results, command, commandName, &wg, i)
}
wg.Wait()
close(results)

View File

@@ -168,5 +168,5 @@ nginx_clear_config() {
declare desc="Remove the nginx conf file"
declare APP="$1"
rm -f "$DOKKU_ROOT/$APP/nginx.conf"
fn-nginx-vhosts-nginx-init-cmd "reload"
suppress_output fn-nginx-vhosts-nginx-init-cmd "reload"
}

View File

@@ -1,57 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/docker-options/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
source "$PLUGIN_AVAILABLE_PATH/ps/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
fn-ps-parallel-cmd() {
declare desc="Run a ps command against all apps"
declare CMD="$1"
local GNU_PARALLEL has_apps=false
for APP in $(dokku_apps); do
has_apps=true
break
done
if [[ "$has_apps" == "false" ]]; then
dokku_log_info1 "No apps detected"
return
fi
if which parallel >/dev/null 2>&1; then
dokku_log_info1 "Running 'dokku ps:${CMD}' in parallel"
GNU_PARALLEL=$(parallel -V 2>&1 | grep GNU || true)
if [[ -z "$GNU_PARALLEL" ]]; then
# shellcheck disable=SC2046
parallel $DOKKU_PARALLEL_ARGUMENTS -i bash -c "dokku ps:${CMD} {}" -- $(dokku_apps)
else
# shellcheck disable=SC2046
dokku_apps | parallel $DOKKU_PARALLEL_ARGUMENTS "dokku ps:${CMD} {}"
fi
return
fi
dokku_log_info1 "Running 'dokku ps:${CMD}' in serially"
for APP in $(dokku_apps); do
if ! (is_deployed "$APP"); then
dokku_log_warn "App $APP has not been deployed"
continue
fi
if [[ "$CMD" == "restore" ]]; then
local DOKKU_APP_RESTORE=$(config_get "$APP" DOKKU_APP_RESTORE || true)
if [[ "$DOKKU_APP_RESTORE" == 0 ]]; then
continue
fi
fi
dokku_log_verbose "Running 'dokku ps:${CMD} $APP' ..."
if ps_${CMD} "$APP"; then
continue
fi
dokku_log_warn "dokku ps:${CMD} $APP failed"
done
}

View File

@@ -99,6 +99,7 @@ func Restore(appName string) error {
return fmt.Errorf("Error running pre-restore: %s", err)
}
common.LogInfo1("Clearing potentially invalid proxy configuration")
if err := common.PlugnTrigger("proxy-clear-config", []string{appName}...); err != nil {
return fmt.Errorf("Error clearing proxy config: %s", err)
}
@@ -111,9 +112,11 @@ func Restore(appName string) error {
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_RESTORE"}...)
restore := strings.TrimSpace(string(b[:]))
if restore == "0" {
common.LogWarn(fmt.Sprintf("Skipping ps:restore for %s as DOKKU_APP_RESTORE=%s", appName, restore))
return nil
}
common.LogInfo1("Starting app")
return Start(appName)
}