From d6310492a396aa2b7e7a489aeda745d5d2f09d91 Mon Sep 17 00:00:00 2001 From: postmannen Date: Mon, 8 Mar 2021 14:09:14 +0100 Subject: [PATCH] removed server from method type --- process.go | 8 ++++++-- subscriberMethodTypes.go | 12 ++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/process.go b/process.go index 6d95969..f53f63b 100644 --- a/process.go +++ b/process.go @@ -50,6 +50,8 @@ type process struct { // The channel to send a messages to the procFunc go routine. // This is typically used within the methodHandler. procFuncCh chan Message + // copy of the configuration from server + configuration *Configuration } // prepareNewProcess will set the the provided values and the default @@ -88,6 +90,8 @@ func newProcess(processes *processes, subject Subject, errCh chan errProcess, pr // It will give the process the next available ID, and also add the // process to the processes map in the server structure. func (p process) spawnWorker(s *server) { + // Copy the configuration from Server + p.configuration = s.configuration // We use the full name of the subject to identify a unique // process. We can do that since a process can only handle // one message queue. @@ -251,7 +255,7 @@ func (p process) subscriberHandler(natsConn *nats.Conn, thisNode string, msg *na // The handler started here is what actually doing the action // that executed a CLI command, or writes to a log file on // the node who received the message. - out, err = mf.handler(s, p, message, thisNode) + out, err = mf.handler(p, message, thisNode) if err != nil { // TODO: Send to error kernel ? @@ -286,7 +290,7 @@ func (p process) subscriberHandler(natsConn *nats.Conn, thisNode string, msg *na // since we don't send a reply for a NACK message, we don't care about the // out return when calling mf.handler //fmt.Printf("-- DEBUG 2.2.1: %#v\n\n", p.subject) - _, err := mf.handler(s, p, message, thisNode) + _, err := mf.handler(p, message, thisNode) if err != nil { // TODO: Send to error kernel ? diff --git a/subscriberMethodTypes.go b/subscriberMethodTypes.go index 4080ba9..3f3a32d 100644 --- a/subscriberMethodTypes.go +++ b/subscriberMethodTypes.go @@ -120,7 +120,7 @@ func (ma MethodsAvailable) CheckIfExists(m Method) (methodHandler, bool) { // ------------------------------------------------------------ type methodHandler interface { - handler(server *server, proc process, message Message, node string) ([]byte, error) + handler(proc process, message Message, node string) ([]byte, error) getKind() CommandOrEvent } @@ -134,7 +134,7 @@ func (m methodCommandCLICommand) getKind() CommandOrEvent { return m.commandOrEvent } -func (m methodCommandCLICommand) handler(s *server, proc process, message Message, node string) ([]byte, error) { +func (m methodCommandCLICommand) handler(proc process, message Message, node string) ([]byte, error) { // Since the command to execute is at the first position in the // slice we need to slice it out. The arguments are at the // remaining positions. @@ -161,14 +161,14 @@ func (m methodEventTextLogging) getKind() CommandOrEvent { return m.commandOrEvent } -func (m methodEventTextLogging) handler(s *server, proc process, message Message, node string) ([]byte, error) { +func (m methodEventTextLogging) handler(proc process, message Message, node string) ([]byte, error) { sub := Subject{ ToNode: string(message.ToNode), CommandOrEvent: proc.subject.CommandOrEvent, Method: message.Method, } - logFile := filepath.Join(s.configuration.SubscribersDataFolder, string(sub.name())+"-"+string(message.FromNode)) + logFile := filepath.Join(proc.configuration.SubscribersDataFolder, string(sub.name())+"-"+string(message.FromNode)) 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) @@ -200,7 +200,7 @@ func (m methodEventSayHello) getKind() CommandOrEvent { return m.commandOrEvent } -func (m methodEventSayHello) handler(s *server, proc process, message Message, node string) ([]byte, error) { +func (m methodEventSayHello) handler(proc process, message Message, node string) ([]byte, error) { //fmt.Printf("-- DEBUG 3.1: %#v, %#v, %#v\n\n", proc.subject.name(), proc.procFunc, proc.procFuncCh) //pn := processNameGet(proc.subject.name(), processKindSubscriber) //fmt.Printf("-- DEBUG 3.2: pn = %#v\n\n", pn) @@ -226,7 +226,7 @@ func (m methodEventErrorLog) getKind() CommandOrEvent { return m.commandOrEvent } -func (m methodEventErrorLog) handler(s *server, proc process, message Message, node string) ([]byte, error) { +func (m methodEventErrorLog) handler(proc process, message Message, node string) ([]byte, error) { log.Printf("<--- Received error from: %v, containing: %v", message.FromNode, message.Data) return nil, nil }