mirror of
https://github.com/postmannen/ctrl.git
synced 2025-04-09 10:24:17 +00:00
made the text logger more generic
This commit is contained in:
parent
62347e2943
commit
8aa79d2a76
5 changed files with 31 additions and 70 deletions
10
data/ship1.OpCommandRequest.log
Normal file
10
data/ship1.OpCommandRequest.log
Normal file
|
@ -0,0 +1,10 @@
|
|||
* proc - : subscriber, id: 1, name: OpCommandRequest.CommandACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 7, name: CLICommandRequest.EventACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 9, name: CLICommandReply.EventACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 4, name: SayHello.EventNACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 8, name: CLICommandRequestNOSEQ.EventACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : publisher, id: 10, name: SayHello.EventNACK.central, allowed from: map[]
|
||||
* proc - : subscriber, id: 3, name: TextLogging.EventACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 2, name: CLICommand.CommandACK.ship1, allowed from: map[central:{}]
|
||||
* proc - : subscriber, id: 5, name: ECHORequest.EventACK.ship1, allowed from: map[*:{}]
|
||||
* proc - : subscriber, id: 6, name: ECHOReply.EventACK.ship1, allowed from: map[*:{}]
|
1
data/ship1.TextLogging.log
Normal file
1
data/ship1.TextLogging.log
Normal file
|
@ -0,0 +1 @@
|
|||
some message sent from a ship for testing
|
|
@ -29,10 +29,10 @@ type Message struct {
|
|||
// Timeout for long a process should be allowed to operate
|
||||
MethodTimeout int `json:"methodTimeout" yaml:"methodTimeout"`
|
||||
|
||||
// msgOriginal are used for example if a reply message is
|
||||
// generated and we also need a copy of thedetails of the
|
||||
// the initial request message
|
||||
MsgOrigSubject Subject
|
||||
// PreviousMessageSubject are used for example if a reply
|
||||
// message is generated and we also need a copy of thedetails
|
||||
// of the the initial request message
|
||||
PreviousMessageSubject Subject
|
||||
// done is used to signal when a message is fully processed.
|
||||
// This is used when choosing when to move the message from
|
||||
// the ringbuffer into the time series log.
|
||||
|
|
|
@ -29,13 +29,6 @@ func (s *server) ProcessesStart() {
|
|||
go proc.spawnWorker(s)
|
||||
}
|
||||
|
||||
{
|
||||
fmt.Printf("Starting OpCommandReply subscriber: %#v\n", s.nodeName)
|
||||
sub := newSubject(OpCommandReply, CommandACK, s.nodeName)
|
||||
proc := newProcess(s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, []node{"*"}, nil)
|
||||
go proc.spawnWorker(s)
|
||||
}
|
||||
|
||||
// Start a subscriber for CLICommand messages
|
||||
if s.configuration.StartSubCLICommand.OK {
|
||||
{
|
||||
|
|
|
@ -54,8 +54,6 @@ const (
|
|||
OpCommand Method = "OpCommand"
|
||||
// Command for client operation request of the system
|
||||
OpCommandRequest Method = "OpCommandRequest"
|
||||
// Command for client operation reply from a node
|
||||
OpCommandReply Method = "OpCommandReply"
|
||||
// Execute a CLI command in for example bash or cmd.
|
||||
// This is a command type, so the output of the command executed
|
||||
// will directly showed in the ACK message received.
|
||||
|
@ -126,9 +124,6 @@ func (m Method) GetMethodsAvailable() MethodsAvailable {
|
|||
OpCommandRequest: methodOpCommandRequest{
|
||||
commandOrEvent: CommandACK,
|
||||
},
|
||||
OpCommandReply: methodOpCommandReply{
|
||||
commandOrEvent: CommandACK,
|
||||
},
|
||||
CLICommand: methodCLICommand{
|
||||
commandOrEvent: CommandACK,
|
||||
},
|
||||
|
@ -272,7 +267,7 @@ func (m methodOpCommandRequest) handler(proc process, message Message, node stri
|
|||
|
||||
// Prepare and queue for sending a new message with the output
|
||||
// of the action executed.
|
||||
newReplyMessage(proc, message, OpCommandReply, out)
|
||||
newReplyMessage(proc, message, TextLogging, out)
|
||||
}()
|
||||
|
||||
ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n", node, message.ID))
|
||||
|
@ -288,15 +283,12 @@ func newReplyMessage(proc process, message Message, method Method, outData []byt
|
|||
// Create a new message for the reply, and put it on the
|
||||
// ringbuffer to be published.
|
||||
newMsg := Message{
|
||||
ToNode: message.FromNode,
|
||||
Data: []string{string(outData)},
|
||||
Method: method,
|
||||
Timeout: message.RequestTimeout,
|
||||
Retries: message.RequestRetries,
|
||||
MsgOrigSubject: Subject{
|
||||
ToNode: string(message.ToNode),
|
||||
Method: message.Method,
|
||||
},
|
||||
ToNode: message.FromNode,
|
||||
Data: []string{string(outData)},
|
||||
Method: method,
|
||||
Timeout: message.RequestTimeout,
|
||||
Retries: message.RequestRetries,
|
||||
PreviousMessageSubject: proc.subject,
|
||||
}
|
||||
fmt.Printf(" ** %#v\n", newMsg)
|
||||
|
||||
|
@ -311,45 +303,6 @@ func newReplyMessage(proc process, message Message, method Method, outData []byt
|
|||
|
||||
// -----
|
||||
|
||||
type methodOpCommandReply struct {
|
||||
commandOrEvent CommandOrEvent
|
||||
}
|
||||
|
||||
func (m methodOpCommandReply) getKind() CommandOrEvent {
|
||||
return m.commandOrEvent
|
||||
}
|
||||
|
||||
func (m methodOpCommandReply) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
// Recreate the subject structure for the message, so we can use
|
||||
// it in the naming of the files to create.
|
||||
sub := Subject{
|
||||
ToNode: string(message.MsgOrigSubject.ToNode),
|
||||
CommandOrEvent: proc.subject.CommandOrEvent,
|
||||
Method: message.MsgOrigSubject.Method,
|
||||
}
|
||||
|
||||
logFile := filepath.Join(proc.configuration.SubscribersDataFolder, string(sub.name()))
|
||||
f, err := os.OpenFile(logFile, 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
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
type methodCLICommand struct {
|
||||
commandOrEvent CommandOrEvent
|
||||
}
|
||||
|
@ -402,13 +355,17 @@ func (m methodTextLogging) getKind() CommandOrEvent {
|
|||
}
|
||||
|
||||
func (m methodTextLogging) handler(proc process, message Message, node string) ([]byte, error) {
|
||||
sub := Subject{
|
||||
ToNode: string(message.ToNode),
|
||||
CommandOrEvent: proc.subject.CommandOrEvent,
|
||||
Method: message.Method,
|
||||
// Recreate the subject structure for the message, so we can use
|
||||
// it in the naming of the files to create.
|
||||
var fileName string
|
||||
switch {
|
||||
case message.PreviousMessageSubject.ToNode != "":
|
||||
fileName = fmt.Sprintf("%v.%v.log", message.PreviousMessageSubject.ToNode, message.PreviousMessageSubject.Method)
|
||||
case message.PreviousMessageSubject.ToNode == "":
|
||||
fileName = fmt.Sprintf("%v.%v.log", message.FromNode, message.Method)
|
||||
}
|
||||
|
||||
logFile := filepath.Join(proc.configuration.SubscribersDataFolder, string(sub.name())+"-"+string(message.FromNode))
|
||||
logFile := filepath.Join(proc.configuration.SubscribersDataFolder, fileName)
|
||||
f, err := os.OpenFile(logFile, 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)
|
||||
|
|
Loading…
Add table
Reference in a new issue