Skip to content

Commit

Permalink
Merge pull request #678 from flanksource/use-ctx-2
Browse files Browse the repository at this point in the history
chore: use ctx.DB in notifications and responder jobs
  • Loading branch information
moshloop authored Dec 13, 2023
2 parents ff6852f + df9d081 commit 16b76fe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
7 changes: 4 additions & 3 deletions logs/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/template"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
"github.com/labstack/echo/v4"
)

func LogsHandler(c echo.Context) error {
ctx := c.Request().Context().(context.Context)
var reqData struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -36,7 +37,7 @@ func LogsHandler(c echo.Context) error {
})
}

component, err := duty.GetComponent(c.Request().Context(), db.Gorm, reqData.ID)
component, err := query.GetComponent(ctx, reqData.ID)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.HTTPError{
Error: err.Error(),
Expand Down
3 changes: 1 addition & 2 deletions notification/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
dbModels "github.com/flanksource/incident-commander/db/models"
"github.com/flanksource/incident-commander/events"
"github.com/google/uuid"
Expand Down Expand Up @@ -112,7 +111,7 @@ var _ = ginkgo.Describe("Test Notification on incident creation", ginkgo.Ordered
sendHandler, err := events.NewNotificationSendConsumerAsync().EventConsumer()
Expect(err).NotTo(HaveOccurred())

ctx := context.NewContext(gocontext.Background()).WithDB(api.DefaultContext.DB(), db.Pool)
ctx := context.NewContext(gocontext.Background()).WithDB(api.DefaultContext.DB(), api.DefaultContext.Pool())

// Order of consumption is important as incident.create event
// produces a notification.send event
Expand Down
6 changes: 3 additions & 3 deletions notification/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/testutils"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
ginkgo "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down Expand Up @@ -85,11 +84,12 @@ var _ = ginkgo.BeforeSuite(func() {
}
logger.Infof("Started postgres on port: %d", port)

if db.Gorm, db.Pool, err = duty.SetupDB(connection, nil); err != nil {
dbGorm, dbPool, err := duty.SetupDB(connection, nil)
if err != nil {
ginkgo.Fail(err.Error())
}

api.DefaultContext = context.NewContext(gocontext.Background()).WithDB(db.Gorm, db.Pool)
api.DefaultContext = context.NewContext(gocontext.Background()).WithDB(dbGorm, dbPool)
setupWebhookServer()
})

Expand Down
9 changes: 5 additions & 4 deletions responder/config_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package responder

import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/job"
"github.com/flanksource/duty/models"

Expand All @@ -12,17 +13,17 @@ import (
// A shared config class for all responder configs.
const configClass = "Responder"

func upsertConfig(configType, externalID, name, config string) error {
func upsertConfig(ctx context.Context, configType, externalID, name, config string) error {
dbUpdateConfigQuery := `UPDATE config_items SET config = ? WHERE external_id = ARRAY[?] AND type = ? AND config_class = ?`
tx := db.Gorm.Exec(dbUpdateConfigQuery, config, externalID, configType, configClass)
tx := ctx.DB().Exec(dbUpdateConfigQuery, config, externalID, configType, configClass)
if tx.Error != nil {
logger.Errorf("Error updating config into database: %v", tx.Error)
return tx.Error
}

if tx.RowsAffected == 0 {
dbInsertConfigQuery := `INSERT INTO config_items (config_class, type, name, external_id, config) VALUES (?, ?, ?, ARRAY[?], ?)`
if err := db.Gorm.Exec(dbInsertConfigQuery, configClass, configType, name, externalID, config).Error; err != nil {
if err := ctx.DB().Exec(dbInsertConfigQuery, configClass, configType, name, externalID, config).Error; err != nil {
logger.Errorf("Error inserting config into database: %v", err)
return tx.Error
}
Expand Down Expand Up @@ -64,7 +65,7 @@ func SyncConfig(ctx job.JobRuntime) error {
jobHistory.AddError(err.Error()).End()
continue
} else {
if err := upsertConfig(configType, team.ID.String(), configName, config); err != nil {
if err := upsertConfig(ctx.Context, configType, team.ID.String(), configName, config); err != nil {
logger.Errorf("Error upserting config: %v", err)
jobHistory.AddError(err.Error()).End()
continue
Expand Down
13 changes: 6 additions & 7 deletions rules/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/flanksource/duty/job"
dutyModels "github.com/flanksource/duty/models"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/db/models"
)

Expand All @@ -36,7 +35,7 @@ func Run(ctx job.JobRuntime) error {
}

statuses := getAllStatii()
response, err := duty.QueryTopology(ctx, db.Pool, duty.TopologyOptions{
response, err := duty.QueryTopology(ctx, ctx.Pool(), duty.TopologyOptions{
Flatten: true,
Status: statuses,
})
Expand All @@ -50,12 +49,12 @@ func Run(ctx job.JobRuntime) error {
return err
}

return createIncidents(autoCreatedOpenIncidents, response.Components)
return createIncidents(ctx.Context, autoCreatedOpenIncidents, response.Components)
}

// createIncidents creates incidents based on the components
// and incident rules.
func createIncidents(openIncidentsMap map[string]map[string]struct{}, components dutyModels.Components) error {
func createIncidents(ctx context.Context, openIncidentsMap map[string]map[string]struct{}, components dutyModels.Components) error {
outer:
for _, component := range components {
for _, _rule := range Rules {
Expand All @@ -82,7 +81,7 @@ outer:
continue // this rule already created this incident
}

if err := db.Gorm.Create(&incident).Error; err != nil {
if err := ctx.DB().Create(&incident).Error; err != nil {
return err
}

Expand All @@ -92,7 +91,7 @@ outer:
Type: "factor",
}

if err := db.Gorm.Create(&hypothesis).Error; err != nil {
if err := ctx.DB().Create(&hypothesis).Error; err != nil {
return err
}

Expand All @@ -104,7 +103,7 @@ outer:
Description: component.Name + " is " + string(component.Status),
}

if err := db.Gorm.Create(&evidence).Error; err != nil {
if err := ctx.DB().Create(&evidence).Error; err != nil {
return err
}

Expand Down

0 comments on commit 16b76fe

Please sign in to comment.