-
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: instant job update notifications using server-sent events
This commit introduces a new feature that enables clients to receive instant notifications for job status updates. Details: - A new endpoint `/jobs/{id}/status/subscribe` has been added, which implements instant 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 8f073d8
Showing
48 changed files
with
2,291 additions
and
6 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,92 @@ | ||
package subscribestatus | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Author: Michael Adler <[email protected]> | ||
*/ | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/Southclaws/fault" | ||
"github.com/go-openapi/runtime" | ||
"github.com/go-openapi/runtime/client" | ||
"github.com/go-openapi/strfmt" | ||
"github.com/rs/zerolog/log" | ||
"github.com/spf13/cobra" | ||
"github.com/tmaxmax/go-sse" | ||
|
||
"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 | ||
} | ||
|
||
// Submit implements the runtime.ClientTransport interface. | ||
func (t SSETransport) Submit(op *runtime.ClientOperation) (interface{}, error) { | ||
cfg := t.baseCmd.CreateTransportConfig() | ||
rt := client.New(cfg.Host, generatedClient.DefaultBasePath, cfg.Schemes) | ||
req := errutil.Must(rt.CreateHttpRequest(op)) | ||
|
||
httpClient := errutil.Must(t.baseCmd.CreateHTTPClient()) | ||
httpClient.Timeout = 0 | ||
|
||
client := sse.Client{ | ||
HTTPClient: httpClient, | ||
DefaultReconnectionTime: time.Second * 5, | ||
ResponseValidator: sse.DefaultValidator, | ||
} | ||
|
||
conn := client.NewConnection(req) | ||
unsubscribe := conn.SubscribeMessages(func(event sse.Event) { | ||
_, _ = os.Stdout.Write(event.Data) | ||
os.Stdout.Write([]byte("\n")) | ||
}) | ||
defer unsubscribe() | ||
|
||
err := conn.Connect() | ||
if err != nil { | ||
return nil, fault.Wrap(err) | ||
} | ||
|
||
return jobs.NewGetJobsIDStatusSubscribeOK(), nil | ||
} | ||
|
||
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) { | ||
params := jobs.NewGetJobsIDStatusSubscribeParams(). | ||
WithID(flags.Koanf.String(idFlag)) | ||
if params.ID == "" { | ||
log.Fatal().Msg("Job ID missing") | ||
} | ||
|
||
baseCmd := flags.NewBaseCmd() | ||
executor := generatedClient.New(SSETransport{baseCmd: &baseCmd}, strfmt.Default) | ||
if _, err := executor.Jobs.GetJobsIDStatusSubscribe(params); err != nil { | ||
log.Fatal().Err(err).Msg("Failed to subscribe to job status") | ||
} | ||
}, | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
package subscribestatus | ||
|
||
/* | ||
* SPDX-FileCopyrightText: 2023 Siemens AG | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Author: Michael Adler <[email protected]> | ||
*/ | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/siemens/wfx/cmd/wfxctl/flags" | ||
"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 | ||
|
||
w.Header().Add("Content-Type", "text/event-stream") | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write([]byte(`data: "hello world" | ||
`)) | ||
})) | ||
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.