Merge pull request #4262 from dokku/4259-better-restore-logging
Improve ps:restore logging
This commit is contained in:
@@ -6,25 +6,20 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParallelCommand is a type that declares functions
|
// parallelCommand is a type that declares functions
|
||||||
// the ps plugins can execute in parallel
|
// the ps plugins can execute in parallel
|
||||||
type ParallelCommand func(string) error
|
type parallelCommand func(string) error
|
||||||
|
|
||||||
// ParallelCommandRun contains all the arguments
|
// parallelCommandResult is the result of a parallel
|
||||||
// necessary for a parallel command run
|
|
||||||
type ParallelCommandRun struct {
|
|
||||||
Name string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParallelCommandResult is the result of a parallel
|
|
||||||
// command run
|
// command run
|
||||||
type ParallelCommandResult struct {
|
type parallelCommandResult struct {
|
||||||
Name string
|
Name string
|
||||||
Error error
|
CommandName string
|
||||||
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCommandAgainstAllApps runs a given ParallelCommand against all apps
|
// RunCommandAgainstAllApps runs a given parallelCommand against all apps
|
||||||
func RunCommandAgainstAllApps(command ParallelCommand, commandName string, parallelCount int) error {
|
func RunCommandAgainstAllApps(command parallelCommand, commandName string, parallelCount int) error {
|
||||||
runInSerial := false
|
runInSerial := false
|
||||||
|
|
||||||
if parallelCount < -1 {
|
if parallelCount < -1 {
|
||||||
@@ -49,8 +44,8 @@ func RunCommandAgainstAllApps(command ParallelCommand, commandName string, paral
|
|||||||
return RunCommandAgainstAllAppsInParallel(command, commandName, parallelCount)
|
return RunCommandAgainstAllAppsInParallel(command, commandName, parallelCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCommandAgainstAllAppsInParallel runs a given ParallelCommand against all apps in parallel
|
// RunCommandAgainstAllAppsInParallel runs a given parallelCommand against all apps in parallel
|
||||||
func RunCommandAgainstAllAppsInParallel(command ParallelCommand, commandName string, parallelCount int) error {
|
func RunCommandAgainstAllAppsInParallel(command parallelCommand, commandName string, parallelCount int) error {
|
||||||
apps, err := DokkuApps()
|
apps, err := DokkuApps()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogWarn(err.Error())
|
LogWarn(err.Error())
|
||||||
@@ -58,19 +53,19 @@ func RunCommandAgainstAllAppsInParallel(command ParallelCommand, commandName str
|
|||||||
}
|
}
|
||||||
|
|
||||||
jobs := make(chan string, parallelCount)
|
jobs := make(chan string, parallelCount)
|
||||||
results := make(chan ParallelCommandResult, len(apps))
|
results := make(chan parallelCommandResult, len(apps))
|
||||||
|
|
||||||
go allocateJobs(apps, jobs)
|
go allocateJobs(apps, jobs)
|
||||||
done := make(chan error)
|
done := make(chan error)
|
||||||
go aggregateResults(results, done)
|
go aggregateResults(results, done)
|
||||||
createParallelWorkerPool(jobs, results, command, parallelCount)
|
createParallelWorkerPool(jobs, results, command, commandName, parallelCount)
|
||||||
err = <-done
|
err = <-done
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunCommandAgainstAllAppsSerially runs a given ParallelCommand against all apps serially
|
// RunCommandAgainstAllAppsSerially runs a given parallelCommand against all apps serially
|
||||||
func RunCommandAgainstAllAppsSerially(command ParallelCommand, commandName string) error {
|
func RunCommandAgainstAllAppsSerially(command parallelCommand, commandName string) error {
|
||||||
apps, err := DokkuApps()
|
apps, err := DokkuApps()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogWarn(err.Error())
|
LogWarn(err.Error())
|
||||||
@@ -81,6 +76,7 @@ func RunCommandAgainstAllAppsSerially(command ParallelCommand, commandName strin
|
|||||||
for _, appName := range apps {
|
for _, appName := range apps {
|
||||||
LogInfo1(fmt.Sprintf("Running %s against app %s", commandName, appName))
|
LogInfo1(fmt.Sprintf("Running %s against app %s", commandName, appName))
|
||||||
if err = command(appName); err != nil {
|
if err = command(appName); err != nil {
|
||||||
|
LogWarn(fmt.Sprintf("Error running %s against app %s: %s", commandName, appName, err.Error()))
|
||||||
errorCount++
|
errorCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -99,12 +95,12 @@ func allocateJobs(input []string, jobs chan string) {
|
|||||||
close(jobs)
|
close(jobs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func aggregateResults(results chan ParallelCommandResult, done chan error) {
|
func aggregateResults(results chan parallelCommandResult, done chan error) {
|
||||||
var parallelError error
|
var parallelError error
|
||||||
errorCount := 0
|
errorCount := 0
|
||||||
for result := range results {
|
for result := range results {
|
||||||
if result.Error != nil {
|
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++
|
errorCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,23 +110,24 @@ func aggregateResults(results chan ParallelCommandResult, done chan error) {
|
|||||||
done <- parallelError
|
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 {
|
for job := range jobs {
|
||||||
LogInfo1(fmt.Sprintf("Running command against %s", job))
|
LogInfo1(fmt.Sprintf("Running command against %s", job))
|
||||||
output := ParallelCommandResult{
|
output := parallelCommandResult{
|
||||||
Name: job,
|
Name: job,
|
||||||
Error: command(job),
|
CommandName: commandName,
|
||||||
|
Error: command(job),
|
||||||
}
|
}
|
||||||
results <- output
|
results <- output
|
||||||
}
|
}
|
||||||
wg.Done()
|
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
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < numberOfWorkers; i++ {
|
for i := 0; i < numberOfWorkers; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go createParallelWorker(jobs, results, command, &wg, i)
|
go createParallelWorker(jobs, results, command, commandName, &wg, i)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(results)
|
close(results)
|
||||||
|
|||||||
@@ -168,5 +168,5 @@ nginx_clear_config() {
|
|||||||
declare desc="Remove the nginx conf file"
|
declare desc="Remove the nginx conf file"
|
||||||
declare APP="$1"
|
declare APP="$1"
|
||||||
rm -f "$DOKKU_ROOT/$APP/nginx.conf"
|
rm -f "$DOKKU_ROOT/$APP/nginx.conf"
|
||||||
fn-nginx-vhosts-nginx-init-cmd "reload"
|
suppress_output fn-nginx-vhosts-nginx-init-cmd "reload"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
@@ -99,6 +99,7 @@ func Restore(appName string) error {
|
|||||||
return fmt.Errorf("Error running pre-restore: %s", err)
|
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 {
|
if err := common.PlugnTrigger("proxy-clear-config", []string{appName}...); err != nil {
|
||||||
return fmt.Errorf("Error clearing proxy config: %s", err)
|
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"}...)
|
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_RESTORE"}...)
|
||||||
restore := strings.TrimSpace(string(b[:]))
|
restore := strings.TrimSpace(string(b[:]))
|
||||||
if restore == "0" {
|
if restore == "0" {
|
||||||
|
common.LogWarn(fmt.Sprintf("Skipping ps:restore for %s as DOKKU_APP_RESTORE=%s", appName, restore))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
common.LogInfo1("Starting app")
|
||||||
return Start(appName)
|
return Start(appName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user