Skip to content

Commit

Permalink
fix(ui): fix sse when 401 occured (#3345)
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjjj authored and sguiheux committed Sep 21, 2018
1 parent 2a72618 commit 586dd0e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 35 deletions.
59 changes: 28 additions & 31 deletions engine/api/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,7 @@ func loadActionDependencies(db gorp.SqlExecutor, a *sdk.Action) error {
return fmt.Errorf("cannot loadActionChildren> %s", err)
}

// Requirements of children are requirement of parent
for _, c := range a.Actions {
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
computeRequirements(a)

return nil
}
Expand Down Expand Up @@ -268,21 +253,7 @@ func UpdateActionDB(db gorp.SqlExecutor, a *sdk.Action, userID int64) error {

//TODO we don't need to compute all job requirements here, but only when running the job
// Requirements of children are requirement of parent
for _, c := range a.Actions {
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
computeRequirements(a)

// Checks if multiple requirements have the same name
if err := isRequirementsValid(a.Requirements); err != nil {
Expand Down Expand Up @@ -522,3 +493,29 @@ func GetPipelineUsingAction(db gorp.SqlExecutor, name string) ([]PipelineUsingAc

return response, nil
}

func computeRequirements(a *sdk.Action) {
if a.Enabled {
// Requirements of children are requirement of parent
for _, c := range a.Actions {
if !c.Enabled { // If action is not enabled we don't need their requirements
continue
}
// Now for each requirement of child, check if it exists in parent
for _, cr := range c.Requirements {
found := false
for _, pr := range a.Requirements {
if pr.Type == cr.Type && pr.Value == cr.Value {
found = true
break
}
}
if !found {
a.Requirements = append(a.Requirements, cr)
}
}
}
} else {
a.Requirements = []sdk.Requirement{}
}
}
1 change: 1 addition & 0 deletions engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ func (api *API) InitRouter() {

// Users
r.Handle("/user", r.GET(api.getUsersHandler))
r.Handle("/user/me", r.GET(api.getUserMeHandler))
r.Handle("/user/favorite", r.POST(api.postUserFavoriteHandler))
r.Handle("/user/timeline", r.GET(api.getTimelineHandler))
r.Handle("/user/timeline/filter", r.GET(api.getTimelineFilterHandler), r.POST(api.postTimelineFilterHandler))
Expand Down
14 changes: 14 additions & 0 deletions engine/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"database/sql"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -158,6 +159,19 @@ func (api *API) getUsersHandler() service.Handler {
}
}

// getUserMeHandler fetches current user data
func (api *API) getUserMeHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
u := getUser(ctx)
if u == nil {
return fmt.Errorf("getUserMeHandler> user is nil")
}
u.Groups = nil
u.Permissions = sdk.UserPermissions{}
return service.WriteJSON(w, *u, http.StatusOK)
}
}

func (api *API) getTimelineFilterHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
u := getUser(ctx)
Expand Down
2 changes: 1 addition & 1 deletion engine/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func WriteError(w http.ResponseWriter, r *http.Request, err error) {
// ErrAlreadyTaken and ErrWorkerModelAlreadyBooked are not useful to log in warning
if sdk.ErrorIs(errProcessed, sdk.ErrAlreadyTaken) ||
sdk.ErrorIs(errProcessed, sdk.ErrWorkerModelAlreadyBooked) ||
sdk.ErrorIs(errProcessed, sdk.ErrJobAlreadyBooked) {
sdk.ErrorIs(errProcessed, sdk.ErrJobAlreadyBooked) || r.URL.Path == "/user/me" {
log.Debug("%-7s | %-4d | %s \t %s", r.Method, errProcessed.Status, r.RequestURI, err)
} else {
log.Warning("%-7s | %-4d | %s \t %s", r.Method, errProcessed.Status, r.RequestURI, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/clictl_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ testcases:
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Disabled > ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_Enabled > ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported
- script: {{.cds.build.cdsctl}} action export CDS_TestIT_GitClone > ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Disabled.exported ./tests/fixtures/action_disabled_expected.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_Enabled.exported ./tests/fixtures/action_enabled.yml
- script: diff ./tests/fixtures/clictl_action_CDS_TestIT_GitClone.exported ./tests/fixtures/action_git_clone_default.yml
- name: action list
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/action_disabled_expected.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: v1.0
name: CDS_TestIT_Disabled
enabled: false
steps:
- name: my empty script
script:
- '#!/bin/bash'
- set -e
- ""

2 changes: 1 addition & 1 deletion ui/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class AppComponent implements OnInit {
urlSubscribe: environment.apiURL + '/events/subscribe',
urlUnsubscribe: environment.apiURL + 'events/unsubscribe',
sseURL: environment.apiURL + '/events',
pingURL: environment.apiURL + '/mon/version'
pingURL: environment.apiURL + '/user/me'
});
this.sseWorker.response().subscribe(e => {
if (e == null) {
Expand Down
1 change: 1 addition & 0 deletions ui/src/app/service/logout.interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class LogoutInterceptor implements HttpInterceptor {

if (navigationExtras.queryParams.redirect) {
this._router.navigate(['/account/login'], navigationExtras);
this._authStore.removeUser();
}
} else {
// error formatted from CDS API
Expand Down
2 changes: 2 additions & 0 deletions ui/src/assets/worker/sharedWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ setInterval(() => {
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', pingUrl , false, null, null);
xhr.setRequestHeader(headerKey, headerValue);
xhr.send(null);
if (xhr.status >= 400) {
if (!offline) {
Expand All @@ -57,6 +58,7 @@ setInterval(() => {
}
}
} catch (e) {
console.error(e);
if (!offline) {
console.log('Closing SSE');
sse.close();
Expand Down
36 changes: 35 additions & 1 deletion ui/src/assets/worker/webWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ importScripts('./common.js');
var sse;
var headAuthKey;
var headAuthValue;
var pingUrl;
var offline = false;
onmessage = function (e) {
if (!sse && e.data.sseURL) {
pingUrl = e.data.pingURL;
headAuthKey = e.data.headAuthKey;
headAuthValue = e.data.headAuthValue;
sse = connectSSE(e.data.sseURL, e.data.headAuthKey, e.data.headAuthValue);
Expand All @@ -20,4 +23,35 @@ onmessage = function (e) {
console.log('SSE Error: ', err);
}
}
};
};

setInterval(() => {
if (pingUrl) {
try {
var xhr = new XMLHttpRequest();
xhr.open('GET', pingUrl , false, null, null);
xhr.setRequestHeader(headAuthKey, headAuthValue);
xhr.send(null);
if (xhr.status >= 400) {
if (!offline) {
console.log('Closing SSE');
sse.close();
offline = true;
}
} else {
if (offline) {
initSSE(true);
offline = false;
}
}
} catch (e) {
console.error(e);
if (!offline) {
console.log('Closing SSE');
sse.close();
offline = true;
}
}

}
}, 5000);

0 comments on commit 586dd0e

Please sign in to comment.