1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00
ctrl/processes.go

248 lines
6.7 KiB
Go
Raw Permalink Normal View History

package ctrl
2021-02-24 09:58:02 +00:00
import (
"context"
2021-02-24 09:58:02 +00:00
"fmt"
"log"
"sync"
2021-02-24 09:58:02 +00:00
)
// processes holds all the information about running processes
type processes struct {
// mutex for processes
mu sync.Mutex
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
// The active spawned processes
2022-04-01 05:09:55 +00:00
// server
server *server
2021-10-08 10:07:10 +00:00
active procsMap
// mutex to lock the map
2021-10-08 20:39:46 +00:00
// mu sync.RWMutex
// The last processID created
lastProcessID int
2021-08-18 10:16:21 +00:00
// The instance global prometheus registry.
metrics *metrics
2022-02-02 07:54:36 +00:00
// Waitgroup to keep track of all the processes started.
2021-08-12 07:21:56 +00:00
wg sync.WaitGroup
// errorKernel
errorKernel *errorKernel
2022-02-02 07:54:36 +00:00
// configuration
configuration *Configuration
// Signatures
2022-04-21 11:21:36 +00:00
nodeAuth *nodeAuth
}
// newProcesses will prepare and return a *processes which
// is map containing all the currently running processes.
2022-04-01 05:09:55 +00:00
func newProcesses(ctx context.Context, server *server) *processes {
p := processes{
2022-04-01 05:09:55 +00:00
server: server,
2022-02-02 07:54:36 +00:00
active: *newProcsMap(),
2022-04-01 05:09:55 +00:00
errorKernel: server.errorKernel,
configuration: server.configuration,
2022-04-21 11:21:36 +00:00
nodeAuth: server.nodeAuth,
2022-04-01 06:51:14 +00:00
metrics: server.metrics,
}
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
return &p
}
// ----------------------
2022-02-02 07:54:36 +00:00
// ----------------------
type procsMap struct {
2021-11-16 18:07:24 +00:00
procNames map[processName]process
2021-11-16 09:21:44 +00:00
mu sync.Mutex
}
func newProcsMap() *procsMap {
cM := procsMap{
2021-11-16 18:07:24 +00:00
procNames: make(map[processName]process),
}
return &cM
}
// ----------------------
// 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
// --- Subscriber services that can be started via flags
proc.startup.startProcess(proc, OpProcessList, nil)
proc.startup.startProcess(proc, OpProcessStart, nil)
proc.startup.startProcess(proc, OpProcessStop, nil)
proc.startup.startProcess(proc, Test, nil)
2022-05-22 04:36:02 +00:00
if proc.configuration.StartProcesses.StartSubFileAppend {
proc.startup.startProcess(proc, FileAppend, nil)
2021-02-24 09:58:02 +00:00
}
if proc.configuration.StartProcesses.StartSubFile {
proc.startup.startProcess(proc, File, nil)
2021-04-06 17:42:03 +00:00
}
if proc.configuration.StartProcesses.StartSubCopySrc {
proc.startup.startProcess(proc, CopySrc, nil)
}
if proc.configuration.StartProcesses.StartSubCopyDst {
proc.startup.startProcess(proc, CopyDst, nil)
}
if proc.configuration.StartProcesses.StartSubHello {
proc.startup.startProcess(proc, Hello, procFuncHello)
2021-02-24 09:58:02 +00:00
}
if proc.configuration.StartProcesses.IsCentralErrorLogger {
proc.startup.startProcess(proc, ErrorLog, nil)
}
if proc.configuration.StartProcesses.StartSubCliCommand {
proc.startup.startProcess(proc, CliCommand, nil)
}
if proc.configuration.StartProcesses.StartSubConsole {
proc.startup.startProcess(proc, Console, nil)
}
if proc.configuration.StartProcesses.StartPubHello != 0 {
proc.startup.startProcess(proc, HelloPublisher, procFuncHelloPublisher)
}
2021-04-06 17:42:03 +00:00
if proc.configuration.StartProcesses.EnableKeyUpdates {
proc.startup.startProcess(proc, KeysRequestUpdate, procFuncKeysRequestUpdate)
proc.startup.startProcess(proc, KeysDeliverUpdate, nil)
}
if proc.configuration.StartProcesses.EnableAclUpdates {
proc.startup.startProcess(proc, AclRequestUpdate, procFuncAclRequestUpdate)
proc.startup.startProcess(proc, AclDeliverUpdate, nil)
2022-04-07 07:34:06 +00:00
}
if proc.configuration.StartProcesses.IsCentralAuth {
proc.startup.startProcess(proc, KeysRequestUpdate, nil)
proc.startup.startProcess(proc, KeysAllow, nil)
proc.startup.startProcess(proc, KeysDelete, nil)
proc.startup.startProcess(proc, AclRequestUpdate, nil)
proc.startup.startProcess(proc, AclAddCommand, nil)
proc.startup.startProcess(proc, AclDeleteCommand, nil)
proc.startup.startProcess(proc, AclDeleteSource, nil)
proc.startup.startProcess(proc, AclGroupNodesAddNode, nil)
proc.startup.startProcess(proc, AclGroupNodesDeleteNode, nil)
proc.startup.startProcess(proc, AclGroupNodesDeleteGroup, nil)
proc.startup.startProcess(proc, AclGroupCommandsAddCommand, nil)
proc.startup.startProcess(proc, AclGroupCommandsDeleteCommand, nil)
proc.startup.startProcess(proc, AclGroupCommandsDeleteGroup, nil)
proc.startup.startProcess(proc, AclExport, nil)
proc.startup.startProcess(proc, AclImport, nil)
2022-04-07 07:34:06 +00:00
}
if proc.configuration.StartProcesses.StartSubHttpGet {
proc.startup.startProcess(proc, HttpGet, nil)
2021-04-06 17:42:03 +00:00
}
2021-04-13 09:28:52 +00:00
if proc.configuration.StartProcesses.StartSubTailFile {
proc.startup.startProcess(proc, TailFile, nil)
2022-02-11 06:27:51 +00:00
}
if proc.configuration.StartProcesses.StartSubCliCommandCont {
proc.startup.startProcess(proc, CliCommandCont, nil)
2021-04-13 09:28:52 +00:00
}
proc.startup.startProcess(proc, PublicKey, nil)
2021-02-24 09:58:02 +00:00
}
2021-08-11 10:23:37 +00:00
// Stop all subscriber processes.
func (p *processes) Stop() {
log.Printf("info: canceling all subscriber processes...\n")
2021-08-11 10:23:37 +00:00
p.cancel()
p.wg.Wait()
log.Printf("info: done canceling all subscriber processes.\n")
2021-08-11 10:23:37 +00:00
}
// ---------------------------------------------------------------------------------------
// Startup holds all the startup methods for subscribers.
2021-08-18 10:16:21 +00:00
type startup struct {
server *server
centralAuth *centralAuth
metrics *metrics
2021-08-18 10:16:21 +00:00
}
2022-04-01 05:09:55 +00:00
func newStartup(server *server) *startup {
2022-04-01 07:21:50 +00:00
s := startup{
server: server,
centralAuth: server.centralAuth,
metrics: server.metrics,
2022-04-01 07:21:50 +00:00
}
2021-08-18 10:16:21 +00:00
return &s
}
// startProcess will start a process. It takes the initial process, request method,
// and a procFunc as it's input arguments. If a procFunc is not needed, use the value nil.
func (s *startup) startProcess(p process, m Method, pf func(ctx context.Context, proc process, procFuncCh chan Message) error) {
2023-04-18 13:47:40 +00:00
er := fmt.Errorf("starting %v subscriber: %#v", m, p.node)
p.errorKernel.logDebug(er)
var sub Subject
switch {
case m == ErrorLog:
sub = newSubject(m, "errorCentral")
default:
sub = newSubject(m, string(p.node))
}
fmt.Printf("DEBUG:::startup subscriber, subject: %v\n", sub)
proc := newProcess(p.ctx, p.processes.server, sub)
2023-04-18 13:47:40 +00:00
proc.procFunc = pf
2022-04-07 07:34:06 +00:00
go proc.start()
2022-02-08 10:49:32 +00:00
}
2021-08-11 10:23:37 +00:00
// ---------------------------------------------------------------
// Print the content of the processes map.
func (p *processes) printProcessesMap() {
er := fmt.Errorf("output of processes map : ")
p.errorKernel.logDebug(er)
2021-10-08 10:07:10 +00:00
2021-11-16 09:21:44 +00:00
{
p.active.mu.Lock()
2021-10-08 10:07:10 +00:00
2021-11-16 18:07:24 +00:00
for pName, proc := range p.active.procNames {
er := fmt.Errorf("info: proc - procName in map: %v , id: %v, subject: %v", pName, proc.processID, proc.subject.name())
proc.errorKernel.logDebug(er)
2021-08-11 10:23:37 +00:00
}
2022-04-01 07:21:50 +00:00
p.metrics.promProcessesTotal.Set(float64(len(p.active.procNames)))
2021-11-16 09:21:44 +00:00
p.active.mu.Unlock()
}
2021-08-11 10:23:37 +00:00
}