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-08-11 08:11:57 +00:00
|
|
|
"sync"
|
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-08-11 08:11:57 +00:00
|
|
|
// processes holds all the information about running processes
|
|
|
|
type processes struct {
|
2021-08-11 10:23:37 +00:00
|
|
|
// The main context for subscriber processes.
|
|
|
|
ctx context.Context
|
|
|
|
// cancel func to send cancel signal to the subscriber processes context.
|
|
|
|
cancel context.CancelFunc
|
2021-08-11 08:11:57 +00:00
|
|
|
// The active spawned processes
|
2021-10-08 10:07:10 +00:00
|
|
|
active procsMap
|
2021-08-11 08:11:57 +00:00
|
|
|
// mutex to lock the map
|
|
|
|
mu sync.RWMutex
|
|
|
|
// The last processID created
|
|
|
|
lastProcessID int
|
2021-08-18 10:16:21 +00:00
|
|
|
// The instance global prometheus registry.
|
|
|
|
metrics *metrics
|
2021-08-12 07:21:56 +00:00
|
|
|
// Waitgroup to keep track of all the processes started
|
|
|
|
wg sync.WaitGroup
|
2021-08-11 08:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newProcesses will prepare and return a *processes which
|
|
|
|
// is map containing all the currently running processes.
|
2021-08-18 10:16:21 +00:00
|
|
|
func newProcesses(ctx context.Context, metrics *metrics) *processes {
|
2021-08-11 08:11:57 +00:00
|
|
|
p := processes{
|
2021-10-08 10:07:10 +00:00
|
|
|
active: *newProcsMap(),
|
2021-08-11 08:11:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 11:01:12 +00:00
|
|
|
// Prepare the parent context for the subscribers.
|
2021-08-11 10:23:37 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
2021-10-08 11:42:19 +00:00
|
|
|
// // Start the processes map.
|
|
|
|
// go func() {
|
|
|
|
// p.active.run(ctx)
|
|
|
|
// }()
|
2021-10-08 10:07:10 +00:00
|
|
|
|
2021-08-11 10:23:37 +00:00
|
|
|
p.ctx = ctx
|
|
|
|
p.cancel = cancel
|
|
|
|
|
2021-08-18 10:16:21 +00:00
|
|
|
p.metrics = metrics
|
|
|
|
|
2021-08-11 08:11:57 +00:00
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2021-10-08 06:16:12 +00:00
|
|
|
// ----------------------
|
|
|
|
|
|
|
|
type keyValue struct {
|
2021-10-08 10:07:10 +00:00
|
|
|
k processName
|
|
|
|
v map[int]process
|
2021-10-08 06:16:12 +00:00
|
|
|
ok bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type kvCh chan keyValue
|
|
|
|
|
|
|
|
type getValue struct {
|
2021-10-08 10:07:10 +00:00
|
|
|
k processName
|
2021-10-08 06:16:12 +00:00
|
|
|
kvCh kvCh
|
|
|
|
}
|
|
|
|
|
|
|
|
type procsMap struct {
|
2021-10-08 11:42:19 +00:00
|
|
|
m map[processName]map[int]process
|
|
|
|
mu sync.Mutex
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newProcsMap() *procsMap {
|
|
|
|
cM := procsMap{
|
2021-10-08 11:42:19 +00:00
|
|
|
m: make(map[processName]map[int]process),
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
return &cM
|
|
|
|
}
|
|
|
|
|
2021-10-08 11:42:19 +00:00
|
|
|
//func (c *procsMap) run(ctx context.Context) {
|
|
|
|
// for {
|
|
|
|
// select {
|
|
|
|
// case kvCh := <-c.mInCh:
|
|
|
|
// kv := <-kvCh
|
|
|
|
// c.m[kv.k] = kv.v
|
|
|
|
//
|
|
|
|
// case gv := <-c.mGetCh:
|
|
|
|
// v, ok := c.m[gv.k]
|
|
|
|
// gv.kvCh <- keyValue{gv.k, v, ok}
|
|
|
|
//
|
|
|
|
// case kvCh := <-c.mDelCh:
|
|
|
|
// kv := <-kvCh
|
|
|
|
// delete(c.m, kv.k)
|
|
|
|
//
|
|
|
|
// case gaCh := <-c.mGetAllCh:
|
|
|
|
// kvSlice := []keyValue{}
|
|
|
|
//
|
|
|
|
// for k, v := range c.m {
|
|
|
|
// kv := keyValue{k: k, v: v}
|
|
|
|
// kvSlice = append(kvSlice, kv)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// gaCh <- kvSlice
|
|
|
|
//
|
|
|
|
// case <-ctx.Done():
|
|
|
|
// log.Printf("info: processes active.Run: got ctx.Done\n")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
2021-10-08 06:16:12 +00:00
|
|
|
|
|
|
|
func (c *procsMap) put(kv keyValue) {
|
2021-10-08 11:42:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
c.m[kv.k] = kv.v
|
|
|
|
c.mu.Unlock()
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 10:07:10 +00:00
|
|
|
func (c *procsMap) get(key processName) keyValue {
|
2021-10-08 11:42:19 +00:00
|
|
|
var kv keyValue
|
2021-10-08 06:16:12 +00:00
|
|
|
|
2021-10-08 11:42:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
kv.v, kv.ok = c.m[key]
|
|
|
|
c.mu.Unlock()
|
2021-10-08 06:16:12 +00:00
|
|
|
|
2021-10-08 11:42:19 +00:00
|
|
|
return kv
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *procsMap) del(kv keyValue) {
|
2021-10-08 11:42:19 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.m, kv.k)
|
|
|
|
c.mu.Unlock()
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *procsMap) getAll() []keyValue {
|
2021-10-08 11:42:19 +00:00
|
|
|
var kvs []keyValue
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
for k, v := range c.m {
|
|
|
|
kvs = append(kvs, keyValue{k: k, v: v})
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
2021-10-08 06:16:12 +00:00
|
|
|
|
2021-10-08 11:42:19 +00:00
|
|
|
return kvs
|
2021-10-08 06:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
|
2021-08-11 08:11:57 +00:00
|
|
|
// Start all the subscriber processes.
|
|
|
|
// Takes an initial process as it's input. All processes
|
|
|
|
// will be tied to this single process's context.
|
|
|
|
func (p *processes) Start(proc process) {
|
2021-08-11 10:23:37 +00:00
|
|
|
// Set the context for the initial process.
|
|
|
|
proc.ctx = p.ctx
|
2021-03-26 08:08:47 +00:00
|
|
|
|
|
|
|
// --- Subscriber services that can be started via flags
|
|
|
|
|
2021-09-20 04:40:34 +00:00
|
|
|
// Allways start the listeners for Op commands
|
2021-03-31 10:26:28 +00:00
|
|
|
{
|
2021-08-11 08:11:57 +00:00
|
|
|
log.Printf("Starting REQOpCommand subscriber: %#v\n", proc.node)
|
|
|
|
sub := newSubject(REQOpCommand, string(proc.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(proc.ctx, p.metrics, proc.natsConn, p, proc.toRingbufferCh, proc.configuration, sub, proc.errorCh, processKindSubscriber, nil)
|
2021-08-11 08:11:57 +00:00
|
|
|
go proc.spawnWorker(proc.processes, proc.natsConn)
|
2021-03-31 10:26:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 04:40:34 +00:00
|
|
|
{
|
|
|
|
log.Printf("Starting REQOpProcessList subscriber: %#v\n", proc.node)
|
|
|
|
sub := newSubject(REQOpProcessList, string(proc.node))
|
|
|
|
proc := newProcess(proc.ctx, p.metrics, proc.natsConn, p, proc.toRingbufferCh, proc.configuration, sub, proc.errorCh, processKindSubscriber, nil)
|
|
|
|
go proc.spawnWorker(proc.processes, proc.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
log.Printf("Starting REQOpProcessStart subscriber: %#v\n", proc.node)
|
|
|
|
sub := newSubject(REQOpProcessStart, string(proc.node))
|
|
|
|
proc := newProcess(proc.ctx, p.metrics, proc.natsConn, p, proc.toRingbufferCh, proc.configuration, sub, proc.errorCh, processKindSubscriber, nil)
|
|
|
|
go proc.spawnWorker(proc.processes, proc.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-09-20 09:53:17 +00:00
|
|
|
{
|
|
|
|
log.Printf("Starting REQOpProcessStop subscriber: %#v\n", proc.node)
|
|
|
|
sub := newSubject(REQOpProcessStop, string(proc.node))
|
|
|
|
proc := newProcess(proc.ctx, p.metrics, proc.natsConn, p, proc.toRingbufferCh, proc.configuration, sub, proc.errorCh, processKindSubscriber, nil)
|
|
|
|
go proc.spawnWorker(proc.processes, proc.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-02-24 09:58:02 +00:00
|
|
|
// Start a subscriber for textLogging messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQToFileAppend {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQToFileAppend(proc)
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 17:42:03 +00:00
|
|
|
// Start a subscriber for text to file messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQToFile {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQToFile(proc)
|
2021-04-06 17:42:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 06:37:24 +00:00
|
|
|
// Start a subscriber for Hello messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQHello {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQHello(proc)
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|
2021-02-24 14:43:31 +00:00
|
|
|
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQErrorLog {
|
2021-04-06 05:56:49 +00:00
|
|
|
// Start a subscriber for REQErrorLog messages
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQErrorLog(proc)
|
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
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQPing {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQPing(proc)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 04:08:26 +00:00
|
|
|
// Start a subscriber for REQPong messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQPong {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQPong(proc)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 09:19:17 +00:00
|
|
|
// Start a subscriber for REQCliCommand messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQCliCommand {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQCliCommand(proc)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a subscriber for CLICommandReply messages
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQToConsole {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQToConsole(proc)
|
2021-03-26 08:08:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:11:57 +00:00
|
|
|
if proc.configuration.StartPubREQHello != 0 {
|
|
|
|
proc.startup.pubREQHello(proc)
|
2021-03-09 10:58:50 +00:00
|
|
|
}
|
2021-04-06 17:42:03 +00:00
|
|
|
|
|
|
|
// Start a subscriber for Http Get Requests
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQHttpGet {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQHttpGet(proc)
|
2021-04-06 17:42:03 +00:00
|
|
|
}
|
2021-04-13 09:28:52 +00:00
|
|
|
|
2021-09-08 16:56:23 +00:00
|
|
|
if proc.configuration.StartSubREQTailFile {
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQTailFile(proc)
|
2021-04-13 09:28:52 +00:00
|
|
|
}
|
2021-07-01 08:05:34 +00:00
|
|
|
|
2021-09-17 08:17:10 +00:00
|
|
|
if proc.configuration.StartSubREQCliCommandCont {
|
|
|
|
proc.startup.subREQCliCommandCont(proc)
|
2021-08-10 10:49:42 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 08:11:57 +00:00
|
|
|
proc.startup.subREQToSocket(proc)
|
2021-02-24 09:58:02 +00:00
|
|
|
}
|
2021-04-08 11:43:47 +00:00
|
|
|
|
2021-08-11 10:23:37 +00:00
|
|
|
// Stop all subscriber processes.
|
|
|
|
func (p *processes) Stop() {
|
2021-08-12 10:27:47 +00:00
|
|
|
log.Printf("info: canceling all subscriber processes...\n")
|
2021-08-11 10:23:37 +00:00
|
|
|
p.cancel()
|
2021-08-12 10:27:47 +00:00
|
|
|
p.wg.Wait()
|
|
|
|
log.Printf("info: done canceling all subscriber processes.\n")
|
|
|
|
|
2021-08-11 10:23:37 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 09:30:40 +00:00
|
|
|
// ---------------------------------------------------------------------------------------
|
2021-04-08 11:43:47 +00:00
|
|
|
|
2021-08-11 08:11:57 +00:00
|
|
|
// Startup holds all the startup methods for subscribers.
|
2021-08-18 10:16:21 +00:00
|
|
|
type startup struct {
|
|
|
|
metrics *metrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStartup(metrics *metrics) *startup {
|
|
|
|
s := startup{metrics: metrics}
|
|
|
|
|
|
|
|
return &s
|
|
|
|
}
|
2021-04-09 09:30:40 +00:00
|
|
|
|
|
|
|
func (s startup) subREQHttpGet(p process) {
|
|
|
|
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Http Get subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQHttpGet, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) pubREQHello(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Hello Publisher: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
|
|
|
|
sub := newSubject(REQHello, p.configuration.CentralNodeName)
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindPublisher, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
|
|
|
|
// Define the procFunc to be used for the process.
|
|
|
|
proc.procFunc = procFunc(
|
|
|
|
func(ctx context.Context) error {
|
|
|
|
ticker := time.NewTicker(time.Second * time.Duration(p.configuration.StartPubREQHello))
|
|
|
|
for {
|
|
|
|
// fmt.Printf("--- DEBUG : procFunc call:kind=%v, Subject=%v, toNode=%v\n", proc.processKind, proc.subject, proc.subject.ToNode)
|
|
|
|
|
|
|
|
d := fmt.Sprintf("Hello from %v\n", p.node)
|
|
|
|
|
|
|
|
m := Message{
|
2021-09-08 04:15:51 +00:00
|
|
|
FileName: "hello.log",
|
|
|
|
Directory: "hello-messages",
|
|
|
|
ToNode: Node(p.configuration.CentralNodeName),
|
|
|
|
FromNode: Node(p.node),
|
|
|
|
Data: []string{d},
|
|
|
|
Method: REQHello,
|
|
|
|
ACKTimeout: 10,
|
|
|
|
Retries: 1,
|
2021-04-09 09:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-25 06:56:44 +00:00
|
|
|
sam, err := newSubjectAndMessage(m)
|
2021-04-09 09:30:40 +00:00
|
|
|
if err != nil {
|
|
|
|
// In theory the system should drop the message before it reaches here.
|
2021-09-17 08:28:48 +00:00
|
|
|
sendErrorLogMessage(proc.configuration, s.metrics, proc.toRingbufferCh, Node(proc.node), err)
|
2021-04-09 09:30:40 +00:00
|
|
|
log.Printf("error: ProcessesStart: %v\n", err)
|
|
|
|
}
|
|
|
|
proc.toRingbufferCh <- []subjectAndMessage{sam}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
case <-ctx.Done():
|
2021-08-03 10:42:50 +00:00
|
|
|
er := fmt.Errorf("info: stopped handleFunc for: publisher %v", proc.subject.name())
|
2021-07-02 16:32:01 +00:00
|
|
|
// sendErrorLogMessage(proc.toRingbufferCh, proc.node, er)
|
|
|
|
log.Printf("%v\n", er)
|
2021-04-09 09:30:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:22:25 +00:00
|
|
|
func (s startup) subREQToConsole(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Text To Console subscriber: %#v\n", p.node)
|
2021-04-13 15:22:25 +00:00
|
|
|
sub := newSubject(REQToConsole, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) subREQCliCommand(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting CLICommand Request subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQCliCommand, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) subREQPong(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Pong subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQPong, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) subREQPing(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Ping Request subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQPing, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) subREQErrorLog(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting REQErrorLog subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQErrorLog, "errorCentral")
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s startup) subREQHello(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting Hello subscriber: %#v\n", p.node)
|
2021-04-09 09:30:40 +00:00
|
|
|
sub := newSubject(REQHello, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
proc.procFuncCh = make(chan Message)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
proc.procFunc = func(ctx context.Context) error {
|
2021-06-29 06:21:42 +00:00
|
|
|
sayHelloNodes := make(map[Node]struct{})
|
2021-04-12 08:51:26 +00:00
|
|
|
|
2021-04-09 09:30:40 +00:00
|
|
|
for {
|
|
|
|
// Receive a copy of the message sent from the method handler.
|
|
|
|
var m Message
|
|
|
|
|
|
|
|
select {
|
|
|
|
case m = <-proc.procFuncCh:
|
|
|
|
case <-ctx.Done():
|
2021-08-03 10:42:50 +00:00
|
|
|
er := fmt.Errorf("info: stopped handleFunc for: subscriber %v", proc.subject.name())
|
2021-07-02 16:32:01 +00:00
|
|
|
// sendErrorLogMessage(proc.toRingbufferCh, proc.node, er)
|
|
|
|
log.Printf("%v\n", er)
|
2021-04-09 09:30:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-12 14:40:18 +00:00
|
|
|
// Add an entry for the node in the map
|
2021-04-09 09:30:40 +00:00
|
|
|
sayHelloNodes[m.FromNode] = struct{}{}
|
|
|
|
|
|
|
|
// update the prometheus metrics
|
2021-08-18 13:41:53 +00:00
|
|
|
s.metrics.promHelloNodesTotal.Set(float64(len(sayHelloNodes)))
|
|
|
|
s.metrics.promHelloNodesContactLast.With(prometheus.Labels{"nodeName": string(m.FromNode)}).SetToCurrentTime()
|
2021-04-12 14:40:18 +00:00
|
|
|
|
2021-04-09 09:30:40 +00:00
|
|
|
}
|
2021-04-08 11:43:47 +00:00
|
|
|
}
|
2021-04-09 09:30:40 +00:00
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:15:13 +00:00
|
|
|
func (s startup) subREQToFile(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting text to file subscriber: %#v\n", p.node)
|
2021-04-13 15:15:13 +00:00
|
|
|
sub := newSubject(REQToFile, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-04-13 13:54:04 +00:00
|
|
|
func (s startup) subREQToFileAppend(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting text logging subscriber: %#v\n", p.node)
|
2021-04-13 13:54:04 +00:00
|
|
|
sub := newSubject(REQToFileAppend, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-09 09:30:40 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
2021-04-08 11:43:47 +00:00
|
|
|
}
|
2021-04-13 09:28:52 +00:00
|
|
|
|
|
|
|
func (s startup) subREQTailFile(p process) {
|
2021-04-16 11:43:58 +00:00
|
|
|
log.Printf("Starting tail log files subscriber: %#v\n", p.node)
|
2021-04-13 09:28:52 +00:00
|
|
|
sub := newSubject(REQTailFile, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-04-13 09:28:52 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
2021-07-01 08:05:34 +00:00
|
|
|
|
2021-09-17 08:17:10 +00:00
|
|
|
func (s startup) subREQCliCommandCont(p process) {
|
2021-08-10 10:49:42 +00:00
|
|
|
log.Printf("Starting cli command with continous delivery: %#v\n", p.node)
|
2021-09-17 08:17:10 +00:00
|
|
|
sub := newSubject(REQCliCommandCont, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-08-10 10:49:42 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
|
|
|
|
2021-07-01 08:05:34 +00:00
|
|
|
func (s startup) subREQToSocket(p process) {
|
|
|
|
log.Printf("Starting write to socket subscriber: %#v\n", p.node)
|
|
|
|
sub := newSubject(REQToSocket, string(p.node))
|
2021-09-08 15:57:21 +00:00
|
|
|
proc := newProcess(p.ctx, s.metrics, p.natsConn, p.processes, p.toRingbufferCh, p.configuration, sub, p.errorCh, processKindSubscriber, nil)
|
2021-07-01 08:05:34 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
|
|
|
go proc.spawnWorker(p.processes, p.natsConn)
|
|
|
|
}
|
2021-08-11 10:23:37 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
|
|
|
|
|
|
// Print the content of the processes map.
|
|
|
|
func (p *processes) printProcessesMap() {
|
|
|
|
log.Printf("*** Output of processes map :\n")
|
2021-10-08 10:07:10 +00:00
|
|
|
|
|
|
|
activeProcs := p.active.getAll()
|
|
|
|
|
|
|
|
for _, vSub := range activeProcs {
|
|
|
|
for _, vID := range vSub.v {
|
2021-09-20 10:22:34 +00:00
|
|
|
log.Printf("* proc - : %v, id: %v, name: %v\n", vID.processKind, vID.processID, vID.subject.name())
|
2021-08-11 10:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 10:07:10 +00:00
|
|
|
p.metrics.promProcessesTotal.Set(float64(len(activeProcs)))
|
2021-08-11 10:23:37 +00:00
|
|
|
|
|
|
|
}
|