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

fix(webhooks): remove unused workflows field #665

Merged
merged 1 commit into from
Jul 2, 2024
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
24 changes: 10 additions & 14 deletions internal/services/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ func (c *WebhooksController) check() error {
}

type HealthCheckResponse struct {
Actions []string `json:"actions"`
Workflows []string `json:"workflows"`
Actions []string `json:"actions"`
}

func (c *WebhooksController) healthcheck(ww *dbsqlc.WebhookWorker) (*HealthCheckResponse, error) {
Expand Down Expand Up @@ -213,14 +212,13 @@ func (c *WebhooksController) run(tenantId string, webhookWorker *dbsqlc.WebhookW
}

ww, err := webhook.New(webhook.WorkerOpts{
Token: token,
ID: id,
Secret: secret,
URL: webhookWorker.Url,
Name: webhookWorker.Name,
TenantID: tenantId,
Actions: h.Actions,
Workflows: h.Workflows,
Token: token,
ID: id,
Secret: secret,
URL: webhookWorker.Url,
Name: webhookWorker.Name,
TenantID: tenantId,
Actions: h.Actions,
})
if err != nil {
return nil, fmt.Errorf("could not create webhook worker: %w", err)
Expand All @@ -240,7 +238,7 @@ func (c *WebhooksController) run(tenantId string, webhookWorker *dbsqlc.WebhookW
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
wfsHashLast := hash(h.Workflows)

actionsHashLast := hash(h.Actions)

healthCheckErrors := 0
Expand Down Expand Up @@ -269,10 +267,9 @@ func (c *WebhooksController) run(tenantId string, webhookWorker *dbsqlc.WebhookW
continue
}

wfsHash := hash(h.Workflows)
actionsHash := hash(h.Actions)

if wfsHash != wfsHashLast || actionsHash != actionsHashLast {
if actionsHash != actionsHashLast {
c.sc.Logger.Debug().Msgf("webhook worker %s of tenant %s health check changed, updating", id, tenantId)

// update the webhook workflow, and restart worker
Expand All @@ -297,7 +294,6 @@ func (c *WebhooksController) run(tenantId string, webhookWorker *dbsqlc.WebhookW
continue
}

wfsHashLast = wfsHash
actionsHashLast = actionsHash

if healthCheckErrors > 0 {
Expand Down
17 changes: 8 additions & 9 deletions pkg/webhook/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ type WebhookWorker struct {
}

type WorkerOpts struct {
Name string
Token string
ID string
Secret string
URL string
TenantID string
Actions []string
Workflows []string
Name string
Token string
ID string
Secret string
URL string
TenantID string
Actions []string
}

func New(opts WorkerOpts) (*WebhookWorker, error) {
Expand All @@ -40,7 +39,7 @@ func New(opts WorkerOpts) (*WebhookWorker, error) {
func (w *WebhookWorker) Start() (func() error, error) {
r, err := worker.NewWorker(
worker.WithClient(w.client),
worker.WithInternalData(w.opts.Actions, w.opts.Workflows),
worker.WithInternalData(w.opts.Actions),
worker.WithName("Webhook_"+w.opts.ID),
)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/worker/webhook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type WebhookHandlerOptions struct {
}

type HealthCheckResponse struct {
Actions []string `json:"actions"`
Workflows []string `json:"workflows"`
Actions []string `json:"actions"`
}

func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...workflowConverter) http.HandlerFunc {
Expand Down Expand Up @@ -76,8 +75,7 @@ func (w *Worker) WebhookHttpHandler(opts WebhookHandlerOptions, workflows ...wor
}

res := HealthCheckResponse{
Actions: actions,
Workflows: w.workflows,
Actions: actions,
}
data, err := json.Marshal(res)
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions pkg/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ type Worker struct {

actions map[string]Action

workflows []string

l *zerolog.Logger

cancelMap sync.Map
Expand Down Expand Up @@ -100,8 +98,7 @@ type WorkerOpts struct {
alerter errors.Alerter
maxRuns *int

actions []string
workflows []string
actions []string
}

func defaultWorkerOpts() *WorkerOpts {
Expand All @@ -128,10 +125,9 @@ func WithLogLevel(lvl string) WorkerOpt {
}
}

func WithInternalData(actions []string, workflows []string) WorkerOpt {
func WithInternalData(actions []string) WorkerOpt {
return func(opts *WorkerOpts) {
opts.actions = actions
opts.workflows = workflows
}
}

Expand Down Expand Up @@ -184,7 +180,6 @@ func NewWorker(fs ...WorkerOpt) (*Worker, error) {
middlewares: mws,
maxRuns: opts.maxRuns,
initActionNames: opts.actions,
workflows: opts.workflows,
}

mws.add(w.panicMiddleware)
Expand Down
Loading