From f2e4b851960ff07382c494909f7f4efd6858bd7f Mon Sep 17 00:00:00 2001 From: Sergey Kibish Date: Tue, 17 Apr 2018 10:41:06 +0300 Subject: [PATCH] Delete webhook on failed request --- pkg/api/api_test.go | 4 ++++ pkg/firebase/firebase.go | 13 +++++++++++++ pkg/scheduler/scheduler.go | 10 ++++++---- pkg/storage/storage_test.go | 5 +++++ pkg/storage/workspace.go | 8 ++++++++ pkg/storage/workspace_test.go | 13 +++++++++++++ 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index 584b636..521b869 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -16,6 +16,7 @@ type mockFirebase struct { set func(path string, v interface{}) (err error) get func(path string) (result json.RawMessage, err error) filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error) + delete func(path string) (err error) } func (m mockFirebase) Set(path string, v interface{}) (err error) { @@ -27,6 +28,9 @@ func (m mockFirebase) Get(path string) (result json.RawMessage, err error) { func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) { return m.filterEqual(path, field, value) } +func (m mockFirebase) Delete(path string) (err error) { + return m.delete(path) +} type mockSlack struct { oAuthAccess func(code string) (r *slack.OAuthAccessResponse, err error) diff --git a/pkg/firebase/firebase.go b/pkg/firebase/firebase.go index d3bcd08..37665bf 100644 --- a/pkg/firebase/firebase.go +++ b/pkg/firebase/firebase.go @@ -19,6 +19,7 @@ type IFirebase interface { Set(path string, v interface{}) (err error) Get(path string) (result json.RawMessage, err error) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) + Delete(path string) (err error) } // New return new instance of the Firebase @@ -65,6 +66,18 @@ func (f *Firebase) Get(path string) (result json.RawMessage, err error) { return } +// Delete deletes record from the database +func (f *Firebase) Delete(path string) (err error) { + ref, err := f.realFirebase.Ref(path) + if err != nil { + return + } + + err = ref.Remove() + + return +} + // FilterEqual filter records with field equal to specific value func (f *Firebase) FilterEqual(path, field string, value interface{}) (result json.RawMessage, err error) { ref, err := f.realFirebase.Ref(path) diff --git a/pkg/scheduler/scheduler.go b/pkg/scheduler/scheduler.go index b2c21c2..9e0481e 100644 --- a/pkg/scheduler/scheduler.go +++ b/pkg/scheduler/scheduler.go @@ -92,15 +92,17 @@ func (s *Scheduler) publish() (err error) { break } - // send trash to all published workspaces + // send trash to all subscribed workspaces for _, v := range workspaces { - go func(wh string) { + go func(wd storage.WorkspaceData) { json := []byte(fmt.Sprintf("{\"text\":%q}", trash)) - _, err := http.Post(wh, "application/json", bytes.NewBuffer(json)) + _, err := http.Post(wd.WebhookURL, "application/json", bytes.NewBuffer(json)) if err != nil { + log.Printf("Deleting workspace \"%s:%s\" because failed to POST", wd.ChannelID, wd.ID) + s.sg.Workspace.Delete(wd) return } - }(v.WebhookURL) + }(v) } return diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 6afcb7b..7b5d04d 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -9,6 +9,7 @@ type mockFirebase struct { set func(path string, v interface{}) (err error) get func(path string) (result json.RawMessage, err error) filterEqual func(path, field string, value interface{}) (result json.RawMessage, err error) + delete func(path string) (err error) } func (m mockFirebase) Set(path string, v interface{}) (err error) { @@ -22,6 +23,10 @@ func (m mockFirebase) FilterEqual(path, field string, value interface{}) (result return m.filterEqual(path, field, value) } +func (m mockFirebase) Delete(path string) (err error) { + return m.delete(path) +} + func TestNew(t *testing.T) { fm := &mockFirebase{} diff --git a/pkg/storage/workspace.go b/pkg/storage/workspace.go index 5ed4a42..775f812 100644 --- a/pkg/storage/workspace.go +++ b/pkg/storage/workspace.go @@ -40,3 +40,11 @@ func (w *Workspace) GetAll() (finResult map[string]WorkspaceData, err error) { return } + +// Delete deletes workspace data +func (w *Workspace) Delete(wd WorkspaceData) (err error) { + refKey := fmt.Sprintf("%s/%s:%s", workspacePath, wd.ID, wd.ChannelID) + err = w.firebase.Delete(refKey) + + return +} diff --git a/pkg/storage/workspace_test.go b/pkg/storage/workspace_test.go index a3f4c39..346ba6c 100644 --- a/pkg/storage/workspace_test.go +++ b/pkg/storage/workspace_test.go @@ -65,3 +65,16 @@ func TestWorkspaceGetAllErr(t *testing.T) { t.Error("Should be error, but everything is OK") } } + +func TestWorkspaceDelete(t *testing.T) { + fm := &mockFirebase{} + fm.delete = func(path string) (err error) { + return nil + } + + wp := Workspace{firebase: fm} + err := wp.Delete(WorkspaceData{ChannelID: "123", ID: "321", WebhookURL: "http://a/b/c"}) + if err != nil { + t.Errorf("Error was not expected: %v", err) + } +}