Skip to content

Commit 29b8662

Browse files
committed
add persisting meta info
1 parent 53596f5 commit 29b8662

9 files changed

Lines changed: 75 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
training-application
22
.DS_Store
33
application.log
4+
/data/

helm-chart/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ data:
99
message: {{ .Values.message }}
1010
color: {{ .Values.color }}
1111
catMode: {{ .Values.catMode }}
12+
persistMetaInfo: {{ .Values.persistMetaInfo }}

helm-chart/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ image:
66
color: white
77
message: "Hello from the app installed via helm"
88
catMode: true
9+
persistMetaInfo: false
910

1011
deployment:
1112
replicas: 1

src/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type appConfig struct {
2828
applicationMessage string
2929
color string
3030
logToFileOnly bool
31+
persistMetaInfo bool
3132
catImageUrl string
3233
}
3334

@@ -47,6 +48,7 @@ func (appConfig *appConfig) String() string {
4748
sb.WriteString(fmt.Sprintf("\tApplication message: %s\n", appConfig.applicationMessage))
4849
sb.WriteString(fmt.Sprintf("\tcolor: %s\n", appConfig.color))
4950
sb.WriteString(fmt.Sprintf("\tlogToFileOnly: %v\n", appConfig.logToFileOnly))
51+
sb.WriteString(fmt.Sprintf("\tpersistMetaInfo: %v\n", appConfig.persistMetaInfo))
5052
sb.WriteString(fmt.Sprintf("\tcatImageUrl: %s\n", appConfig.catImageUrl))
5153
return sb.String()
5254
}
@@ -83,6 +85,7 @@ func (appConfig *appConfig) initAppConfig(isReady bool) {
8385
appConfig.applicationMessage = getAppConfigStringValue(fileConfig, "message", "APP_MESSAGE", "not set")
8486
appConfig.color = getAppConfigStringValue(fileConfig, "color", "APP_COLOR", "not set")
8587
appConfig.logToFileOnly = getAppConfigBoolValue(fileConfig, "logToFileOnly", "", false)
88+
appConfig.persistMetaInfo = getAppConfigBoolValue(fileConfig, "persistMetaInfo", "", false)
8689
appConfig.startUpDelaySeconds = getAppConfigIntValue(fileConfig, "startUpDelaySeconds", "", 0)
8790
appConfig.tearDownDelaySeconds = getAppConfigIntValue(fileConfig, "tearDownDelaySeconds", "", 0)
8891
catMode := getAppConfigBoolValue(fileConfig, "catMode", "", false)

src/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ func main() {
5555

5656
server := newServer(config)
5757

58+
if !config.persistMetaInfo {
59+
log.Info("Application does not persist meta info")
60+
} else {
61+
persister, err := newPersister(config)
62+
if err != nil {
63+
log.Errorf("error on starting persistence %v", err)
64+
} else {
65+
go persister.writeMetaInfo()
66+
}
67+
}
68+
5869
config.ready = true
5970
log.Info("Application set to ready")
6071
log.Info("For getting help, type 'help'")

src/persister.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
log "github.com/sirupsen/logrus"
9+
)
10+
11+
type persister struct {
12+
config *appConfig
13+
}
14+
15+
var dirPath = "./data/"
16+
var metaInfoFilePath = dirPath + "metainfo.txt"
17+
18+
func newPersister(appConfig *appConfig) (*persister, error) {
19+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
20+
return nil, err
21+
}
22+
return &persister{
23+
config: appConfig,
24+
}, nil
25+
}
26+
27+
func (p *persister) writeMetaInfo() {
28+
29+
metaInfoFile, err := os.OpenFile(metaInfoFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
30+
if err != nil {
31+
log.Errorf("cannot open file %s: %v\n", metaInfoFilePath, err)
32+
}
33+
defer metaInfoFile.Close()
34+
35+
ticker := time.NewTicker(5 * time.Second)
36+
defer ticker.Stop()
37+
38+
// TODO write application startup and shutdown into metainfo file
39+
40+
for {
41+
select {
42+
case <-ticker.C:
43+
podName := os.Getenv("POD_NAME")
44+
podIP := os.Getenv("POD_IP")
45+
timeStamp := time.Now().Format("2006-01-02 15:04:05")
46+
_, err = fmt.Fprintf(metaInfoFile, "%s pod name %s, pod ip %s\n", timeStamp, podName, podIP)
47+
if err != nil {
48+
log.Errorf("cannot append to file %s: %v\n", metaInfoFilePath, err)
49+
return
50+
}
51+
}
52+
}
53+
}

src/root.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ <h2>Configuration</h2>
1818
Seconds the application needs to start up: {{.StartUpDelaySeconds}}<br>
1919
Seconds the application needs to shut down gracefully: {{.TearDownDelaySeconds}}<br>
2020
Only log to file: {{.LogToFileOnly}}<br>
21+
Persist Meta Info: {{.PersistMetaInfo}}<br>
2122

2223
<h2>Tech Details</h2>
2324

src/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type TemplateData struct {
3434
TearDownDelaySeconds int
3535
RequestInfo *requestInfo
3636
LogToFileOnly bool
37+
PersistMetaInfo bool
3738
ProcessId int
3839
UserId int
3940
Hostname string
@@ -110,6 +111,7 @@ func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
110111
StartUpDelaySeconds: s.config.startUpDelaySeconds,
111112
TearDownDelaySeconds: s.config.tearDownDelaySeconds,
112113
LogToFileOnly: s.config.logToFileOnly,
114+
PersistMetaInfo: s.config.persistMetaInfo,
113115
ProcessId: os.Getpid(),
114116
UserId: os.Getuid(),
115117
RequestInfo: requestInfo,
@@ -151,4 +153,4 @@ func (s *server) handleReadiness(w http.ResponseWriter, r *http.Request) {
151153
w.WriteHeader(http.StatusServiceUnavailable)
152154
log.Info("Readiness endpoint ('/readiness') responded with Status Code 503 Service Unavailable")
153155
}
154-
}
156+
}

training-application.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = v0
44
message = Message from default training-application.conf
55
color = lightGrey
66
logToFileOnly = false
7+
persistMetaInfo = false
78
catMode = false
89
startUpDelaySeconds = 0
910
tearDownDelaySeconds = 0

0 commit comments

Comments
 (0)