@@ -32,11 +32,15 @@ type StartupData struct {
32
32
type StartupRecorder struct {
33
33
Data StartupData
34
34
35
- m sync.Mutex
35
+ ServiceCount int
36
+ allLogged chan struct {}
37
+ m sync.Mutex
36
38
}
37
39
38
40
func New () * StartupRecorder {
39
- return & StartupRecorder {}
41
+ return & StartupRecorder {
42
+ allLogged : make (chan struct {}),
43
+ }
40
44
}
41
45
42
46
func (l * StartupRecorder ) ServiceReady (serviceName string , dependencies []string , start time.Time ) {
@@ -56,6 +60,10 @@ func (l *StartupRecorder) ServiceReady(serviceName string, dependencies []string
56
60
l .m .Lock ()
57
61
defer l .m .Unlock ()
58
62
l .Data .Services = append (l .Data .Services , serviceData )
63
+ l .ServiceCount --
64
+ if l .ServiceCount == 0 {
65
+ close (l .allLogged )
66
+ }
59
67
}
60
68
61
69
func (l * StartupRecorder ) MicroshiftStarts (start time.Time ) {
@@ -69,6 +77,8 @@ func (l *StartupRecorder) MicroshiftReady() {
69
77
klog .InfoS ("MICROSHIFT READY" , "since-start" , time .Since (l .Data .Microshift .Start ))
70
78
l .Data .Microshift .Ready = ready
71
79
l .Data .Microshift .TimeToReady = ready .Sub (l .Data .Microshift .Start )
80
+
81
+ l .OutputData ()
72
82
}
73
83
74
84
func (l * StartupRecorder ) ServicesStart (start time.Time ) {
@@ -77,18 +87,25 @@ func (l *StartupRecorder) ServicesStart(start time.Time) {
77
87
}
78
88
79
89
func (l * StartupRecorder ) OutputData () {
80
- jsonOutput , err := json .Marshal (l .Data )
81
- if err != nil {
82
- klog .Error ("Failed to marshal startup data" )
83
- }
84
-
85
- klog .Infof ("Startup data: %s" , string (jsonOutput ))
86
-
87
- path , ok := os .LookupEnv ("STARTUP_LOGS_PATH" )
88
- if ok {
89
- err = os .WriteFile (path , jsonOutput , 0600 )
90
- if err != nil {
91
- klog .Error ("Failed to write startup data to file" )
90
+ go func () {
91
+ select {
92
+ case <- l .allLogged :
93
+ jsonOutput , err := json .Marshal (l .Data )
94
+ if err != nil {
95
+ klog .Error ("Failed to marshal startup data" )
96
+ }
97
+
98
+ klog .Infof ("Startup data: %s" , string (jsonOutput ))
99
+
100
+ path , ok := os .LookupEnv ("STARTUP_LOGS_PATH" )
101
+ if ok {
102
+ err = os .WriteFile (path , jsonOutput , 0600 )
103
+ if err != nil {
104
+ klog .Error ("Failed to write startup data to file" )
105
+ }
106
+ }
107
+ case <- time .After (30 * time .Second ):
108
+ klog .Error ("StartupRecorder timed out" )
92
109
}
93
- }
110
+ }()
94
111
}
0 commit comments