forked from graniticio/granitic
/
suspend.go
115 lines (85 loc) · 3.73 KB
/
suspend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2016 Granitic. All rights reserved.
// Use of this source code is governed by an Apache 2.0 license that can be found in the LICENSE file at the root of this project.
package runtimectl
import (
"errors"
"github.com/graniticio/granitic/ioc"
"github.com/graniticio/granitic/logging"
)
const (
suspendCommandName = "suspend"
suspendSummary = "Suspends one component or all components."
suspendUsage = "suspend [component] [-fw true] [-rc true]"
suspendHelp = "Suspends a component (or all components, if no component name is supplied) that implements the ioc.Suspendable interface."
suspendHelpTwo = "If the '-fw true' argument is supplied when no component name is specified, built-in Granitic framework components will also be suspended (except for the runtime command control server)."
suspendHelpThree = "If the '-rc true' AND '-fw true' arguments are supplied, the runtime command control server will also be suspended and no further runtime control of the application will be possible."
resumeCommandName = "resume"
resumeSummary = "Resumes one component or all components that have previously been suspended."
resumeUsage = "resume [component] [-fw true] [-rc true]"
resumeHelp = "Resumes a component (or all components, if no component name is supplied) that implements the ioc.Suspendable interface."
resumeHelpTwo = "If the '-fw true' argument is supplied when no component name is specified, built-in Granitic framework components will also be resumed."
)
// Create a variant of the lifecycleCommand configured as the suspend command.
func NewSuspendCommand() *lifecycleCommand {
sc := new(lifecycleCommand)
sc.checkFunc = isSuspendable
sc.filterFunc = findSuspendable
sc.invokeFunc = invokeSuspendable
sc.commandHelp = []string{suspendHelp, suspendHelpTwo, suspendHelpThree}
sc.commandName = suspendCommandName
sc.commandSummary = suspendSummary
sc.commandUsage = suspendUsage
sc.outputPrefix = "Suspending"
sc.noneFoundMessage = "No suspendable components found."
return sc
}
// Create a variant of the lifecycleCommand configured as the resume command.
func NewResumeCommand() *lifecycleCommand {
sc := new(lifecycleCommand)
sc.checkFunc = isSuspendable
sc.filterFunc = findSuspendable
sc.invokeFunc = invokeResumable
sc.commandHelp = []string{resumeHelp, resumeHelpTwo}
sc.commandName = resumeCommandName
sc.commandSummary = resumeSummary
sc.commandUsage = resumeUsage
sc.outputPrefix = "Resuming"
sc.noneFoundMessage = "No resumable components found."
return sc
}
func invokeResumable(comps []*ioc.Component, l logging.Logger, cc *ioc.ComponentContainer) {
defer func() {
if r := recover(); r != nil {
l.LogErrorfWithTrace("Panic recovered while resuming components %s", r)
}
}()
if err := cc.Lifecycle.ResumeComponents(comps); err != nil {
l.LogErrorf("Problem resuming components from remote command", err.Error())
}
}
func invokeSuspendable(comps []*ioc.Component, l logging.Logger, cc *ioc.ComponentContainer) {
defer func() {
if r := recover(); r != nil {
l.LogErrorfWithTrace("Panic recovered while suspending components %s", r)
}
}()
if err := cc.Lifecycle.SuspendComponents(comps); err != nil {
l.LogErrorf("Problem suspending components from remote command", err.Error())
}
}
func isSuspendable(i interface{}) (bool, error) {
if _, found := i.(ioc.Suspendable); found {
return found, nil
} else {
return false, errors.New("Component does not implement ioc.Suspendable")
}
}
func findSuspendable(cc *ioc.ComponentContainer, frameworkMode bool, exclude ...string) []*ioc.Component {
var of ownershipFilter
if frameworkMode {
of = All
} else {
of = ApplicationOwned
}
return filteredComponents(cc, ioc.CanSuspend, of, true, exclude...)
}