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

Add webhooks #192

Closed
wants to merge 13 commits into from
Closed

Add webhooks #192

wants to merge 13 commits into from

Conversation

multikatt
Copy link

@multikatt multikatt commented Oct 16, 2023

DRAFT!

This PR implements webhooks in hostd to address #189.
It's not done all the way though, but before going too far I want to confirm I'm on the right track. The code here is heavily "borrowed" from renterd, so there's a fair bit of duplication. Is this ok or would it be better to reuse the code from renterd? What would be the preferred method of doing it in that case?
Lacking in the version here is the ability to delete webhooks, add actions and cleaning things up a bit.

Copy link
Member

@n8maninger n8maninger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good start! Thanks for tackling this.

)

type Manager struct {
ctx context.Context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bad practice to store a context on a struct. https://go.dev/blog/context-and-structs

api/api.go Outdated Show resolved Hide resolved
api/api.go Outdated Show resolved Hide resolved
api/api.go Outdated Show resolved Hide resolved
api/webhooks.go Outdated Show resolved Hide resolved
host/webhooks/webhooks.go Outdated Show resolved Hide resolved
host/webhooks/webhooks.go Outdated Show resolved Hide resolved
store WebhookStore

mu sync.Mutex
queues map[string]*eventQueue // URL -> queue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but seems overkill. A single queue would also probably be fine for
this small of a usecase.

host/webhooks/webhooks.go Outdated Show resolved Hide resolved
host/webhooks/webhooks.go Outdated Show resolved Hide resolved
Copy link
Member

@n8maninger n8maninger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good start! Thanks for tackling this.

Copy link
Member

@n8maninger n8maninger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good start! Thanks for tackling this.

@multikatt
Copy link
Author

Thanks for the comments! I'll get on adjusting.

hook_url TEXT NOT NULL
);`

if _, err = tx.Exec(query); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove the extra if and context and return

_, err := tx.Exec(...)
return err

}

func (s *Store) Webhooks() (whs []webhooks.Webhook, err error) {
rows, err := s.db.Query(`SELECT * FROM webhooks;`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be explicit about columns SELECT id, scope, ... FROM webhooks

persist/sqlite/webhooks.go Outdated Show resolved Hide resolved
persist/sqlite/init.sql Outdated Show resolved Hide resolved
func (s *Store) AddWebhook(wb webhooks.Webhook) (err error) {
var count int

err = s.db.QueryRow(`SELECT COUNT(*) FROM webhooks WHERE scope = ? AND hook_url = ?;`, strings.Join(wb.Scope, ","), wb.URL).Scan(&count)
Copy link
Member

@n8maninger n8maninger Oct 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the extra SELECT COUNT(*), just INSERT. Also add ON CONFLICT (hook_url) DO UPDATE for upserts

}
ctx, cancel := context.WithCancel(context.Background())
m := &Manager{
ctx: ctx,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the context.Context and context.CancelFunc from the struct

@multikatt
Copy link
Author

I'm having a real busy week which is why i haven't had time to look further at this, but I think I'll have time this weekend. Just so you know I haven't forgotten about it.

@justmert
Copy link

Hey @multikatt, are you still implementing this feature?

@multikatt
Copy link
Author

@justmert @n8maninger Im sorry about leaving this for so long, I was hoping to have time to get back to it but life's gotten in the way unfortunately so I'm just not going to be able to finish this within a reasonable time frame. Hopefully somebody can use what I've done so far.

@n8maninger
Copy link
Member

@justmert @n8maninger Im sorry about leaving this for so long, I was hoping to have time to get back to it but life's gotten in the way unfortunately so I'm just not going to be able to finish this within a reasonable time frame. Hopefully somebody can use what I've done so far.

No worries. I’ll finish it out. Thanks for getting this far

@justmert
Copy link

justmert commented Dec 5, 2023

Hi, sorry for impatient me. Is there a time when it will be implemented? Since I need this functionality. Thank you @n8maninger

@n8maninger n8maninger closed this Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants