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

Receive Server-Side-Events within the UX and display them raw on the Publishing Process view #224

Merged
merged 6 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/connect-client/commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"os"

"github.com/r3labs/sse/v2"
"github.com/rstudio/connect-client/internal/apptypes"
"github.com/rstudio/connect-client/internal/bundles"
"github.com/rstudio/connect-client/internal/bundles/gitignore"
Expand Down Expand Up @@ -252,14 +253,18 @@ func (cmd *PublishUICmd) Run(args *cli_types.CommonArgs, ctx *cli_types.CLIConte
if err != nil {
return err
}
log := events.NewLoggerWithSSE(args.Debug)
eventServer := sse.New()
eventServer.CreateStream("messages")

log := events.NewLoggerWithSSE(args.Debug, eventServer)
svc := ui.NewUIService(
"/",
cmd.UIArgs,
&cmd.PublishArgs,
ctx.LocalToken,
ctx.Fs,
ctx.Accounts,
log)
log,
eventServer)
return svc.Run()
}
4 changes: 1 addition & 3 deletions internal/events/sse_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ func NewLogger(debug bool) logging.Logger {
return logging.FromStdLogger(slog.New(stderrHandler))
}

func NewLoggerWithSSE(debug bool) logging.Logger {
func NewLoggerWithSSE(debug bool, eventServer *sse.Server) logging.Logger {
level := logLevel(debug)
eventServer := sse.New()
eventServer.CreateStream("messages")
stderrHandler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: level})
sseHandler := NewSSEHandler(eventServer, &SSEHandlerOptions{Level: level})
multiHandler := logging.NewMultiHandler(stderrHandler, sseHandler)
Expand Down
4 changes: 3 additions & 1 deletion internal/events/sse_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"testing"

"github.com/r3labs/sse/v2"
"github.com/rstudio/connect-client/internal/logging"
"github.com/rstudio/connect-client/internal/util/utiltest"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -35,6 +36,7 @@ func (s *LoggerSuite) TestNewLoggerDebug() {
}

func (s *LoggerSuite) TestNewLoggerWithSSE() {
log := NewLoggerWithSSE(false)
sseServer := sse.New()
log := NewLoggerWithSSE(false, sseServer)
s.IsType(log.Handler(), &logging.MultiHandler{})
}
11 changes: 8 additions & 3 deletions internal/services/ui/ui_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/rstudio/connect-client/web"

"github.com/gorilla/mux"
"github.com/r3labs/sse/v2"
"github.com/spf13/afero"
)

Expand All @@ -30,9 +31,10 @@ func NewUIService(
token services.LocalToken,
fs afero.Fs,
lister accounts.AccountList,
log logging.Logger) *api.Service {
log logging.Logger,
eventServer *sse.Server) *api.Service {

handler := RouterHandlerFunc(fs, publish, lister, log)
handler := RouterHandlerFunc(fs, publish, lister, log, eventServer)

return api.NewService(
publish.State,
Expand All @@ -50,7 +52,7 @@ func NewUIService(
)
}

func RouterHandlerFunc(afs afero.Fs, publishArgs *cli_types.PublishArgs, lister accounts.AccountList, log logging.Logger) http.HandlerFunc {
func RouterHandlerFunc(afs afero.Fs, publishArgs *cli_types.PublishArgs, lister accounts.AccountList, log logging.Logger, eventServer *sse.Server) http.HandlerFunc {
deployment := publishArgs.State
base := deployment.SourceDir

Expand All @@ -63,6 +65,9 @@ func RouterHandlerFunc(afs afero.Fs, publishArgs *cli_types.PublishArgs, lister
r.Handle(ToPath("accounts"), api.GetAccountsHandlerFunc(lister, log)).
Methods(http.MethodGet)

// GET /api/events
r.HandleFunc(ToPath("events"), eventServer.ServeHTTP)

// GET /api/files
r.Handle(ToPath("files"), api.GetFileHandlerFunc(base, filesService, pathsService, log)).
Methods(http.MethodGet)
Expand Down
24 changes: 23 additions & 1 deletion web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
/>
<PublishProcess
v-if="currentView === 'publish'"
:events="allEvents"
:event-stream="eventStream"
@back="onConfigure"
/>
</q-page-container>
Expand All @@ -36,7 +38,7 @@

<script setup lang="ts">

import { ref } from 'vue';
import { onBeforeUnmount, ref } from 'vue';

import AppMenu from 'src/components/AppMenu.vue';
import ConfigurePublish from 'src/components/configurePublish/ConfigurePublish.vue';
Expand All @@ -45,13 +47,19 @@ import WhitePositLogo from 'src/components/icons/WhitePositLogo.vue';

import { useApi } from 'src/api';
import { useDeploymentStore } from 'src/stores/deployment';
import { EventStream } from 'src/api/resources/EventStream';
import { EventStreamMessage } from 'src/api/types/events';

type viewType = 'configure' | 'publish';

const currentView = ref<viewType>('configure');
const api = useApi();
const deploymentStore = useDeploymentStore();

const eventStream = new EventStream();
// Temporary storage of events
const allEvents = ref<EventStreamMessage[]>([]);

const onPublish = () => {
currentView.value = 'publish';
};
Expand All @@ -64,6 +72,20 @@ const getInitialDeploymentState = async() => {
deploymentStore.deployment = deployment;
};

const incomingEvent = (msg: EventStreamMessage) => {
allEvents.value.push(msg);
};
eventStream.addEventMonitorCallback(['*'], incomingEvent);

eventStream.setDebugMode(true);
eventStream.open('/api/events?stream=messages');
console.log(eventStream.status());

// Have to be sure to close connection or it will be leaked on agent (if it continues to run)
onBeforeUnmount(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity how did you choose between onBeforeUnmount and unmounted?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When I looked up lifecycle events for guidance, I followed this reference: Destruction Hooks – Cleaning Things Up which said:

Because this is before the component starts to get torn down, this is the time to do most, if not all, of the clean up. At this stage, your component is still fully functional and nothing has been destroyed yet.

It did seem more useful to have some of the component still around just incase I needed it, versus at unmount:

at this point, most of your component and its properties are gone so there’s not much you can do.

eventStream.close();
});

getInitialDeploymentState();
</script>

Expand Down
Loading