2023-04-08 13:58:47 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
2023-06-03 01:38:35 -06:00
|
|
|
"time"
|
2023-06-04 13:18:10 -06:00
|
|
|
"os"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-04-08 13:58:47 -06:00
|
|
|
)
|
|
|
|
|
2023-06-04 13:18:10 -06:00
|
|
|
type Logger interface {
|
|
|
|
Init() error
|
|
|
|
Logf(component string, format string, items ... interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultLogger struct {
|
|
|
|
init bool
|
|
|
|
init_lock sync.Mutex
|
|
|
|
loggers map[string]zerolog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
var log DefaultLogger = DefaultLogger{loggers: map[string]zerolog.Logger{}}
|
|
|
|
var all_components = []string{"update", "graph", "event", "resource", "manager", "test"}
|
|
|
|
|
|
|
|
func (logger * DefaultLogger) Init(components []string) error {
|
|
|
|
file, err := os.OpenFile("test.log", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
component_enabled := func (component string) bool {
|
|
|
|
for _, c := range(components) {
|
|
|
|
if c == component {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
writer := io.MultiWriter(file, os.Stdout)
|
|
|
|
for _, c := range(all_components) {
|
|
|
|
if component_enabled(c) == true {
|
|
|
|
logger.loggers[c] = zerolog.New(writer).With().Timestamp().Str("component", c).Logger()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (logger * DefaultLogger) Logf(component string, format string, items ... interface{}) {
|
|
|
|
logger.init_lock.Lock()
|
|
|
|
if logger.init == false {
|
|
|
|
logger.Init(all_components)
|
|
|
|
logger.init = true
|
|
|
|
}
|
|
|
|
logger.init_lock.Unlock()
|
|
|
|
|
|
|
|
l, exists := logger.loggers[component]
|
|
|
|
if exists == true {
|
|
|
|
l.Log().Msg(fmt.Sprintf(format, items...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// Generate a random graphql id
|
2023-05-31 00:37:51 -06:00
|
|
|
func randid() string{
|
2023-04-08 13:58:47 -06:00
|
|
|
uuid_str := uuid.New().String()
|
2023-05-31 00:37:51 -06:00
|
|
|
return uuid_str
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
type GraphSignal interface {
|
|
|
|
Source() GraphNode
|
|
|
|
Type() string
|
|
|
|
Description() string
|
2023-06-03 01:38:35 -06:00
|
|
|
Time() time.Time
|
2023-06-03 02:45:16 -06:00
|
|
|
Last() string
|
|
|
|
Trace(id string) GraphSignal
|
2023-06-04 17:23:49 -06:00
|
|
|
String() string
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type BaseSignal struct {
|
|
|
|
source GraphNode
|
|
|
|
signal_type string
|
|
|
|
description string
|
2023-06-03 01:38:35 -06:00
|
|
|
time time.Time
|
2023-06-03 02:45:16 -06:00
|
|
|
last_id string
|
2023-06-03 01:38:35 -06:00
|
|
|
}
|
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
func (signal BaseSignal) String() string {
|
|
|
|
source_name := "nil"
|
|
|
|
source := signal.source
|
|
|
|
if source != nil {
|
|
|
|
source_name = source.Name()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("{type: %s, description: %s, source: %s, last: %s}", signal.signal_type, signal.description, source_name, signal.last_id)
|
|
|
|
}
|
|
|
|
|
2023-06-03 01:38:35 -06:00
|
|
|
func (signal BaseSignal) Time() time.Time {
|
|
|
|
return signal.time
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Source() GraphNode {
|
|
|
|
return signal.source
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Type() string {
|
|
|
|
return signal.signal_type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Description() string {
|
|
|
|
return signal.description
|
|
|
|
}
|
|
|
|
|
2023-06-03 02:45:16 -06:00
|
|
|
func (signal BaseSignal) Trace(id string) GraphSignal {
|
|
|
|
new_signal := signal
|
|
|
|
new_signal.last_id = id
|
|
|
|
return new_signal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal BaseSignal) Last() string {
|
|
|
|
return signal.last_id
|
|
|
|
}
|
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
func NewSignal(source GraphNode, signal_type string) (BaseSignal) {
|
|
|
|
signal := BaseSignal{
|
|
|
|
source: source,
|
|
|
|
signal_type: signal_type,
|
|
|
|
}
|
|
|
|
return signal
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// GraphNode is the interface common to both DAG nodes and Event tree nodes
|
2023-04-08 13:58:47 -06:00
|
|
|
type GraphNode interface {
|
|
|
|
Name() string
|
|
|
|
Description() string
|
2023-05-31 00:37:51 -06:00
|
|
|
ID() string
|
2023-06-02 17:31:29 -06:00
|
|
|
UpdateListeners(update GraphSignal)
|
|
|
|
update(update GraphSignal)
|
2023-06-01 22:42:47 -06:00
|
|
|
RegisterChannel(listener chan GraphSignal)
|
|
|
|
UnregisterChannel(listener chan GraphSignal)
|
|
|
|
UpdateChannel() chan GraphSignal
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-03 18:56:14 -06:00
|
|
|
func NewBaseNode(name string, description string, id string) BaseNode {
|
|
|
|
node := BaseNode{
|
|
|
|
name: name,
|
|
|
|
description: description,
|
|
|
|
id: id,
|
2023-06-04 17:23:49 -06:00
|
|
|
signal: make(chan GraphSignal, 512),
|
2023-06-03 18:56:14 -06:00
|
|
|
listeners: map[chan GraphSignal]chan GraphSignal{},
|
|
|
|
}
|
2023-06-04 13:18:10 -06:00
|
|
|
log.Logf("graph", "NEW_NODE: %s - %s", node.ID(), node.Name())
|
2023-06-03 18:56:14 -06:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:42:33 -06:00
|
|
|
// BaseNode is the most basic implementation of the GraphNode interface
|
|
|
|
// It is used to implement functions common to Events and Resources
|
2023-04-08 13:58:47 -06:00
|
|
|
type BaseNode struct {
|
|
|
|
name string
|
|
|
|
description string
|
2023-05-31 00:37:51 -06:00
|
|
|
id string
|
2023-06-01 22:42:47 -06:00
|
|
|
signal chan GraphSignal
|
2023-04-08 13:58:47 -06:00
|
|
|
listeners_lock sync.Mutex
|
2023-06-01 22:42:47 -06:00
|
|
|
listeners map[chan GraphSignal]chan GraphSignal
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) Name() string {
|
|
|
|
return node.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node * BaseNode) Description() string {
|
|
|
|
return node.description
|
|
|
|
}
|
|
|
|
|
2023-05-31 00:37:51 -06:00
|
|
|
func (node * BaseNode) ID() string {
|
2023-04-08 13:58:47 -06:00
|
|
|
return node.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new listener channel for the node, add it to the nodes listener list, and return the new channel
|
2023-06-03 21:27:20 -06:00
|
|
|
const listener_buffer = 1000
|
2023-06-01 22:42:47 -06:00
|
|
|
func (node * BaseNode) UpdateChannel() chan GraphSignal {
|
|
|
|
new_listener := make(chan GraphSignal, listener_buffer)
|
|
|
|
node.RegisterChannel(new_listener)
|
2023-04-08 13:58:47 -06:00
|
|
|
return new_listener
|
|
|
|
}
|
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
func (node * BaseNode) RegisterChannel(listener chan GraphSignal) {
|
2023-04-08 13:58:47 -06:00
|
|
|
node.listeners_lock.Lock()
|
2023-06-01 22:42:47 -06:00
|
|
|
_, exists := node.listeners[listener]
|
|
|
|
if exists == false {
|
|
|
|
node.listeners[listener] = listener
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-01 22:42:47 -06:00
|
|
|
node.listeners_lock.Unlock()
|
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
func (node * BaseNode) UnregisterChannel(listener chan GraphSignal) {
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
_, exists := node.listeners[listener]
|
|
|
|
if exists == false {
|
|
|
|
panic("Attempting to unregister non-registered listener")
|
|
|
|
} else {
|
|
|
|
delete(node.listeners, listener)
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
node.listeners_lock.Unlock()
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
2023-04-08 13:58:47 -06:00
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
// Send the update to listener channels
|
|
|
|
func (node * BaseNode) UpdateListeners(update GraphSignal) {
|
|
|
|
node.listeners_lock.Lock()
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
closed := []chan GraphSignal{}
|
|
|
|
|
2023-06-01 22:42:47 -06:00
|
|
|
for _, listener := range node.listeners {
|
2023-06-04 13:18:10 -06:00
|
|
|
log.Logf("update", "UPDATE_LISTENER %s: %p", node.Name(), listener)
|
2023-06-02 17:31:29 -06:00
|
|
|
select {
|
|
|
|
case listener <- update:
|
|
|
|
default:
|
2023-06-04 13:18:10 -06:00
|
|
|
log.Logf("update", "CLOSED_LISTENER: %s: %p", node.Name(), listener)
|
2023-06-03 21:27:20 -06:00
|
|
|
go func(node GraphNode, listener chan GraphSignal) {
|
|
|
|
listener <- NewSignal(node, "listener_closed")
|
|
|
|
close(listener)
|
|
|
|
}(node, listener)
|
2023-06-02 17:31:29 -06:00
|
|
|
closed = append(closed, listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range(closed) {
|
|
|
|
delete(node.listeners, listener)
|
2023-06-01 22:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
node.listeners_lock.Unlock()
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
|
|
|
|
2023-06-02 17:31:29 -06:00
|
|
|
func (node * BaseNode) update(signal GraphSignal) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendUpdate(node GraphNode, signal GraphSignal) {
|
2023-06-04 17:23:49 -06:00
|
|
|
source := signal.Source()
|
|
|
|
source_name := "nil"
|
|
|
|
if source != nil {
|
|
|
|
source_name = source.Name()
|
|
|
|
}
|
2023-06-03 01:38:35 -06:00
|
|
|
|
2023-06-04 17:23:49 -06:00
|
|
|
node_name := "nil"
|
|
|
|
if node != nil {
|
|
|
|
node_name = node.Name()
|
2023-06-03 01:38:35 -06:00
|
|
|
}
|
2023-06-04 17:23:49 -06:00
|
|
|
log.Logf("update", "UPDATE %s -> %s: %+v", source_name, node_name, signal)
|
2023-06-01 22:42:47 -06:00
|
|
|
node.UpdateListeners(signal)
|
2023-06-02 17:31:29 -06:00
|
|
|
node.update(signal)
|
2023-04-08 13:58:47 -06:00
|
|
|
}
|
2023-06-02 17:31:29 -06:00
|
|
|
|