2021-02-25 17:46:40 -08:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2021-10-14 10:08:39 -07:00
|
|
|
log "log"
|
2023-03-20 16:00:15 -07:00
|
|
|
strconv "strconv"
|
2021-10-14 10:08:39 -07:00
|
|
|
sync "sync"
|
2021-02-25 17:46:40 -08:00
|
|
|
|
2023-01-11 18:49:48 -08:00
|
|
|
gstructs "github.com/dragonheim/gagent/internal/gstructs"
|
2021-05-23 08:42:29 -07:00
|
|
|
|
2023-03-20 07:20:46 -07:00
|
|
|
/*
|
2023-03-21 07:34:10 -07:00
|
|
|
* picol "github.com/dragonheim/gagent/pkg/picol"
|
2023-03-20 07:20:46 -07:00
|
|
|
*/
|
2021-10-18 05:46:52 -07:00
|
|
|
|
2021-10-25 12:43:10 -07:00
|
|
|
prometheus "github.com/prometheus/client_golang/prometheus"
|
|
|
|
promauto "github.com/prometheus/client_golang/prometheus/promauto"
|
2021-10-18 05:46:52 -07:00
|
|
|
|
2021-02-25 17:46:40 -08:00
|
|
|
zmq "github.com/pebbe/zmq4"
|
|
|
|
)
|
|
|
|
|
2021-10-25 12:43:10 -07:00
|
|
|
var (
|
|
|
|
opsProcessed = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Name: "agent_requests_collected",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2021-10-18 05:46:52 -07:00
|
|
|
/*
|
2023-03-20 07:20:46 -07:00
|
|
|
* The "worker" processes the agent code. The worker nodes do not know
|
|
|
|
* anything about the network structure. Instead they know only to which
|
|
|
|
* router(s) they are connected. The worker will execute the agent code and
|
|
|
|
* pass the agent and it's results to a router.
|
|
|
|
* Main is the entrypoint for the worker process
|
|
|
|
*/
|
2021-11-06 20:18:49 -07:00
|
|
|
func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
|
2021-05-21 23:03:50 +00:00
|
|
|
log.Printf("[INFO] Starting worker\n")
|
2021-11-06 20:18:49 -07:00
|
|
|
defer wg.Done()
|
2021-02-25 17:46:40 -08:00
|
|
|
|
2021-10-25 12:43:10 -07:00
|
|
|
for key := range config.Routers {
|
|
|
|
rport := config.WorkerPort
|
|
|
|
if config.Routers[key].WorkerPort != 0 {
|
|
|
|
rport = config.Routers[key].WorkerPort
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:21:44 -07:00
|
|
|
/*
|
|
|
|
* Generate connect string for this router.
|
|
|
|
*/
|
2023-03-21 07:34:10 -07:00
|
|
|
connectString := "tcp://" + config.Routers[key].RouterAddr + ":" + strconv.Itoa(rport)
|
2021-10-25 12:43:10 -07:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go getAgent(wg, config.UUID, connectString)
|
2021-02-25 17:46:40 -08:00
|
|
|
}
|
2023-03-20 07:20:46 -07:00
|
|
|
/*
|
2023-03-21 07:34:10 -07:00
|
|
|
* workerListener := "tcp://" + config.ListenAddr + ":" + strconv.Itoa(config.WorkerPort)
|
2023-03-20 07:20:46 -07:00
|
|
|
*/
|
2021-10-25 12:43:10 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAgent(wg *sync.WaitGroup, uuid string, connectString string) {
|
|
|
|
log.Printf("[DEBUG] Attempting to connect to %s\n", connectString)
|
|
|
|
defer wg.Done()
|
2021-02-26 10:41:42 -08:00
|
|
|
|
2021-05-21 23:03:50 +00:00
|
|
|
subscriber, _ := zmq.NewSocket(zmq.REP)
|
|
|
|
defer subscriber.Close()
|
2021-02-26 10:41:42 -08:00
|
|
|
|
2022-07-15 06:38:21 -07:00
|
|
|
_ = subscriber.Connect(connectString)
|
2021-02-26 10:41:42 -08:00
|
|
|
|
2021-05-21 23:03:50 +00:00
|
|
|
msg, err := subscriber.Recv(0)
|
|
|
|
if err != nil {
|
2021-05-30 17:15:46 +00:00
|
|
|
log.Printf("[DEBUG] Received error: %v", err)
|
2021-02-26 10:41:42 -08:00
|
|
|
}
|
2021-05-30 22:29:16 +00:00
|
|
|
log.Printf("[DEBUG] Received message: %v", msg[0])
|
2021-02-26 10:41:42 -08:00
|
|
|
}
|