-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MM-54589] Report job statuses, review connectivity check, structured…
… logging * [MM-54589] Report job start failure (#46) * [MM-54749] Make connectivity check more resilient to errors (#47) * [MM-54747] Implement structured logging (#48)
- Loading branch information
1 parent
70a3df0
commit 77ceb17
Showing
14 changed files
with
278 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ jobs: | |
|
||
strategy: | ||
matrix: | ||
go-version: [1.18.x] | ||
go-version: [1.21.x] | ||
|
||
runs-on: ubuntu-latest | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/mattermost/mattermost-plugin-calls/server/public" | ||
) | ||
|
||
func (rec *Recorder) postJobStatus(status public.JobStatus) error { | ||
apiURL := fmt.Sprintf("%s/plugins/%s/bot/calls/%s/jobs/%s/status", | ||
rec.client.URL, pluginID, rec.cfg.CallID, rec.cfg.RecordingID) | ||
|
||
payload, err := json.Marshal(&status) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal: %w", err) | ||
} | ||
|
||
ctx, cancelCtx := context.WithTimeout(context.Background(), httpRequestTimeout) | ||
defer cancelCtx() | ||
resp, err := rec.client.DoAPIRequestBytes(ctx, http.MethodPost, apiURL, payload, "") | ||
if err != nil { | ||
return fmt.Errorf("request failed%w", err) | ||
} | ||
defer resp.Body.Close() | ||
cancelCtx() | ||
|
||
return nil | ||
} | ||
|
||
func (rec *Recorder) ReportJobFailure(errMsg string) error { | ||
return rec.postJobStatus(public.JobStatus{ | ||
JobType: public.JobTypeRecording, | ||
Status: public.JobStatusTypeFailed, | ||
Error: errMsg, | ||
}) | ||
} | ||
|
||
func (rec *Recorder) ReportJobStarted() error { | ||
return rec.postJobStatus(public.JobStatus{ | ||
JobType: public.JobTypeRecording, | ||
Status: public.JobStatusTypeStarted, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/mattermost/calls-recorder/cmd/recorder/config" | ||
"github.com/mattermost/mattermost-plugin-calls/server/public" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReportJobFailure(t *testing.T) { | ||
middlewares := []middleware{} | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
for _, mw := range middlewares { | ||
if mw(w, r) { | ||
return | ||
} | ||
} | ||
http.NotFound(w, r) | ||
})) | ||
defer ts.Close() | ||
|
||
cfg := config.RecorderConfig{ | ||
SiteURL: ts.URL, | ||
CallID: "8w8jorhr7j83uqr6y1st894hqe", | ||
ThreadID: "udzdsg7dwidbzcidx5khrf8nee", | ||
RecordingID: "67t5u6cmtfbb7jug739d43xa9e", | ||
AuthToken: "qj75unbsef83ik9p7ueypb6iyw", | ||
} | ||
cfg.SetDefaults() | ||
rec, err := NewRecorder(cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, rec) | ||
|
||
t.Run("request failure", func(t *testing.T) { | ||
middlewares = []middleware{ | ||
func(w http.ResponseWriter, r *http.Request) bool { | ||
if r.URL.Path != "/plugins/com.mattermost.calls/bot/calls/8w8jorhr7j83uqr6y1st894hqe/jobs/67t5u6cmtfbb7jug739d43xa9e/status" { | ||
w.WriteHeader(404) | ||
return true | ||
} | ||
|
||
w.WriteHeader(400) | ||
fmt.Fprintln(w, `{"message": "server error"}`) | ||
return true | ||
}, | ||
} | ||
err := rec.ReportJobFailure("") | ||
require.EqualError(t, err, "request failed: server error") | ||
}) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
var errMsg string | ||
middlewares = []middleware{ | ||
func(w http.ResponseWriter, r *http.Request) bool { | ||
if r.URL.Path != "/plugins/com.mattermost.calls/bot/calls/8w8jorhr7j83uqr6y1st894hqe/jobs/67t5u6cmtfbb7jug739d43xa9e/status" { | ||
w.WriteHeader(404) | ||
return true | ||
} | ||
|
||
var status public.JobStatus | ||
if err := json.NewDecoder(r.Body).Decode(&status); err != nil { | ||
w.WriteHeader(400) | ||
fmt.Fprintf(w, `{"message": %q}`, err.Error()) | ||
return true | ||
} | ||
|
||
if status.JobType != public.JobTypeRecording { | ||
w.WriteHeader(400) | ||
return true | ||
} | ||
|
||
if status.Status != public.JobStatusTypeFailed { | ||
w.WriteHeader(400) | ||
return true | ||
} | ||
|
||
errMsg = status.Error | ||
|
||
w.WriteHeader(200) | ||
return true | ||
}, | ||
} | ||
err := rec.ReportJobFailure("some error") | ||
require.Nil(t, err) | ||
require.Equal(t, "some error", errMsg) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.