-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce real-time job update notifications via SSE
This commit introduces a new feature that enables clients to receive real-time notifications for job status updates. Details: - A new endpoint `/jobs/{id}/status/subscribe` has been added, which implements real-time notifications through Server-Sent Events (SSE). - By subscribing to this endpoint, clients can efficiently keep track of job status changes without the need to repeatedly poll the server. - A client reference implementation has been added to wfxctl. Signed-off-by: Michael Adler <[email protected]>
- Loading branch information
1 parent
649698b
commit 46296f8
Showing
42 changed files
with
2,415 additions
and
3 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
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,97 @@ | ||
package subscribestatus | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Author: Michael Adler <[email protected]> | ||
*/ | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/bcicen/jstream" | ||
"github.com/go-openapi/runtime" | ||
"github.com/go-openapi/runtime/client" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/siemens/wfx/cmd/wfxctl/errutil" | ||
"github.com/siemens/wfx/cmd/wfxctl/flags" | ||
generatedClient "github.com/siemens/wfx/generated/client" | ||
"github.com/siemens/wfx/generated/client/jobs" | ||
) | ||
|
||
const ( | ||
idFlag = "id" | ||
) | ||
|
||
func init() { | ||
f := Command.Flags() | ||
f.String(idFlag, "", "job id") | ||
} | ||
|
||
type SSETransport struct { | ||
baseCmd *flags.BaseCmd | ||
} | ||
|
||
func (t SSETransport) Submit(op *runtime.ClientOperation) (interface{}, error) { | ||
cfg := t.baseCmd.CreateTransportConfig() | ||
r := client.New(cfg.Host, generatedClient.DefaultBasePath, cfg.Schemes) | ||
req := errutil.Must(r.CreateHttpRequest(op)) | ||
|
||
// this also works with unix-domain sockets | ||
client := errutil.Must(t.baseCmd.CreateHTTPClient()) | ||
// no timeout for this connection, it can be open for a long time | ||
client.Timeout = 0 | ||
resp := errutil.Must(client.Do(req)) | ||
if !isSuccess(resp.StatusCode) { | ||
return nil, fmt.Errorf("got http status code %d", resp.StatusCode) | ||
} | ||
|
||
defer resp.Body.Close() | ||
reader := bufio.NewReader(resp.Body) | ||
|
||
decoder := jstream.NewDecoder(reader, 0) | ||
for mv := range decoder.Stream() { | ||
if event, ok := mv.Value.(map[string]any); ok { | ||
fmt.Println(event["data"]) | ||
} | ||
} | ||
|
||
return jobs.NewGetJobsIDStatusSubscribeOK(), nil | ||
} | ||
|
||
func isSuccess(statusCode int) bool { | ||
return statusCode >= http.StatusOK && statusCode < 300 | ||
} | ||
|
||
var Command = &cobra.Command{ | ||
Use: "subscribe-status", | ||
Short: "Subscribe to job update events", | ||
Example: ` | ||
wfxctl job subscribe-status --id=1 | ||
`, | ||
TraverseChildren: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
baseCmd := flags.NewBaseCmd() | ||
httpClient := errutil.Must(baseCmd.CreateHTTPClient()) | ||
params := jobs.NewGetJobsIDStatusSubscribeParams(). | ||
WithHTTPClient(httpClient). | ||
WithID(flags.Koanf.String(idFlag)) | ||
|
||
if params.ID == "" { | ||
log.Fatal().Msg("Job ID missing") | ||
} | ||
|
||
client := baseCmd.CreateClient() | ||
client.SetTransport(SSETransport{baseCmd: &baseCmd}) | ||
_, err := client.Jobs.GetJobsIDStatusSubscribe(params) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to subscribe to job status") | ||
} | ||
}, | ||
} |
52 changes: 52 additions & 0 deletions
52
cmd/wfxctl/cmd/job/subscribestatus/subscribe_status_test.go
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,52 @@ | ||
package subscribestatus | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Author: Michael Adler <[email protected]> | ||
*/ | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/siemens/wfx/cmd/wfxctl/errutil" | ||
"github.com/siemens/wfx/cmd/wfxctl/flags" | ||
"github.com/siemens/wfx/generated/model" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSubscribeJobStatus(t *testing.T) { | ||
const expectedPath = "/api/wfx/v1/jobs/1/status/subscribe" | ||
var actualPath string | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
actualPath = r.URL.Path | ||
|
||
event := model.SseEvent{Data: "hello world"} | ||
b := errutil.Must(json.Marshal(event)) | ||
|
||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(b) | ||
})) | ||
defer ts.Close() | ||
|
||
u, _ := url.Parse(ts.URL) | ||
_ = flags.Koanf.Set(flags.ClientHostFlag, u.Hostname()) | ||
port, _ := strconv.Atoi(u.Port()) | ||
_ = flags.Koanf.Set(flags.ClientPortFlag, port) | ||
|
||
_ = flags.Koanf.Set(idFlag, "1") | ||
|
||
err := Command.Execute() | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, expectedPath, actualPath) | ||
} |
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.