feat: signals plugins

next try after #18153 without any external dependencies and only
supporting websocket. missing tests and necessary documentation but will
work on those after initial comments if this would be proper way to go
forward. already planning for the notification plugins on top of this.

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-12-04 12:36:50 +02:00
parent 04d94dc3d0
commit 047beadd9d
38 changed files with 1308 additions and 7 deletions
@@ -0,0 +1,32 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createApiRef } from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
/** @public */
export const signalsApiRef = createApiRef<SignalsApi>({
id: 'plugin.signals.service',
});
/** @public */
export type SignalsApi = {
subscribe(
onMessage: (message: JsonObject, topic?: string) => void,
topic?: string,
): void;
unsubscribe(topic?: string): void;
};
@@ -0,0 +1,133 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SignalsApi } from './SignalsApi';
import { JsonObject } from '@backstage/types';
import { DiscoveryApi } from '@backstage/core-plugin-api';
/** @public */
export class SignalsClient implements SignalsApi {
static instance: SignalsClient | null = null;
private ws: WebSocket | null = null;
private discoveryApi: DiscoveryApi;
private cbs: Map<string, (message: JsonObject, topic?: string) => void> =
new Map();
private queue: JsonObject[] = [];
private reconnectTimeout: any;
static create(options: { discoveryApi: DiscoveryApi }) {
if (!SignalsClient.instance) {
SignalsClient.instance = new SignalsClient(options);
}
return SignalsClient.instance;
}
private constructor(options: { discoveryApi: DiscoveryApi }) {
this.discoveryApi = options.discoveryApi;
}
subscribe(
onMessage: (message: JsonObject, topic?: string) => void,
topic?: string,
): void {
const subscriptionTopic = topic ?? '*';
// Do not allow to subscribe to same topic multiple times
if (this.cbs.has(subscriptionTopic)) {
return;
}
this.cbs.set(subscriptionTopic, onMessage);
this.connect().then(() => {
this.send({ action: 'subscribe', topic });
});
}
unsubscribe(topic?: string): void {
const subscriptionTopic = topic ?? '*';
this.cbs.delete(subscriptionTopic);
this.send({ action: 'unsubscribe', topic });
}
private send(data?: JsonObject): void {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) {
if (data) {
this.queue.push(data);
}
return;
}
// First send queue
for (const msg of this.queue) {
this.ws!.send(JSON.stringify(msg));
}
this.queue = [];
if (data) {
this.ws!.send(JSON.stringify(data));
}
}
private async connect() {
if (this.ws) {
return;
}
const apiUrl = `${await this.discoveryApi.getBaseUrl('signals')}`;
const url = new URL(apiUrl);
url.protocol = url.protocol === 'http:' ? 'ws' : 'wss';
this.ws = new WebSocket(url.toString());
this.ws.onmessage = (data: MessageEvent) => {
try {
const json = JSON.parse(data.data) as JsonObject;
let cb = this.cbs.get('*');
if (json.topic) {
cb = this.cbs.get(json.topic as string);
}
if (cb) {
cb(json.message as JsonObject, json.topic as string);
}
} catch (e) {
// NOOP
}
};
this.ws.onerror = () => {
this.reconnect();
};
this.ws.onclose = () => {
this.reconnect();
};
while (this.ws.readyState !== WebSocket.OPEN) {
await new Promise(r => setTimeout(r, 10));
}
this.send();
}
private reconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
}
this.reconnectTimeout = setTimeout(() => {
if (this.ws) {
this.ws.close();
}
this.ws = null;
this.connect();
}, 5000);
}
}
+17
View File
@@ -0,0 +1,17 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './SignalsApi';
export * from './SignalsClient';
+16
View File
@@ -0,0 +1,16 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './useSignalsApi';
@@ -0,0 +1,37 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SignalsClient } from '../api';
import { discoveryApiRef, useApi } from '@backstage/core-plugin-api';
import { JSONObject } from '@apollo/explorer/src/helpers/types';
import { useEffect } from 'react';
/** @public */
export const useSignalsApi = (
onMessage: (message: JSONObject) => void,
topic?: string,
) => {
const discovery = useApi(discoveryApiRef);
const signals = SignalsClient.create({ discoveryApi: discovery });
useEffect(() => {
signals.subscribe(onMessage, topic);
}, [signals, onMessage, topic]);
useEffect(() => {
return () => {
signals.unsubscribe(topic);
};
}, [signals, topic]);
};
+18
View File
@@ -0,0 +1,18 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './api';
export * from './hooks';
+16
View File
@@ -0,0 +1,16 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '@testing-library/jest-dom';