Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: event consumer #520

Merged
merged 8 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Event struct {
ID uuid.UUID `json:"id"`
ID uuid.UUID `gorm:"default:generate_ulid()"`
Name string `json:"name"`
Properties types.JSONStringMap `json:"properties"`
Error string `json:"error"`
Expand Down
5 changes: 5 additions & 0 deletions api/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func NewContext(db *gorm.DB, echoCtx EchoContext) *Context {
return c
}

func (c Context) WithDB(db *gorm.DB) *Context {
c.db = db
return &c
}

func (c *Context) DB() *gorm.DB {
if c.db == nil {
return nil
Expand Down
1 change: 1 addition & 0 deletions api/responders.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type IncidentResponders struct {

type Responder struct {
ID uuid.UUID `json:"id,omitempty"`
Type string `json:"type"`
Properties types.JSONStringMap `json:"properties" gorm:"type:jsonstringmap;<-:false"`
ExternalID string `json:"external_id,omitempty"`
IncidentID uuid.UUID `json:"incident_id,omitempty"`
Expand Down
9 changes: 7 additions & 2 deletions auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (k *KratosHandler) KratosMiddleware() (*kratosMiddleware, error) {
}, nil
}

var skipAuthPaths = []string{"/health", "/metrics"}
var skipAuthPaths = []string{"/health", "/metrics", "/kratos/*"}

func canSkipAuth(c echo.Context) bool {
return collections.Contains(skipAuthPaths, c.Path())
Expand Down Expand Up @@ -102,7 +102,12 @@ func (k *kratosMiddleware) Session(next echo.HandlerFunc) echo.HandlerFunc {
email = e
}
}
ctx.WithUser(&api.ContextUser{ID: uuid.MustParse(session.Identity.GetId()), Email: email})

if uid, err := uuid.Parse(session.Identity.GetId()); err != nil {
return c.String(http.StatusUnauthorized, "Unauthorized")
} else {
ctx.WithUser(&api.ContextUser{ID: uid, Email: email})
}

return next(ctx)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,11 @@ var Serve = &cobra.Command{

go jobs.Start()

events.StartConsumers(db.Gorm, events.Config{
events.StartConsumers(db.Gorm, db.Pool, events.Config{
UpstreamPush: api.UpstreamConf,
})

playbookRunConsumer := playbook.NewQueueConsumer(db.Gorm, db.Pool)
go playbookRunConsumer.Listen()
go playbook.StartPlaybookRunConsumer(db.Gorm, db.Pool)

go launchKopper()

Expand Down
11 changes: 0 additions & 11 deletions db/playbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ func FindPlaybooksForComponent(ctx *api.Context, configType string, tags map[str
return playbooks, err
}

// GetScheduledPlaybookRuns returns all the scheduled playbook runs that are scheduled to run now
// or before now.
func GetScheduledPlaybookRuns(ctx *api.Context, limit int, exceptions ...uuid.UUID) ([]models.PlaybookRun, error) {
var runs []models.PlaybookRun
if err := ctx.DB().Not(exceptions).Where("start_time <= NOW()").Where("status = ?", models.PlaybookRunStatusScheduled).Limit(limit).Order("start_time").Find(&runs).Error; err != nil {
return nil, err
}

return runs, nil
}

func PersistPlaybookFromCRD(obj *v1.Playbook) error {
specJSON, err := json.Marshal(obj.Spec)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions events/checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package events

func NewCheckConsumerSync() SyncEventConsumer {
return SyncEventConsumer{
watchEvents: []string{EventCheckPassed, EventCheckFailed},
consumers: []SyncEventHandlerFunc{addNotificationEvent, schedulePlaybookRun},
}
}
14 changes: 14 additions & 0 deletions events/components.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package events

func NewComponentConsumerSync() SyncEventConsumer {
return SyncEventConsumer{
watchEvents: []string{
EventComponentStatusError,
EventComponentStatusHealthy,
EventComponentStatusInfo,
EventComponentStatusUnhealthy,
EventComponentStatusWarning,
},
consumers: []SyncEventHandlerFunc{addNotificationEvent, schedulePlaybookRun},
}
}
150 changes: 0 additions & 150 deletions events/event_consumer.go

This file was deleted.

Loading
Loading