mirror of
https://github.com/postmannen/ctrl.git
synced 2025-03-05 14:56:49 +00:00
restructured function logic for sending messages
This commit is contained in:
parent
7b230f1fc6
commit
130f9fc57e
1 changed files with 136 additions and 135 deletions
75
process.go
75
process.go
|
@ -589,20 +589,22 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
// to jump back here to the beginning of the loop and continue
|
// to jump back here to the beginning of the loop and continue
|
||||||
// with the next message.
|
// with the next message.
|
||||||
for {
|
for {
|
||||||
var err error
|
|
||||||
var m Message
|
|
||||||
|
|
||||||
// Wait and read the next message on the message channel, or
|
// Wait and read the next message on the message channel, or
|
||||||
// exit this function if Cancel are received via ctx.
|
// exit this function if Cancel are received via ctx.
|
||||||
select {
|
select {
|
||||||
case m = <-p.subject.messageCh:
|
case m := <-p.subject.messageCh:
|
||||||
|
p.publishAMessage(m, zEnc, once, natsConn)
|
||||||
case <-p.ctx.Done():
|
case <-p.ctx.Done():
|
||||||
er := fmt.Errorf("info: canceling publisher: %v", p.subject.name())
|
er := fmt.Errorf("info: canceling publisher: %v", p.subject.name())
|
||||||
//sendErrorLogMessage(p.toRingbufferCh, Node(p.node), er)
|
//sendErrorLogMessage(p.toRingbufferCh, Node(p.node), er)
|
||||||
log.Printf("%v\n", er)
|
log.Printf("%v\n", er)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p process) publishAMessage(m Message, zEnc *zstd.Encoder, once sync.Once, natsConn *nats.Conn) {
|
||||||
// Create the initial header, and set values below depending on the
|
// Create the initial header, and set values below depending on the
|
||||||
// various configuration options chosen.
|
// various configuration options chosen.
|
||||||
natsMsgHeader := nats.Header{}
|
natsMsgHeader := nats.Header{}
|
||||||
|
@ -619,7 +621,7 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er := fmt.Errorf("error: messageDeliverNats: cbor encode message failed: %v", err)
|
er := fmt.Errorf("error: messageDeliverNats: cbor encode message failed: %v", err)
|
||||||
p.processes.errorKernel.errSend(p, m, er)
|
p.processes.errorKernel.errSend(p, m, er)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
natsMsgPayloadSerialized = b
|
natsMsgPayloadSerialized = b
|
||||||
|
@ -628,11 +630,11 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
default:
|
default:
|
||||||
var bufGob bytes.Buffer
|
var bufGob bytes.Buffer
|
||||||
gobEnc := gob.NewEncoder(&bufGob)
|
gobEnc := gob.NewEncoder(&bufGob)
|
||||||
err = gobEnc.Encode(m)
|
err := gobEnc.Encode(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
er := fmt.Errorf("error: messageDeliverNats: gob encode message failed: %v", err)
|
er := fmt.Errorf("error: messageDeliverNats: gob encode message failed: %v", err)
|
||||||
p.processes.errorKernel.errSend(p, m, er)
|
p.processes.errorKernel.errSend(p, m, er)
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
natsMsgPayloadSerialized = bufGob.Bytes()
|
natsMsgPayloadSerialized = bufGob.Bytes()
|
||||||
|
@ -666,7 +668,7 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error: failed to write gzip: %v\n", err)
|
log.Printf("error: failed to write gzip: %v\n", err)
|
||||||
gzipW.Close()
|
gzipW.Close()
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
gzipW.Close()
|
gzipW.Close()
|
||||||
|
|
||||||
|
@ -677,7 +679,7 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
|
|
||||||
default: // no compression
|
default: // no compression
|
||||||
// Allways log the error to console.
|
// Allways log the error to console.
|
||||||
er := fmt.Errorf("error: compression type not defined: %v, setting default to zero compression", err)
|
er := fmt.Errorf("error: compression type not defined, setting default to zero compression")
|
||||||
log.Printf("%v\n", er)
|
log.Printf("%v\n", er)
|
||||||
|
|
||||||
// We only wan't to send the error message to errorCentral once.
|
// We only wan't to send the error message to errorCentral once.
|
||||||
|
@ -705,34 +707,33 @@ func (p process) publishMessages(natsConn *nats.Conn) {
|
||||||
p.processes.active.mu.Unlock()
|
p.processes.active.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle the error.
|
// // Handle the error.
|
||||||
|
// //
|
||||||
|
// // NOTE: None of the processes above generate an error, so the the
|
||||||
|
// // if clause will never be triggered. But keeping it here as an example
|
||||||
|
// // for now for how to handle errors.
|
||||||
|
// if err != nil {
|
||||||
|
// // Create an error type which also creates a channel which the
|
||||||
|
// // errorKernel will send back the action about what to do.
|
||||||
|
// ep := errorEvent{
|
||||||
|
// //errorType: logOnly,
|
||||||
|
// process: p,
|
||||||
|
// message: m,
|
||||||
|
// errorActionCh: make(chan errorAction),
|
||||||
|
// }
|
||||||
|
// p.errorCh <- ep
|
||||||
//
|
//
|
||||||
// NOTE: None of the processes above generate an error, so the the
|
// // Wait for the response action back from the error kernel, and
|
||||||
// if clause will never be triggered. But keeping it here as an example
|
// // decide what to do. Should we continue, quit, or .... ?
|
||||||
// for now for how to handle errors.
|
// switch <-ep.errorActionCh {
|
||||||
if err != nil {
|
// case errActionContinue:
|
||||||
// Create an error type which also creates a channel which the
|
// // Just log and continue
|
||||||
// errorKernel will send back the action about what to do.
|
// log.Printf("The errAction was continue...so we're continuing\n")
|
||||||
ep := errorEvent{
|
// case errActionKill:
|
||||||
//errorType: logOnly,
|
// log.Printf("The errAction was kill...so we're killing\n")
|
||||||
process: p,
|
// // ....
|
||||||
message: m,
|
// default:
|
||||||
errorActionCh: make(chan errorAction),
|
// log.Printf("Info: publishMessages: The errAction was not defined, so we're doing nothing\n")
|
||||||
}
|
// }
|
||||||
p.errorCh <- ep
|
// }
|
||||||
|
|
||||||
// Wait for the response action back from the error kernel, and
|
|
||||||
// decide what to do. Should we continue, quit, or .... ?
|
|
||||||
switch <-ep.errorActionCh {
|
|
||||||
case errActionContinue:
|
|
||||||
// Just log and continue
|
|
||||||
log.Printf("The errAction was continue...so we're continuing\n")
|
|
||||||
case errActionKill:
|
|
||||||
log.Printf("The errAction was kill...so we're killing\n")
|
|
||||||
// ....
|
|
||||||
default:
|
|
||||||
log.Printf("Info: publishMessages: The errAction was not defined, so we're doing nothing\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue