2021-02-24 09:58:02 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
2021-04-07 16:54:08 +00:00
|
|
|
"context"
|
2021-02-24 09:58:02 +00:00
|
|
|
"fmt"
|
2021-03-12 11:08:11 +00:00
|
|
|
"log"
|
2021-03-09 10:58:50 +00:00
|
|
|
"time"
|
2021-03-04 15:27:55 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-02-24 09:58:02 +00:00
|
|
|
)
|
|
|
|
|
2021-03-10 06:11:14 +00:00
|
|
|
func (s *server) ProcessesStart() {
|
2021-03-26 08:08:47 +00:00
|
|
|
|
|
|
|
// --- Subscriber services that can be started via flags
|
|
|
|
|
2021-03-31 10:26:28 +00:00
|
|
|
{
|
2021-04-04 05:55:07 +00:00
|
|
|
fmt.Printf("Starting REQOpCommand subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQOpCommand, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, []node{"*"}, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-31 10:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 09:58:02 +00:00
|
|
|
// Start a subscriber for textLogging messages
|
2021-04-05 06:17:04 +00:00
|
|
|
if s.configuration.StartSubREQTextToLogFile.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
2021-04-05 06:17:04 +00:00
|
|
|
fmt.Printf("Starting text logging subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQTextToLogFile, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQTextToLogFile.Values, nil)
|
2021-03-26 08:08:47 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
2021-04-07 14:45:51 +00:00
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:42:03 +00:00
|
|
|
// Start a subscriber for text to file messages
|
|
|
|
if s.configuration.StartSubREQTextToFile.OK {
|
|
|
|
{
|
|
|
|
fmt.Printf("Starting text to file subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQTextToFile, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQTextToFile.Values, nil)
|
2021-04-06 17:42:03 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
2021-04-07 14:45:51 +00:00
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-04-06 17:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 06:37:24 +00:00
|
|
|
// Start a subscriber for Hello messages
|
2021-04-06 03:46:07 +00:00
|
|
|
if s.configuration.StartSubREQHello.OK {
|
2021-03-25 12:39:59 +00:00
|
|
|
{
|
2021-04-05 06:37:24 +00:00
|
|
|
fmt.Printf("Starting Hello subscriber: %#v\n", s.nodeName)
|
2021-04-06 03:46:07 +00:00
|
|
|
sub := newSubject(REQHello, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQHello.Values, nil)
|
2021-03-25 12:39:59 +00:00
|
|
|
proc.procFuncCh = make(chan Message)
|
2021-03-10 06:11:14 +00:00
|
|
|
|
2021-03-31 08:42:33 +00:00
|
|
|
// The reason for running the say hello subscriber as a procFunc is that
|
|
|
|
// a handler are not able to hold state, and we need to hold the state
|
|
|
|
// of the nodes we've received hello's from in the sayHelloNodes map,
|
|
|
|
// which is the information we pass along to generate metrics.
|
2021-04-07 16:54:08 +00:00
|
|
|
proc.procFunc = func(ctx context.Context) error {
|
2021-03-25 12:39:59 +00:00
|
|
|
sayHelloNodes := make(map[node]struct{})
|
|
|
|
for {
|
|
|
|
// Receive a copy of the message sent from the method handler.
|
2021-04-07 16:54:08 +00:00
|
|
|
var m Message
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m = <-proc.procFuncCh:
|
|
|
|
case <-ctx.Done():
|
|
|
|
er := fmt.Errorf("info: stopped handleFunc for: %v", proc.subject.name())
|
|
|
|
sendErrorLogMessage(proc.toRingbufferCh, proc.node, er)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 12:39:59 +00:00
|
|
|
fmt.Printf("--- DEBUG : procFunc call:kind=%v, Subject=%v, toNode=%v\n", proc.processKind, proc.subject, proc.subject.ToNode)
|
2021-03-10 13:14:09 +00:00
|
|
|
|
2021-03-25 12:39:59 +00:00
|
|
|
sayHelloNodes[m.FromNode] = struct{}{}
|
2021-03-05 12:10:46 +00:00
|
|
|
|
2021-03-25 12:39:59 +00:00
|
|
|
// update the prometheus metrics
|
2021-03-31 08:42:33 +00:00
|
|
|
proc.processes.metricsCh <- metricType{
|
2021-03-25 12:39:59 +00:00
|
|
|
metric: prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Name: "hello_nodes",
|
|
|
|
Help: "The current number of total nodes who have said hello",
|
|
|
|
}),
|
|
|
|
value: float64(len(sayHelloNodes)),
|
|
|
|
}
|
2021-03-05 12:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 14:45:51 +00:00
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-05 12:10:46 +00:00
|
|
|
}
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|
2021-02-24 14:43:31 +00:00
|
|
|
|
2021-04-06 05:56:49 +00:00
|
|
|
if s.configuration.StartSubREQErrorLog.OK {
|
|
|
|
// Start a subscriber for REQErrorLog messages
|
2021-02-24 14:43:31 +00:00
|
|
|
{
|
2021-04-06 05:56:49 +00:00
|
|
|
fmt.Printf("Starting REQErrorLog subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQErrorLog, "errorCentral")
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQErrorLog.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-02-24 14:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-09 10:58:50 +00:00
|
|
|
|
2021-04-06 04:08:26 +00:00
|
|
|
// Start a subscriber for Ping Request messages
|
|
|
|
if s.configuration.StartSubREQPing.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
2021-04-06 04:08:26 +00:00
|
|
|
fmt.Printf("Starting Ping Request subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQPing, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQPing.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 04:08:26 +00:00
|
|
|
// Start a subscriber for REQPong messages
|
|
|
|
if s.configuration.StartSubREQPong.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
2021-04-06 04:08:26 +00:00
|
|
|
fmt.Printf("Starting Pong subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQPong, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQPong.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 09:19:17 +00:00
|
|
|
// Start a subscriber for REQCliCommand messages
|
|
|
|
if s.configuration.StartSubREQCliCommand.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
|
|
|
fmt.Printf("Starting CLICommand Request subscriber: %#v\n", s.nodeName)
|
2021-04-04 09:19:17 +00:00
|
|
|
sub := newSubject(REQCliCommand, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQCliCommand.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 04:54:18 +00:00
|
|
|
// Start a subscriber for Not In Order Cli Command Request messages
|
|
|
|
if s.configuration.StartSubREQnCliCommand.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
2021-04-05 04:54:18 +00:00
|
|
|
fmt.Printf("Starting CLICommand Not Sequential Request subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQnCliCommand, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQnCliCommand.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a subscriber for CLICommandReply messages
|
2021-04-05 05:27:39 +00:00
|
|
|
if s.configuration.StartSubREQTextToConsole.OK {
|
2021-03-26 08:08:47 +00:00
|
|
|
{
|
2021-04-05 05:27:39 +00:00
|
|
|
fmt.Printf("Starting Text To Console subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQTextToConsole, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQTextToConsole.Values, nil)
|
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Publisher services that can be started via flags
|
|
|
|
|
2021-03-09 10:58:50 +00:00
|
|
|
// --------- Testing with publisher ------------
|
|
|
|
// Define a process of kind publisher with subject for SayHello to central,
|
|
|
|
// and register a procFunc with the process that will handle the actual
|
|
|
|
// sending of say hello.
|
2021-04-06 03:46:07 +00:00
|
|
|
if s.configuration.StartPubREQHello != 0 {
|
2021-04-05 06:37:24 +00:00
|
|
|
fmt.Printf("Starting Hello Publisher: %#v\n", s.nodeName)
|
2021-03-10 13:14:09 +00:00
|
|
|
|
2021-04-06 03:46:07 +00:00
|
|
|
sub := newSubject(REQHello, s.configuration.CentralNodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindPublisher, []node{}, nil)
|
2021-03-09 10:58:50 +00:00
|
|
|
|
2021-03-26 09:25:56 +00:00
|
|
|
// Define the procFunc to be used for the process.
|
2021-03-10 13:14:09 +00:00
|
|
|
proc.procFunc = procFunc(
|
2021-04-07 16:54:08 +00:00
|
|
|
func(ctx context.Context) error {
|
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(s.configuration.StartPubREQHello))
|
2021-03-10 13:14:09 +00:00
|
|
|
for {
|
|
|
|
fmt.Printf("--- DEBUG : procFunc call:kind=%v, Subject=%v, toNode=%v\n", proc.processKind, proc.subject, proc.subject.ToNode)
|
2021-03-09 10:58:50 +00:00
|
|
|
|
2021-03-10 13:14:09 +00:00
|
|
|
d := fmt.Sprintf("Hello from %v\n", s.nodeName)
|
2021-03-09 10:58:50 +00:00
|
|
|
|
2021-03-10 13:14:09 +00:00
|
|
|
m := Message{
|
|
|
|
ToNode: "central",
|
|
|
|
FromNode: node(s.nodeName),
|
|
|
|
Data: []string{d},
|
2021-04-06 03:46:07 +00:00
|
|
|
Method: REQHello,
|
2021-03-10 13:14:09 +00:00
|
|
|
}
|
2021-03-10 06:11:14 +00:00
|
|
|
|
2021-03-12 11:08:11 +00:00
|
|
|
sam, err := newSAM(m)
|
|
|
|
if err != nil {
|
|
|
|
// In theory the system should drop the message before it reaches here.
|
|
|
|
log.Printf("error: ProcessesStart: %v\n", err)
|
|
|
|
}
|
2021-03-29 11:36:30 +00:00
|
|
|
proc.toRingbufferCh <- []subjectAndMessage{sam}
|
2021-04-07 16:54:08 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ctx.Done():
|
2021-04-08 10:51:54 +00:00
|
|
|
fmt.Printf(" ** DEBUG: got <- ctx.Done\n")
|
2021-04-07 16:54:08 +00:00
|
|
|
er := fmt.Errorf("info: stopped handleFunc for: %v", proc.subject.name())
|
|
|
|
sendErrorLogMessage(proc.toRingbufferCh, proc.node, er)
|
|
|
|
return nil
|
|
|
|
}
|
2021-03-10 13:14:09 +00:00
|
|
|
}
|
|
|
|
})
|
2021-04-07 14:45:51 +00:00
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-03-09 10:58:50 +00:00
|
|
|
}
|
2021-04-06 17:42:03 +00:00
|
|
|
|
|
|
|
// Start a subscriber for Http Get Requests
|
|
|
|
if s.configuration.StartSubREQHttpGet.OK {
|
|
|
|
{
|
|
|
|
fmt.Printf("Starting Http Get subscriber: %#v\n", s.nodeName)
|
|
|
|
sub := newSubject(REQHttpGet, s.nodeName)
|
2021-04-07 14:45:51 +00:00
|
|
|
proc := newProcess(s.natsConn, s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubREQHttpGet.Values, nil)
|
2021-04-06 17:42:03 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
2021-04-07 14:45:51 +00:00
|
|
|
go proc.spawnWorker(s.processes, s.natsConn)
|
2021-04-06 17:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|