Skip to content

Server check guard change 7 x #4100

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

Draft
wants to merge 3 commits into
base: dspace-7_x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
import {
ThemedPageInternalServerErrorComponent
} from './page-internal-server-error/themed-page-internal-server-error.component';
import { ServerCheckGuard } from './core/server-check/server-check.guard';
import { ServerStatusGuard } from './core/server-check/server-status-guard.service';
import { MenuResolver } from './menu.resolver';
import { ThemedPageErrorComponent } from './page-error/themed-page-error.component';

Expand All @@ -49,7 +49,7 @@ import { ThemedPageErrorComponent } from './page-error/themed-page-error.compone
{
path: '',
canActivate: [AuthBlockingGuard],
canActivateChild: [ServerCheckGuard],
canActivateChild: [ServerStatusGuard],
resolve: [MenuResolver],
children: [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/core-state.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { JsonPatchOperationsState } from './json-patch/json-patch-operations.red
import { MetaTagState } from './metadata/meta-tag.reducer';
import { RouteState } from './services/route.reducer';
import { RequestState } from './data/request-state.model';
import { ServerStatusState } from './history/server-status.reducer';

/**
* The core sub-state in the NgRx store
Expand All @@ -22,6 +23,7 @@ export interface CoreState {
'cache/object-updates': ObjectUpdatesState;
'data/request': RequestState;
'history': HistoryState;
'serverStatus': ServerStatusState;
'index': MetaIndexState;
'auth': AuthState;
'json/patch': JsonPatchOperationsState;
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/core.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { historyReducer } from './history/history.reducer';
import { metaTagReducer } from './metadata/meta-tag.reducer';
import { CoreState } from './core-state.model';
import { serverStatusReducer } from './history/server-status.reducer';

export const coreReducers: ActionReducerMap<CoreState> = {
'bitstreamFormats': bitstreamFormatReducer,
Expand All @@ -22,6 +23,7 @@ export const coreReducers: ActionReducerMap<CoreState> = {
'cache/object-updates': objectUpdatesReducer,
'data/request': requestReducer,
'history': historyReducer,
'serverStatus': serverStatusReducer,
'index': indexReducer,
'auth': authReducer,
'json/patch': jsonPatchOperationsReducer,
Expand Down
18 changes: 14 additions & 4 deletions src/app/core/data/request.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import { RequestError } from './request-error.model';
import { RestRequestMethod } from './rest-request-method';
import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model';
import { RequestEntry } from './request-entry.model';
import { ServerStatusService } from '../server-check/server-status.service';

@Injectable()
export class RequestEffects {

execute = createEffect(() => this.actions$.pipe(
execute = createEffect(() => this.actions$.pipe(
ofType(RequestActionTypes.EXECUTE),
mergeMap((action: RequestExecuteAction) => {
return this.requestService.getByUUID(action.payload).pipe(
Expand All @@ -51,7 +52,14 @@ export class RequestEffects {
map((response: ParsedResponse) => new RequestSuccessAction(request.uuid, response.statusCode, response.link, response.unCacheableObject)),
catchError((error: RequestError) => {
if (hasValue(error.statusCode)) {
// if it's an error returned by the server, complete the request
// if it's an error returned by the server, check if the server is still running and update its status
// then navigate to the internal error page while still completing the request
this.serverStatusService.checkAndUpdateServerStatus(request, error)
.subscribe((isAvailable: boolean) => {
if (!isAvailable) {
this.serverStatusService.navigateToInternalServerErrorPage();
}
});
return [new RequestErrorAction(request.uuid, error.statusCode, error.message)];
} else {
// if it's a client side error, throw it
Expand All @@ -70,7 +78,7 @@ export class RequestEffects {
* This assumes that the server cached everything a negligible
* time ago, and will likely need to be revisited later
*/
fixTimestampsOnRehydrate = createEffect(() => this.actions$
fixTimestampsOnRehydrate = createEffect(() => this.actions$
.pipe(ofType(StoreActionTypes.REHYDRATE),
map(() => new ResetResponseTimestampsAction(new Date().getTime()))
));
Expand All @@ -81,6 +89,8 @@ export class RequestEffects {
private injector: Injector,
protected requestService: RequestService,
protected xsrfService: XSRFService,
) { }
protected serverStatusService: ServerStatusService,
) {
}

}
42 changes: 9 additions & 33 deletions src/app/core/data/root-data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { RootDataService } from './root-data.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import {
createSuccessfulRemoteDataObject$,
createFailedRemoteDataObject$
createSuccessfulRemoteDataObject$
} from '../../shared/remote-data.utils';
import { Observable } from 'rxjs';
import { RemoteData } from './remote-data';
import { Root } from './root.model';
import { cold } from 'jasmine-marbles';

