1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-05 06:46:48 +00:00

moved canceling of context, and comments

This commit is contained in:
postmannen 2021-03-29 06:53:34 +02:00
parent 03da2e18e7
commit 0d822b0751
2 changed files with 29 additions and 15 deletions

View file

@ -2,10 +2,10 @@
{ {
"toNode": "ship1", "toNode": "ship1",
"data": ["bash","-c","sleep 5 & echo 'apekatt'"], "data": ["bash","-c","sleep 3 & echo 'apekatt'"],
"method":"CLICommandRequest", "method":"CLICommandRequest",
"timeout":10, "timeout":10,
"retries":3, "retries":3,
"MethodTimeout": 3 "MethodTimeout": 7
} }
] ]

View file

@ -197,6 +197,9 @@ func (m methodCLICommand) getKind() CommandOrEvent {
return m.commandOrEvent return m.commandOrEvent
} }
// handler to run a CLI command with timeout context. The handler will
// return the output of the command run back to the calling publisher
// in the ack message.
func (m methodCLICommand) handler(proc process, message Message, node string) ([]byte, error) { func (m methodCLICommand) handler(proc process, message Message, node string) ([]byte, error) {
out := []byte{} out := []byte{}
@ -204,7 +207,6 @@ func (m methodCLICommand) handler(proc process, message Message, node string) ([
a := message.Data[1:] a := message.Data[1:]
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout))
defer cancel()
outCh := make(chan []byte) outCh := make(chan []byte)
@ -219,11 +221,11 @@ func (m methodCLICommand) handler(proc process, message Message, node string) ([
select { select {
case <-ctx.Done(): case <-ctx.Done():
fmt.Printf(" ** Before\n") cancel()
er := fmt.Errorf("error: method timed out %v", proc) er := fmt.Errorf("error: method timed out %v", message)
sendErrorLogMessage(proc.newMessagesCh, proc.node, er) sendErrorLogMessage(proc.newMessagesCh, proc.node, er)
fmt.Printf(" ** After\n")
case out = <-outCh: case out = <-outCh:
cancel()
} }
ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s---", node, message.ID, out)) ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s---", node, message.ID, out))
@ -365,15 +367,21 @@ func (m methodCLICommandRequest) getKind() CommandOrEvent {
return m.commandOrEvent return m.commandOrEvent
} }
// handler to run a CLI command with timeout context. The handler will
// return the output of the command run back to the calling publisher
// as a new message.
func (m methodCLICommandRequest) handler(proc process, message Message, node string) ([]byte, error) { func (m methodCLICommandRequest) handler(proc process, message Message, node string) ([]byte, error) {
log.Printf("<--- CLICommand REQUEST received from: %v, containing: %v", message.FromNode, message.Data) log.Printf("<--- CLICommandREQUEST received from: %v, containing: %v", message.FromNode, message.Data)
// Execute the CLI command in it's own go routine, so we are able
// to return immediately with an ack reply that the messag was
// received, and we create a new message to send back to the calling
// node for the out put of the actual command.
go func() { go func() {
c := message.Data[0] c := message.Data[0]
a := message.Data[1:] a := message.Data[1:]
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout))
defer cancel()
outCh := make(chan []byte) outCh := make(chan []byte)
@ -388,11 +396,11 @@ func (m methodCLICommandRequest) handler(proc process, message Message, node str
select { select {
case <-ctx.Done(): case <-ctx.Done():
fmt.Printf(" ** Before\n") cancel()
er := fmt.Errorf("error: method timed out %v", proc) er := fmt.Errorf("error: method timed out %v", message)
sendErrorLogMessage(proc.newMessagesCh, proc.node, er) sendErrorLogMessage(proc.newMessagesCh, proc.node, er)
fmt.Printf(" ** After\n")
case out := <-outCh: case out := <-outCh:
cancel()
// Create a new message for the reply, and put it on the // Create a new message for the reply, and put it on the
// ringbuffer to be published. // ringbuffer to be published.
@ -429,18 +437,24 @@ func (m methodCLICommandRequestNOSEQ) getKind() CommandOrEvent {
return m.commandOrEvent return m.commandOrEvent
} }
// handler to run a CLI command with timeout context. The handler will
// return the output of the command run back to the calling publisher
// as a new message.
// The NOSEQ method will process messages as they are recived, // The NOSEQ method will process messages as they are recived,
// and the reply back will be sent as soon as the process is // and the reply back will be sent as soon as the process is
// done. No order are preserved. // done. No order are preserved.
func (m methodCLICommandRequestNOSEQ) handler(proc process, message Message, node string) ([]byte, error) { func (m methodCLICommandRequestNOSEQ) handler(proc process, message Message, node string) ([]byte, error) {
log.Printf("<--- CLICommand REQUEST received from: %v, containing: %v", message.FromNode, message.Data) log.Printf("<--- CLICommand REQUEST received from: %v, containing: %v", message.FromNode, message.Data)
// Execute the CLI command in it's own go routine, so we are able
// to return immediately with an ack reply that the messag was
// received, and we create a new message to send back to the calling
// node for the out put of the actual command.
go func() { go func() {
c := message.Data[0] c := message.Data[0]
a := message.Data[1:] a := message.Data[1:]
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout)) ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout))
defer cancel()
outCh := make(chan []byte) outCh := make(chan []byte)
@ -455,11 +469,11 @@ func (m methodCLICommandRequestNOSEQ) handler(proc process, message Message, nod
select { select {
case <-ctx.Done(): case <-ctx.Done():
fmt.Printf(" ** Before\n") cancel()
er := fmt.Errorf("error: method timed out %v", proc) er := fmt.Errorf("error: method timed out %v", message)
sendErrorLogMessage(proc.newMessagesCh, proc.node, er) sendErrorLogMessage(proc.newMessagesCh, proc.node, er)
fmt.Printf(" ** After\n")
case out := <-outCh: case out := <-outCh:
cancel()
// Create a new message for the reply, and put it on the // Create a new message for the reply, and put it on the
// ringbuffer to be published. // ringbuffer to be published.