Skip to content

Commit

Permalink
update deploy events and use in command
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeodor committed May 28, 2024
1 parent 8c27f61 commit bd2eb6c
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/deploy/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

func run(cmd *cobra.Command, args []string) {
service := app.New(gql.NewClient(), nil, http.DefaultClient)
service := app.New(gql.NewClient(), gql.NewSubscriptionClient(), http.DefaultClient)
err := Deploy(cmd.Context(), ".", slug, appName, service)

if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions cli/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type AppService interface {
CreateVersion(ctx context.Context, input app.CreateAppVersionInput) (app.CreateAppVersionOutput, error)
UploadAppSource(uploadURL string, archive io.Reader) error
DeployApp(ctx context.Context, input app.DeployAppInput) (app.DeployAppOutput, error)
DeployEvents(ctx context.Context, input app.DeployEventsInput) error
}

var (
Expand Down Expand Up @@ -92,11 +93,22 @@ func Deploy(ctx context.Context, dir string, slug string, appName string, apps A
}

deployAppInput := app.DeployAppInput(appVersionOutput)
_, err = apps.DeployApp(ctx, deployAppInput)
deployAppOutput, err := apps.DeployApp(ctx, deployAppInput)
if err != nil {
output.PrintErrorDetails("Error deploying app", err)
}

// TODO: show deploy progress
input := app.DeployEventsInput{
DeploymentVersionID: deployAppOutput.DeploymentVersionID,
Handler: func(de app.DeployEvent) bool {
println("DEPLOY:", de.Message)
return true
},
}
err = apps.DeployEvents(ctx, input)
if err != nil {
output.PrintErrorDetails("Error receiving deploy logs", err)
}

return nil
}
4 changes: 3 additions & 1 deletion cli/cmd/deploy/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func TestDeploy(t *testing.T) {
const appVersionID = "app-version-id"
const uploadURL = "https://upload/url"
const deployVersionID = "deploy-version-id"
t.Run("happy path", func(t *testing.T) {

t.Run("happy path can run", func(t *testing.T) {
dir := t.TempDir()
copyTo(t, "../../testdata/streamlit_app", dir)

Expand All @@ -33,6 +34,7 @@ func TestDeploy(t *testing.T) {
apps.On("AppVersionUploadURL", mock.Anything, mock.Anything).Return(app.AppVersionUploadURLOutput{UploadURL: uploadURL}, nil)
apps.On("UploadAppSource", mock.Anything, mock.Anything).Return(nil)
apps.On("DeployApp", mock.Anything, mock.Anything).Return(app.DeployAppOutput{DeploymentVersionID: deployVersionID}, nil)
apps.On("DeployEvents", mock.Anything, mock.Anything).Return(nil)

err := Deploy(context.TODO(), dir, slug, appName, apps)

Expand Down
6 changes: 6 additions & 0 deletions cli/cmd/deploy/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type mockAppService struct {
mock.Mock
}

// DeployEvents implements AppService.
func (m *mockAppService) DeployEvents(ctx context.Context, input app.DeployEventsInput) error {
args := m.Called(ctx, input)
return args.Error(0)
}

// DeployApp implements AppService.
func (m *mockAppService) DeployApp(ctx context.Context, input app.DeployAppInput) (app.DeployAppOutput, error) {
args := m.Called(ctx, input)
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/log/log_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func printVerbose(out io.Writer, entry LogEntry, timestamps, verbose bool) {

func getClient() SubscriptionClient {
var previousError error
client := gql.GetSubscriptionClient()
client := gql.NewSubscriptionClient()
client = client.OnError(func(sc *graphql.SubscriptionClient, err error) error {
if previousError != nil {
output.PrintError(
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/push/build_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func buildEventSubscription(client subscriptionClient, buildID string, appPath s
func getClient() *graphql.SubscriptionClient {
var previousError error

client := gql.GetSubscriptionClient()
client := gql.NewSubscriptionClient()
client = client.OnError(func(sc *graphql.SubscriptionClient, err error) error {
if previousError != nil {
fmt.Printf("Error occurred listening for deploy logs. This does not mean that you app will be unavailable.\nFirst error: %s\nSecond error: %s\n", previousError, err)
Expand Down
4 changes: 2 additions & 2 deletions cli/internal/app/deploy_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type DeployEventMessage struct {
}

type DeployEventsSubscription struct {
AppDeployLogs DeployEvent `graphql:"appDeployLogs(appDeploymentID: $deployVersionID)"`
AppDeployEvents DeployEvent `graphql:"appDeployEvents(appDeploymentVersionID: $deployVersionID)"`
}

type GraphQLID string
Expand All @@ -52,7 +52,7 @@ func (s *Service) DeployEvents(ctx context.Context, input DeployEventsInput) err
return err
}

ok := input.Handler(DeployEvent{Message: value.AppDeployLogs.Message})
ok := input.Handler(DeployEvent{Message: value.AppDeployEvents.Message})
if !ok {
return graphql.ErrSubscriptionStopped
}
Expand Down
6 changes: 3 additions & 3 deletions cli/internal/app/deploy_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func TestDeployEvents(t *testing.T) {
DeploymentVersionID: "some-id",
}
err := s.DeployEvents(context.TODO(), input)
ch <- test.SubMessage{Msg: `{"appDeployLogs": {"message": "message 1"}}`}
ch <- test.SubMessage{Msg: `{"appDeployLogs": {"message": "message 2"}}`}
ch <- test.SubMessage{Msg: `{"appDeployLogs": {"message": "message 3"}}`}
ch <- test.SubMessage{Msg: `{"appDeployEvents": {"message": "message 1"}}`}
ch <- test.SubMessage{Msg: `{"appDeployEvents": {"message": "message 2"}}`}
ch <- test.SubMessage{Msg: `{"appDeployEvents": {"message": "message 3"}}`}
close(ch)

expected := []DeployEvent{
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/gql/subscription_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hasura/go-graphql-client"
)

func GetSubscriptionClient() *graphql.SubscriptionClient {
func NewSubscriptionClient() *graphql.SubscriptionClient {
client := graphql.NewSubscriptionClient(wsURL)

accessToken := getAccessToken()
Expand Down
2 changes: 1 addition & 1 deletion shared/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ extend type Mutation {
}

extend type Subscription {
appDeployLogs(appDeploymentID: ID!): LogMessage!
appDeployEvents(appDeploymentVersionID: ID!): LogMessage!
}

###############################################################################
Expand Down

0 comments on commit bd2eb6c

Please sign in to comment.