describe('RootDataService', () => {
let service: RootDataService;
Expand All @@ -21,9 +19,9 @@ describe('RootDataService', () => {
halService = jasmine.createSpyObj('halService', {
getRootHref: rootEndpoint,
});
requestService = jasmine.createSpyObj('requestService', [
'setStaleByHref',
]);
requestService = jasmine.createSpyObj('halService', {
setStaleByHref: {},
});
service = new RootDataService(requestService, null, null, halService);

findByHrefSpy = spyOn(service as any, 'findByHref');
Expand All @@ -45,36 +43,14 @@ describe('RootDataService', () => {
});
});

describe('checkServerAvailability', () => {
let result$: Observable<boolean>;

it('should return observable of true when root endpoint is available', () => {
spyOn(service, 'findRoot').and.returnValue(createSuccessfulRemoteDataObject$<Root>({} as any));

result$ = service.checkServerAvailability();

expect(result$).toBeObservable(cold('(a|)', {
a: true
}));
});

it('should return observable of false when root endpoint is not available', () => {
spyOn(service, 'findRoot').and.returnValue(createFailedRemoteDataObject$<Root>('500'));

result$ = service.checkServerAvailability();

expect(result$).toBeObservable(cold('(a|)', {
a: false
}));
});

});

describe(`invalidateRootCache`, () => {
it(`should set the cached root request to stale`, () => {
describe('invalidateRootCache', () => {
it('should call setStaleByHrefSubstring with the root endpoint href', () => {
service.invalidateRootCache();

expect(halService.getRootHref).toHaveBeenCalled();
expect(requestService.setStaleByHref).toHaveBeenCalledWith(rootEndpoint);
});
});


});
18 changes: 1 addition & 17 deletions src/app/core/data/root-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import { ROOT } from './root.resource-type';
import { RequestService } from './request.service';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { Observable, of as observableOf } from 'rxjs';
import { Observable } from 'rxjs';
import { RemoteData } from './remote-data';
import { FollowLinkConfig } from '../../shared/utils/follow-link-config.model';
import { catchError, map } from 'rxjs/operators';
import { BaseDataService } from './base/base-data.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { dataService } from './base/data-service.decorator';
import { getFirstCompletedRemoteData } from '../shared/operators';

/**
* A service to retrieve the {@link Root} object from the REST API.
Expand All @@ -28,20 +26,6 @@ export class RootDataService extends BaseDataService<Root> {
super('', requestService, rdbService, objectCache, halService, 6 * 60 * 60 * 1000);
}

/**
* Check if root endpoint is available
*/
checkServerAvailability(): Observable<boolean> {
return this.findRoot().pipe(
catchError((err ) => {
console.error(err);
return observableOf(false);
}),
getFirstCompletedRemoteData(),
map((rootRd: RemoteData<Root>) => rootRd.statusCode === 200)
);
}

/**
* Find the {@link Root} object of the REST API
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
Expand Down
24 changes: 24 additions & 0 deletions src/app/core/history/server-status.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* eslint-disable max-classes-per-file */
import { Action } from '@ngrx/store';

import { type } from '../../shared/ngrx/type';

export const ServerStatusActionTypes = {
UPDATE_SERVER_STATUS: type('dspace/server-status/UPDATE_SERVER_STATUS'),
};


export class UpdateServerStatusAction implements Action {
type = ServerStatusActionTypes.UPDATE_SERVER_STATUS;
payload: {
isAvailable: boolean;
};

constructor(isAvailable: boolean) {
this.payload = {isAvailable};
}
}


export type ServerStatusAction
= UpdateServerStatusAction;
25 changes: 25 additions & 0 deletions src/app/core/history/server-status.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ServerStatusAction, ServerStatusActionTypes, UpdateServerStatusAction } from './server-status.actions';

/**
* The auth state.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface ServerStatusState {
isAvailable: boolean;
}

/**
* The initial state.
*/
const initialState: ServerStatusState = {isAvailable: true};

export function serverStatusReducer(state = initialState, action: ServerStatusAction): ServerStatusState {
switch (action.type) {
case ServerStatusActionTypes.UPDATE_SERVER_STATUS: {
return {isAvailable: (action as UpdateServerStatusAction).payload.isAvailable};
}
default: {
return state;
}
}
}
88 changes: 0 additions & 88 deletions src/app/core/server-check/server-check.guard.spec.ts

This file was deleted.

Loading