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

View File

@ -26,7 +26,7 @@
></div>
</template>
<template v-else>
<p>Unable to retrieve releases from GitHub.</p>
<p>Unable to retrieve changelog for current release from GitHub.</p>
<p>
<a
: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
// data is expired. Before that, server would return same information anyway.
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
module.exports = {
isUpdateAvailable: false,
fetch,
};
@ -17,8 +18,10 @@ const versions = {
};
async function fetch() {
const time = Date.now();
// Serving information from cache
if (versions.current.changelog) {
if (versions.expiresAt > time) {
return versions;
}
@ -36,11 +39,8 @@ async function fetch() {
updateVersions(response);
// Emptying cached information after reaching said expiration date
setTimeout(() => {
delete versions.current.changelog;
delete versions.latest;
}, TIME_TO_LIVE);
// Add expiration date to the data to send to the client for later refresh
versions.expiresAt = time + TIME_TO_LIVE;
} catch (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
if (!release.prerelease || release.prerelease === prerelease) {
module.exports.isUpdateAvailable = true;
versions.latest = {
prerelease: release.prerelease,
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;
};