mirror of
https://github.com/postmannen/ctrl.git
synced 2025-03-05 14:56:49 +00:00
errorLog handler writing to file structure
This commit is contained in:
parent
27fc56256b
commit
2c2f53f6f4
6 changed files with 57 additions and 9 deletions
|
@ -132,7 +132,7 @@ func newConfigurationDefaults() Configuration {
|
||||||
DefaultMessageTimeout: 10,
|
DefaultMessageTimeout: 10,
|
||||||
DefaultMessageRetries: 1,
|
DefaultMessageRetries: 1,
|
||||||
StartPubREQHello: 30,
|
StartPubREQHello: 30,
|
||||||
SubscribersDataFolder: "./data",
|
SubscribersDataFolder: "./var",
|
||||||
CentralNodeName: "",
|
CentralNodeName: "",
|
||||||
StartSubREQErrorLog: flagNodeSlice{Values: []node{}},
|
StartSubREQErrorLog: flagNodeSlice{Values: []node{}},
|
||||||
StartSubREQHello: flagNodeSlice{OK: true, Values: []node{"*"}},
|
StartSubREQHello: flagNodeSlice{OK: true, Values: []node{"*"}},
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
2021-04-06 04:45:05.440035 +0000 UTC, ping reply sent from central
|
|
||||||
2021-04-06 04:45:13.980909 +0000 UTC, ping reply sent from central
|
|
|
@ -7,7 +7,7 @@ NodeName = "central"
|
||||||
ProfilingPort = ""
|
ProfilingPort = ""
|
||||||
PromHostAndPort = ":2112"
|
PromHostAndPort = ":2112"
|
||||||
StartPubREQHello = 0
|
StartPubREQHello = 0
|
||||||
SubscribersDataFolder = "./data"
|
SubscribersDataFolder = "./var"
|
||||||
|
|
||||||
[StartSubREQCliCommand]
|
[StartSubREQCliCommand]
|
||||||
OK = true
|
OK = true
|
||||||
|
|
|
@ -6,6 +6,6 @@
|
||||||
"method":"REQCliCommand",
|
"method":"REQCliCommand",
|
||||||
"timeout":10,
|
"timeout":10,
|
||||||
"retries":3,
|
"retries":3,
|
||||||
"methodTimeout": 2
|
"methodTimeout": 4
|
||||||
}
|
}
|
||||||
]
|
]
|
|
@ -175,13 +175,16 @@ func sendErrorLogMessage(newMessagesCh chan<- []subjectAndMessage, FromNode node
|
||||||
// createErrorMsgContent will prepare a subject and message with the content
|
// createErrorMsgContent will prepare a subject and message with the content
|
||||||
// of the error
|
// of the error
|
||||||
func createErrorMsgContent(FromNode node, theError error) subjectAndMessage {
|
func createErrorMsgContent(FromNode node, theError error) subjectAndMessage {
|
||||||
fmt.Printf(" --- Sending error message to central !!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
|
// Add time stamp
|
||||||
|
er := fmt.Sprintf("%v, %v\n", time.Now().UTC(), theError.Error())
|
||||||
|
|
||||||
sam := subjectAndMessage{
|
sam := subjectAndMessage{
|
||||||
Subject: newSubject(REQErrorLog, "errorCentral"),
|
Subject: newSubject(REQErrorLog, "errorCentral"),
|
||||||
Message: Message{
|
Message: Message{
|
||||||
|
Label: "errorLog",
|
||||||
ToNode: "errorCentral",
|
ToNode: "errorCentral",
|
||||||
FromNode: FromNode,
|
FromNode: FromNode,
|
||||||
Data: []string{theError.Error()},
|
Data: []string{er},
|
||||||
Method: REQErrorLog,
|
Method: REQErrorLog,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -354,9 +354,56 @@ func (m methodREQErrorLog) handler(proc process, message Message, node string) (
|
||||||
|
|
||||||
// --
|
// --
|
||||||
|
|
||||||
// --
|
// If it was a request type message we want to check what the initial messages
|
||||||
|
// method, so we can use that in creating the file name to store the data.
|
||||||
|
fmt.Printf(" ** DEBUG: %v\n", message.PreviousMessage)
|
||||||
|
var fileName string
|
||||||
|
var folderTree string
|
||||||
|
switch {
|
||||||
|
case message.PreviousMessage == nil:
|
||||||
|
// If this was a direct request there are no previous message to take
|
||||||
|
// information from, so we use the one that are in the current mesage.
|
||||||
|
fileName = fmt.Sprintf("%v.%v.log", message.ToNode, message.Method)
|
||||||
|
folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.Label, string(message.FromNode))
|
||||||
|
case message.PreviousMessage.ToNode != "":
|
||||||
|
fileName = fmt.Sprintf("%v.%v.log", message.PreviousMessage.ToNode, message.PreviousMessage.Method)
|
||||||
|
folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Label, string(message.PreviousMessage.ToNode))
|
||||||
|
case message.PreviousMessage.ToNode == "":
|
||||||
|
fileName = fmt.Sprintf("%v.%v.log", message.FromNode, message.Method)
|
||||||
|
folderTree = filepath.Join(proc.configuration.SubscribersDataFolder, message.PreviousMessage.Label, string(message.PreviousMessage.ToNode))
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
// Check if folder structure exist, if not create it.
|
||||||
|
if _, err := os.Stat(folderTree); os.IsNotExist(err) {
|
||||||
|
err := os.MkdirAll(folderTree, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error: failed to create directory %v: %v", folderTree, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("info: Creating subscribers data folder at %v\n", folderTree)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open file and write data.
|
||||||
|
file := filepath.Join(folderTree, fileName)
|
||||||
|
f, err := os.OpenFile(file, os.O_APPEND|os.O_RDWR|os.O_CREATE, os.ModeAppend)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: methodEventTextLogging.handler: failed to open file: %v\n", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
for _, d := range message.Data {
|
||||||
|
_, err := f.Write([]byte(d))
|
||||||
|
f.Sync()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error: methodEventTextLogging.handler: failed to write to file: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
|
||||||
|
return ackMsg, nil
|
||||||
|
|
||||||
|
// --
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---
|
// ---
|
||||||
|
|
Loading…
Add table
Reference in a new issue