Skip to content

Commit

Permalink
improved progress hub signalr handling
Browse files Browse the repository at this point in the history
- fixed reconnecting with old auth token
  • Loading branch information
sei-aschlackman committed Feb 20, 2024
1 parent 0c235ce commit 5dc1455
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
5 changes: 1 addition & 4 deletions src/app/components/options-bar/options-bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,7 @@ export class OptionsBarComponent implements OnInit, OnDestroy {
this.tasksInProgress = <Array<NotificationData>>data;
}
});
this.notificationService.connectToProgressHub(
this.vmId,
this.authService.getAuthorizationToken(),
);
this.notificationService.connectToProgressHub(this.vmId);

this.inFrame = this.inIframe();

Expand Down
61 changes: 50 additions & 11 deletions src/app/services/notification/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,60 @@ import * as signalR from '@microsoft/signalr';
import { BehaviorSubject } from 'rxjs';
import { BASE_PATH } from '../../generated/vm-api';
import { NotificationData } from '../../models/notification/notification-model';
import { ComnAuthService } from '@cmusei/crucible-common';

@Injectable()
export class NotificationService {
private hubConnection: signalR.HubConnection;
private connectionPromise: Promise<void>;

private vmId: string;
private apiUrl: string;
public progressConnection: signalR.HubConnection;

public tasksInProgress = new BehaviorSubject<Array<NotificationData>>(
new Array<NotificationData>(),
);

constructor(@Inject(BASE_PATH) basePath: string) {
constructor(
private authService: ComnAuthService,
@Inject(BASE_PATH) basePath: string,
) {
this.apiUrl = basePath;

this.authService.user$.subscribe(() => {
this.reconnect();
});
}

connectToProgressHub(vmString: string, userToken: string) {
connectToProgressHub(vmId: string) {
this.vmId = vmId;

if (this.connectionPromise) {
return this.connectionPromise;
}

console.log('Starting connection to ProgressHub');
this.progressConnection = new signalR.HubConnectionBuilder()
.withUrl(`${this.apiUrl}/hubs/progress?access_token=${userToken}`)
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl(`${this.apiUrl}/hubs/progress`, {
accessTokenFactory: () => {
return this.authService.getAuthorizationToken();
},
})
.withAutomaticReconnect(new RetryPolicy(60, 0, 5))
.build();

this.progressConnection.on('Progress', (data: [NotificationData]) => {
this.hubConnection.on('Progress', (data: [NotificationData]) => {
this.tasksInProgress.next(data);
});

this.progressConnection.on('Complete', (data: [NotificationData]) => {
this.hubConnection.on('Complete', (data: [NotificationData]) => {
this.tasksInProgress.next(data);
});

this.progressConnection
.start()
this.connectionPromise = this.hubConnection.start();
this.connectionPromise
.then(() => {
this.progressConnection.invoke('Join', vmString);
console.log('Progress connection started');
this.joinGroups();
})
.catch(() => {
console.log(
Expand All @@ -48,6 +69,24 @@ export class NotificationService {
'Error while establishing Progress connection with the VM Console API.',
);
});

return this.connectionPromise;
}

private joinGroups() {
if (this.vmId) {
this.hubConnection.invoke('Join', this.vmId);
console.log('Progress connection started');
}
}

private reconnect() {
if (this.hubConnection != null) {
this.hubConnection.stop().then(() => {
this.connectionPromise = this.hubConnection.start();
this.connectionPromise.then(() => this.joinGroups());
});
}
}
}

Expand Down

0 comments on commit 5dc1455

Please sign in to comment.