Treat default weblinks same as any other weblinks (#3385)

- Use seenWeblink and check for non-arrays

- Spread out link validation

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-07-22 20:28:01 -04:00
1 parent 0204eef848
commit da53aa0d79
6 files changed
+132 -55

No files matched your search

+56 -52
View File
@@ -19,43 +19,19 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { observable, reaction } from "mobx";
import { computed, observable, reaction } from "mobx";
import { WeblinkStore } from "../../common/weblink-store";
import { WebLink } from "../../common/catalog-entities";
import { catalogEntityRegistry } from "../catalog";
import got from "got";
import logger from "../logger";
import { docsUrl, slackUrl } from "../../common/vars";
const defaultLinks = [
{ title: "Lens Website", url: "https://k8slens.dev" },
{ title: "Lens Documentation", url: docsUrl },
{ title: "Lens Community Slack", url: slackUrl },
{ title: "Kubernetes Documentation", url: "https://kubernetes.io/docs/home/" },
{ title: "Lens on Twitter", url: "https://twitter.com/k8slens" },
{ title: "Lens Official Blog", url: "https://medium.com/k8slens" }
].map((link) => (
new WebLink({
metadata: {
uid: link.url,
name: link.title,
source: "app",
labels: {}
},
spec: {
url: link.url
},
status: {
phase: "available",
active: true
}
})
));
import type { Disposer } from "../../common/utils";
import { random } from "lodash";
async function validateLink(link: WebLink) {
try {
const response = await got.get(link.spec.url, {
throwHttpErrors: false
throwHttpErrors: false,
timeout: 20_000,
});
if (response.statusCode >= 200 && response.statusCode < 500) {
@@ -63,7 +39,7 @@ async function validateLink(link: WebLink) {
} else {
link.status.phase = "unavailable";
}
} catch(error) {
} catch {
link.status.phase = "unavailable";
}
}
@@ -71,32 +47,60 @@ async function validateLink(link: WebLink) {
export function syncWeblinks() {
const weblinkStore = WeblinkStore.getInstance();
const weblinks = observable.array(defaultLinks);
const webLinkEntities = observable.map<string, [WebLink, Disposer]>();
function periodicallyCheckLink(link: WebLink): Disposer {
validateLink(link);
let interval: NodeJS.Timeout;
const timeout = setTimeout(() => {
interval = setInterval(() => validateLink(link), 60 * 60 * 1000); // every 60 minutes
}, random(0, 5 * 60 * 1000, false)); // spread out over 5 minutes
return () => {
clearTimeout(timeout);
clearInterval(interval);
};
}
reaction(() => weblinkStore.weblinks, (links) => {
weblinks.replace(links.map((data) => new WebLink({
metadata: {
uid: data.id,
name: data.name,
source: "local",
labels: {}
},
spec: {
url: data.url
},
status: {
phase: "available",
active: true
}
})));
weblinks.push(...defaultLinks);
const seenWeblinks = new Set<string>();
for (const link of weblinks) {
validateLink(link).catch((error) => {
logger.error(`failed to validate link ${link.spec.url}: %s`, error);
});
for (const weblinkData of links) {
seenWeblinks.add(weblinkData.id);
if (!webLinkEntities.has(weblinkData.id)) {
const link = new WebLink({
metadata: {
uid: weblinkData.id,
name: weblinkData.name,
source: "local",
labels: {}
},
spec: {
url: weblinkData.url
},
status: {
phase: "available",
active: true
}
});
webLinkEntities.set(weblinkData.id, [
link,
periodicallyCheckLink(link),
]);
}
}
// Stop checking and remove weblinks that are no longer in the store
for (const [weblinkId, [, disposer]] of webLinkEntities) {
if (!seenWeblinks.has(weblinkId)) {
disposer();
webLinkEntities.delete(weblinkId);
}
}
}, {fireImmediately: true});
catalogEntityRegistry.addObservableSource("weblinks", weblinks);
catalogEntityRegistry.addComputedSource("weblinks", computed(() => Array.from(webLinkEntities.values(), ([link]) => link)));
}