-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Prrromanssss/agentagregator_change_to_orch…
…estrator reorganize structure
- Loading branch information
Showing
14 changed files
with
618 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,43 @@ | ||
package agentservice | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"time" | ||
|
||
ag "github.com/Prrromanssss/DAEE-fullstack/internal/agent" | ||
agentapp "github.com/Prrromanssss/DAEE-fullstack/internal/app/agent" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/config" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/domain/brokers" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/lib/logger/sl" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/rabbitmq" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/lib/logger/logcleaner" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/lib/logger/setup" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/storage" | ||
"github.com/Prrromanssss/DAEE-fullstack/internal/storage/postgres" | ||
) | ||
|
||
// RunAgent makes all requirements needed to run agent and call AgentService to run it. | ||
func RunAgent(log *slog.Logger, cfg *config.Config, dbCfg *storage.Storage) { | ||
const fn = "agentservice.RunAgent" | ||
|
||
log = log.With( | ||
slog.String("fn", fn), | ||
) | ||
|
||
func main() { | ||
ctxWithCancel, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
amqpCfg, err := rabbitmq.NewAMQPConfig(log, cfg.RabbitMQURL) | ||
if err != nil { | ||
cancel() | ||
log.Error("can't create NewAMQPConfig", sl.Err(err)) | ||
return | ||
} | ||
|
||
producer, err := rabbitmq.NewAMQPProducer(log, amqpCfg, cfg.QueueForResultsFromAgents) | ||
if err != nil { | ||
cancel() | ||
log.Error("can't create NewAMQPProducer", sl.Err(err)) | ||
return | ||
} | ||
|
||
consumer, err := rabbitmq.NewAMQPConsumer(log, amqpCfg, cfg.QueueForExpressionsToAgents) | ||
if err != nil { | ||
cancel() | ||
log.Error("can't create NewAMQPConsumer", sl.Err(err)) | ||
return | ||
} | ||
// Load Config | ||
cfg := config.MustLoad() | ||
|
||
agent, err := ag.NewAgent( | ||
log, | ||
dbCfg, | ||
postgres.Agent{}, | ||
200, | ||
cancel, | ||
// Configuration Logger | ||
log := setup.SetupLogger(cfg.Env, cfg.LogPathAgent) | ||
log.Info( | ||
"start agent", | ||
slog.String("env", cfg.Env), | ||
slog.String("version", "2"), | ||
) | ||
if err != nil { | ||
cancel() | ||
log.Error("can't create agent", sl.Err(err)) | ||
return | ||
} | ||
log.Debug("debug messages are enabled") | ||
|
||
go AgentService(ctxWithCancel, log, amqpCfg, producer, consumer, agent) | ||
} | ||
go logcleaner.CleanLog(10*time.Minute, cfg.LogPathAgent, 100) | ||
|
||
// AgentService gets messages from SimpleComputers, handle these messages, | ||
// sends pings to Agent Agregator. | ||
func AgentService( | ||
ctx context.Context, | ||
log *slog.Logger, | ||
amqpCfg *rabbitmq.AMQPConfig, | ||
producer brokers.Producer, | ||
consumer brokers.Consumer, | ||
agent *ag.Agent, | ||
) { | ||
defer func() { | ||
amqpCfg.Close() | ||
producer.Close() | ||
consumer.Close() | ||
agent.MakeExpressionsTerminated(ctx) | ||
}() | ||
// Configuration Storage | ||
dbCfg := storage.NewStorage(cfg.StorageURL) | ||
|
||
go func() { | ||
for msgFromAgentAgregator := range consumer.GetMessages() { | ||
go agent.ConsumeMessageFromAgentAgregator(ctx, msgFromAgentAgregator) | ||
} | ||
}() | ||
|
||
ticker := time.NewTicker(time.Duration(agent.InactiveTime) * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case result := <-agent.SimpleComputers: | ||
go agent.ConsumeMessageFromComputers(ctx, result, producer) | ||
case <-ctx.Done(): | ||
agent.Terminate() | ||
return | ||
case <-ticker.C: | ||
agent.Ping(producer) | ||
} | ||
// Configuration Agent | ||
application, err := agentapp.New(log, cfg, dbCfg, cancel) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
go application.MustRun(ctxWithCancel) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.