mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 14:25:16 +03:00
refactor: defer close connection or file
This commit is contained in:
@@ -43,7 +43,6 @@ func (a *App) Run() {
|
||||
a.log.Error().Err(err).Msg("failed to connect to the database for migrations")
|
||||
return
|
||||
}
|
||||
defer migratePGConn.Close()
|
||||
|
||||
mgrt, err := migrator.NewMigrator(hubdir.MigrationsFS, "migrations")
|
||||
if err != nil {
|
||||
@@ -57,7 +56,9 @@ func (a *App) Run() {
|
||||
return
|
||||
}
|
||||
a.log.Info().Msg("migrations applied successfully")
|
||||
migratePGConn.Close()
|
||||
if err := migratePGConn.Close(); err != nil {
|
||||
a.log.Warn().Err(err).Msg("failed to close migrate postgres connection")
|
||||
}
|
||||
|
||||
a.log.Info().Msg("creating database connection pool")
|
||||
pool, err := pgxpool.New(ctx, a.cfg.GetURLPostgres())
|
||||
|
||||
@@ -26,7 +26,7 @@ func NewMigrator(sqlFiles embed.FS, dirname string) (*Migrator, error) {
|
||||
return &Migrator{srcDriver: d}, nil
|
||||
}
|
||||
|
||||
func (m *Migrator) ApplyMigrations(db *sql.DB) error {
|
||||
func (m *Migrator) ApplyMigrations(db *sql.DB) (err error) {
|
||||
driver, err := postgres.WithInstance(db, &postgres.Config{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create db instance: %w", err)
|
||||
@@ -37,7 +37,12 @@ func (m *Migrator) ApplyMigrations(db *sql.DB) error {
|
||||
return fmt.Errorf("unable to create migration: %w", err)
|
||||
}
|
||||
|
||||
defer migrator.Close()
|
||||
defer func() {
|
||||
closeErr, _ := migrator.Close()
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
}()
|
||||
|
||||
if err = migrator.Up(); err != nil && !errors.Is(err, migrate.ErrNoChange) {
|
||||
return fmt.Errorf("unable to apply migrations: %w", err)
|
||||
|
||||
@@ -30,16 +30,16 @@ func NewHubService(store Store, logger zerolog.Logger) *HubService {
|
||||
}
|
||||
|
||||
func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
|
||||
h.log.Debug().Str("agentID", data.AgentId).Str("agentName", data.AgentName).Msg("started registering agent")
|
||||
agent, err := h.store.GetAgentByAgentID(ctx, data.AgentId)
|
||||
h.log.Debug().Str("agentID", data.AgentID).Str("agentName", data.AgentName).Msg("started registering agent")
|
||||
agent, err := h.store.GetAgentByAgentID(ctx, data.AgentID)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return domain.RegisterAgentResponse{}, fmt.Errorf("failed select agent to db: %w", err)
|
||||
}
|
||||
|
||||
if data.AgentId != "" && !errors.Is(err, sql.ErrNoRows) {
|
||||
if data.AgentID != "" && !errors.Is(err, sql.ErrNoRows) {
|
||||
h.log.Debug().Str("agentID", agent.AgentID).Str("agentName", data.AgentName).Msg("agent exists, updating")
|
||||
|
||||
data.AgentId = agent.AgentID
|
||||
data.AgentID = agent.AgentID
|
||||
|
||||
agentStore := toCreateAgentModel(data)
|
||||
|
||||
@@ -55,7 +55,7 @@ func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgen
|
||||
return domain.RegisterAgentResponse{}, fmt.Errorf("generate agent ID: %w", err)
|
||||
}
|
||||
|
||||
data.AgentId = AgentID
|
||||
data.AgentID = AgentID
|
||||
|
||||
agentStore := toCreateAgentModel(data)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
func toCreateAgentModel(agent domain.RegisterAgentRequest) domainHub.CreateAgentModel {
|
||||
return domainHub.CreateAgentModel{
|
||||
AgentID: agent.AgentId,
|
||||
AgentID: agent.AgentID,
|
||||
AgentName: agent.AgentName,
|
||||
Architecture: agent.Host.Arch,
|
||||
System: agent.Host.System,
|
||||
|
||||
@@ -16,7 +16,7 @@ func toDBAgent(agent domainHub.CreateAgentModel) gen2.CreateAgentParams {
|
||||
System: agent.System,
|
||||
Hostname: agent.Hostname,
|
||||
Version: agent.Version,
|
||||
Capabilities: toJsonCapabilities(agent.Capabilities),
|
||||
Capabilities: toJSONCapabilities(agent.Capabilities),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,11 @@ func toUpdateDBAgent(agent domainHub.CreateAgentModel) gen2.UpdateAgentByIDParam
|
||||
System: agent.System,
|
||||
Hostname: agent.Hostname,
|
||||
Version: agent.Version,
|
||||
Capabilities: toJsonCapabilities(agent.Capabilities),
|
||||
Capabilities: toJSONCapabilities(agent.Capabilities),
|
||||
}
|
||||
}
|
||||
|
||||
func toJsonCapabilities(caps []domain.Capability) []byte {
|
||||
func toJSONCapabilities(caps []domain.Capability) []byte {
|
||||
data, err := json.Marshal(caps)
|
||||
if err != nil {
|
||||
// Note: Error is silently handled - consider logging in production
|
||||
|
||||
Reference in New Issue
Block a user