8
8
"pmon3/pmond/model"
9
9
"pmon3/pmond/process"
10
10
"pmon3/pmond/protos"
11
- "slices"
12
11
"strings"
13
12
"time"
14
13
)
@@ -20,96 +19,130 @@ func Initialize(cmd *protos.Cmd) *protos.CmdResp {
20
19
Name : cmd .GetName (),
21
20
}
22
21
22
+ blocking := cmd .GetArg2 () == "blocking"
23
+
24
+ var err error
25
+
23
26
if cmd .GetArg1 () == "apps-config-only" {
24
- startedFromConfig := StartsAppsFromConfig ()
25
- if ! startedFromConfig {
26
- newCmdResp .Error = "no applications were started"
27
- }
28
- return & newCmdResp
27
+ err = StartsAppsFromConfig (blocking )
29
28
} else {
30
- if err := StartApps (); err != nil {
31
- newCmdResp .Error = err .Error ()
32
- }
33
-
34
- return & newCmdResp
29
+ err = StartAppsFromBoth (blocking )
30
+ }
31
+ if err != nil {
32
+ newCmdResp .Error = err .Error ()
35
33
}
34
+
35
+ return & newCmdResp
36
36
}
37
37
38
- func StartsAppsFromConfig () bool {
38
+ func StartsAppsFromConfig (blocking bool ) error {
39
39
40
40
if pmond .Config .AppsConfig == nil || len (pmond .Config .AppsConfig .Apps ) == 0 {
41
- return false
41
+ return nil
42
42
}
43
43
44
44
nonDependentApps , dependentApps , err := conf .ComputeDepGraph (pmond .Config .AppsConfig .Apps )
45
45
if err != nil {
46
- return false
46
+ return err
47
47
}
48
48
49
- for _ , app := range dependentApps {
50
- err := EnqueueProcess (app .File , & app .Flags )
51
- time .Sleep (pmond .Config .GetDependentProcessEnqueuedWait ())
52
- if err != nil {
53
- pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
54
- }
49
+ if blocking {
50
+ err = appConfAppEnqueueUsingDepGraphResults (nonDependentApps , dependentApps )
51
+ } else {
52
+ go appConfAppEnqueueUsingDepGraphResults (nonDependentApps , dependentApps )
55
53
}
56
54
57
- for _ , app := range nonDependentApps {
58
- err := EnqueueProcess (app .File , & app .Flags )
59
- if err != nil {
60
- pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
61
- }
55
+ return err
56
+ }
57
+
58
+ func StartAppsFromBoth (blocking bool ) error {
59
+ nonDependentApps , dependentApps , err := getQueueableFromBoth ()
60
+ if err != nil {
61
+ return err
62
62
}
63
63
64
- return true
64
+ if blocking {
65
+ err = processEnqueueUsingDepGraphResults (nonDependentApps , dependentApps )
66
+ } else {
67
+ go processEnqueueUsingDepGraphResults (nonDependentApps , dependentApps )
68
+ }
69
+
70
+ return err
65
71
}
66
72
67
- func StartApps () error {
73
+ func getQueueableFromBoth () ( * []model. Process , * []model. Process , error ) {
68
74
var all []model.Process
69
75
err := db .Db ().Find (& all ).Error
70
76
if err != nil {
71
- return err
77
+ return nil , nil , err
72
78
}
73
79
74
80
var qPs []model.Process
75
81
qNm := map [string ]bool {}
76
82
77
- dbProcessNames := model .ProcessNames (& all )
78
-
79
83
for _ , appConfigApp := range pmond .Config .AppsConfig .Apps {
80
-
81
84
processName := appConfigApp .Flags .Name
82
-
83
- if slices .Contains (dbProcessNames , processName ) {
84
- appLog , _ := getAppsConfigAppLogPath (& appConfigApp )
85
- appUser , _ := getAppsConfigAppUser (& appConfigApp )
86
- p := model .FromFileAndExecFlags (appConfigApp .File , & appConfigApp .Flags , appLog , appUser )
87
- qPs = append (qPs , * p )
88
- pmond .Log .Infof ("overwrite with conf: pushing to stack %s" , processName )
89
- qNm [processName ] = true
90
- } else {
91
- dbPs , _ := model .GetProcessByName (processName , & all )
92
- if dbPs != nil {
93
- qPs = append (qPs , * dbPs )
94
- qNm [processName ] = true
95
- pmond .Log .Infof ("append from db: pushing to stack %s" , processName )
96
- }
97
- }
85
+ appLog , _ := getAppsConfigAppLogPath (& appConfigApp )
86
+ appUser , _ := getAppsConfigAppUser (& appConfigApp )
87
+ p := model .FromFileAndExecFlags (appConfigApp .File , & appConfigApp .Flags , appLog , appUser )
88
+ qPs = append (qPs , * p )
89
+ qNm [processName ] = true
98
90
}
99
91
100
92
for _ , dbPs := range all {
101
- if ! qNm [dbPs .Name ] {
93
+ processName := dbPs .Name
94
+ if ! qNm [processName ] {
102
95
qPs = append (qPs , dbPs )
103
- pmond .Log .Infof ("append reamainder from db: pushing to stack %s" , dbPs .Name )
96
+ pmond .Log .Infof ("append reamainder from db: pushing to stack %s" , processName )
97
+ } else {
98
+ pmond .Log .Infof ("overwritten with apps conf: %s" , processName )
104
99
}
105
100
}
106
101
107
102
nonDependentApps , dependentApps , err := model .ComputeDepGraph (& qPs )
108
103
if err != nil {
109
104
pmond .Log .Errorf ("encountered error attempting to prioritize databse processes from dep graph: %s" , err )
110
- return err
105
+ return nil , nil , err
106
+ }
107
+
108
+ return nonDependentApps , dependentApps , nil
109
+ }
110
+
111
+ func appConfAppEnqueueUsingDepGraphResults (nonDependentApps * []conf.AppsConfigApp , dependentApps * []conf.AppsConfigApp ) error {
112
+
113
+ var retErr error
114
+
115
+ if dependentApps != nil {
116
+ for _ , app := range * dependentApps {
117
+ pmond .Log .Infof ("launch dependent %s" , strings .Join (conf .AppNames (dependentApps ), " " ))
118
+ err := EnqueueProcess (app .File , & app .Flags )
119
+ time .Sleep (pmond .Config .GetDependentProcessEnqueuedWait ())
120
+ if err != nil {
121
+ pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
122
+ retErr = err
123
+ }
124
+ }
111
125
}
112
126
127
+ if nonDependentApps != nil {
128
+ pmond .Log .Infof ("launch independent %s" , strings .Join (conf .AppNames (nonDependentApps ), " " ))
129
+
130
+ for _ , app := range * nonDependentApps {
131
+ err := EnqueueProcess (app .File , & app .Flags )
132
+ if err != nil {
133
+ pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
134
+ retErr = err
135
+ }
136
+ }
137
+ }
138
+
139
+ return retErr
140
+ }
141
+
142
+ func processEnqueueUsingDepGraphResults (nonDependentApps * []model.Process , dependentApps * []model.Process ) error {
143
+
144
+ var retErr error
145
+
113
146
if dependentApps != nil {
114
147
pmond .Log .Infof ("launch dependent %s" , strings .Join (model .ProcessNames (dependentApps ), " " ))
115
148
@@ -119,7 +152,7 @@ func StartApps() error {
119
152
time .Sleep (pmond .Config .GetDependentProcessEnqueuedWait ())
120
153
if err != nil {
121
154
pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
122
- return err
155
+ retErr = err
123
156
}
124
157
}
125
158
}
@@ -132,12 +165,12 @@ func StartApps() error {
132
165
err := process .Enqueue (& app , true )
133
166
if err != nil {
134
167
pmond .Log .Errorf ("encountered error attempting to enqueue process: %s" , err )
135
- return err
168
+ retErr = err
136
169
}
137
170
}
138
171
}
139
172
140
- return nil
173
+ return retErr
141
174
}
142
175
143
176
func getAppsConfigAppLogPath (app * conf.AppsConfigApp ) (string , error ) {
0 commit comments