Check for TL updates on server start

This commit is contained in:
Pavel Djundik 2020-01-02 12:54:47 +02:00
parent 304e8bf5b0
commit 0bdac63953
5 changed files with 30 additions and 18 deletions

View File

@ -1,7 +1,7 @@
<template> <template>
<div id="version-checker" :class="[$store.state.versionStatus]"> <div id="version-checker" :class="[$store.state.versionStatus]">
<p v-if="$store.state.versionStatus === 'loading'"> <p v-if="$store.state.versionStatus === 'loading'">
Checking for updates... Checking for updates
</p> </p>
<p v-if="$store.state.versionStatus === 'new-version'"> <p v-if="$store.state.versionStatus === 'new-version'">
The Lounge <b>{{ $store.state.versionData.latest.version }}</b> The Lounge <b>{{ $store.state.versionData.latest.version }}</b>
@ -35,7 +35,7 @@
</template> </template>
<template v-if="$store.state.versionStatus === 'error'"> <template v-if="$store.state.versionStatus === 'error'">
<p> <p>
Information about latest releases could not be retrieved. Information about latest release could not be retrieved.
</p> </p>
<button id="check-now" class="btn btn-small" @click="checkNow">Try again</button> <button id="check-now" class="btn btn-small" @click="checkNow">Try again</button>
@ -48,11 +48,6 @@ import socket from "../js/socket";
export default { export default {
name: "VersionChecker", name: "VersionChecker",
data() {
return {
status: "loading",
};
},
mounted() { mounted() {
if (!this.$store.state.versionData) { if (!this.$store.state.versionData) {
this.checkNow(); this.checkNow();

View File

@ -26,7 +26,7 @@
></div> ></div>
</template> </template>
<template v-else> <template v-else>
<p>Unable to retrieve releases from GitHub.</p> <p>Unable to retrieve changelog for current release from GitHub.</p>
<p> <p>
<a <a
:href=" :href="

View File

@ -24,6 +24,12 @@ socket.on("changelog", function(data) {
// When there is a button to refresh the checker available, display it when // When there is a button to refresh the checker available, display it when
// data is expired. Before that, server would return same information anyway. // data is expired. Before that, server would return same information anyway.
if (data.expiresAt) { if (data.expiresAt) {
setTimeout(() => store.commit("versionDataExpired", true), data.expiresAt - Date.now()); const expires = data.expiresAt - Date.now();
if (expires > 0) {
setTimeout(() => store.commit("versionDataExpired", true), expires);
} else {
store.commit("versionDataExpired", true);
}
} }
}); });

View File

@ -7,6 +7,7 @@ const pkg = require("../../package.json");
const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds const TIME_TO_LIVE = 15 * 60 * 1000; // 15 minutes, in milliseconds
module.exports = { module.exports = {
isUpdateAvailable: false,
fetch, fetch,
}; };
@ -17,8 +18,10 @@ const versions = {
}; };
async function fetch() { async function fetch() {
const time = Date.now();
// Serving information from cache // Serving information from cache
if (versions.current.changelog) { if (versions.expiresAt > time) {
return versions; return versions;
} }
@ -36,11 +39,8 @@ async function fetch() {
updateVersions(response); updateVersions(response);
// Emptying cached information after reaching said expiration date // Add expiration date to the data to send to the client for later refresh
setTimeout(() => { versions.expiresAt = time + TIME_TO_LIVE;
delete versions.current.changelog;
delete versions.latest;
}, TIME_TO_LIVE);
} catch (error) { } catch (error) {
log.error(`Failed to fetch changelog: ${error}`); log.error(`Failed to fetch changelog: ${error}`);
} }
@ -74,6 +74,8 @@ function updateVersions(response) {
// Find latest release or pre-release if current version is also a pre-release // Find latest release or pre-release if current version is also a pre-release
if (!release.prerelease || release.prerelease === prerelease) { if (!release.prerelease || release.prerelease === prerelease) {
module.exports.isUpdateAvailable = true;
versions.latest = { versions.latest = {
prerelease: release.prerelease, prerelease: release.prerelease,
version: release.tag_name, version: release.tag_name,
@ -84,7 +86,4 @@ function updateVersions(response) {
} }
} }
} }
// Add expiration date to the data to send to the client for later refresh
versions.expiresAt = Date.now() + TIME_TO_LIVE;
} }

View File

@ -241,6 +241,18 @@ module.exports = function(options = {}) {
} }
}); });
changelog.fetch().then((versionData) => {
if (!versionData.latest) {
return;
}
log.info(
`The Lounge ${colors.green(
versionData.latest.version
)} is available. Read more on GitHub: ${versionData.latest.url}`
);
});
return server; return server;
}; };