Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Check latest plugins version

Chocobozzze0ce715a1ded
diff --git a/server.ts b/server.ts
index f6fae3718f..67abf4b5ce 100644
--- a/server.ts
+++ b/server.ts
@@ -113,6 +113,7 @@ import { RemoveOldHistoryScheduler } from './server/lib/schedulers/remove-old-hi
import { isHTTPSignatureDigestValid } from './server/helpers/peertube-crypto'
import { PeerTubeSocket } from './server/lib/peertube-socket'
import { updateStreamingPlaylistsInfohashesIfNeeded } from './server/lib/hls'
+import { PluginsCheckScheduler } from './server/lib/schedulers/plugins-check-scheduler'
// ----------- Command line -----------
@@ -250,6 +251,7 @@ async function startApplication () {
VideosRedundancyScheduler.Instance.enable()
RemoveOldHistoryScheduler.Instance.enable()
RemoveOldViewsScheduler.Instance.enable()
+ PluginsCheckScheduler.Instance.enable()
// Redis initialization
Redis.Instance.init()
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts
index 114cc49b6b..bb410e8917 100644
--- a/server/controllers/api/plugins.ts
+++ b/server/controllers/api/plugins.ts
@@ -180,5 +180,11 @@ async function listAvailablePlugins (req: express.Request, res: express.Response
const resultList = await listAvailablePluginsFromIndex(query)
+ if (!resultList) {
+ return res.status(503)
+ .json({ error: 'Plugin index unavailable. Please retry later' })
+ .end()
+ }
+
return res.json(resultList)
}
diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts
index 2e30c94a1e..80476d6494 100644
--- a/server/helpers/requests.ts
+++ b/server/helpers/requests.ts
@@ -1,18 +1,22 @@
import * as Bluebird from 'bluebird'
import { createWriteStream, remove } from 'fs-extra'
import * as request from 'request'
-import { ACTIVITY_PUB } from '../initializers/constants'
+import { ACTIVITY_PUB, WEBSERVER } from '../initializers/constants'
import { processImage } from './image-utils'
import { join } from 'path'
import { logger } from './logger'
import { CONFIG } from '../initializers/config'
+const packageJSON = require('../../../package.json')
+
function doRequest <T> (
requestOptions: request.CoreOptions & request.UriOptions & { activityPub?: boolean },
bodyKBLimit = 1000 // 1MB
): Bluebird<{ response: request.RequestResponse, body: T }> {
+ if (!(requestOptions.headers)) requestOptions.headers = {}
+ requestOptions.headers['User-Agent'] = getUserAgent()
+
if (requestOptions.activityPub === true) {
- if (!Array.isArray(requestOptions.headers)) requestOptions.headers = {}
requestOptions.headers['accept'] = ACTIVITY_PUB.ACCEPT_HEADER
}
@@ -27,6 +31,9 @@ function doRequestAndSaveToFile (
destPath: string,
bodyKBLimit = 10000 // 10MB
) {
+ if (!requestOptions.headers) requestOptions.headers = {}
+ requestOptions.headers['User-Agent'] = getUserAgent()
+
return new Bluebird<void>((res, rej) => {
const file = createWriteStream(destPath)
file.on('finish', () => res())
@@ -60,6 +67,10 @@ async function downloadImage (url: string, destDir: string, destName: string, si
}
}
+function getUserAgent () {
+ return `PeerTube/${packageJSON.version} (+${WEBSERVER.URL})`
+}
+
// ---------------------------------------------------------------------------
export {
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 06e8c070b7..367ae8d458 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -618,6 +618,7 @@ if (isTestInstance() === true) {
SCHEDULER_INTERVALS_MS.removeOldHistory = 5000
SCHEDULER_INTERVALS_MS.removeOldViews = 5000
SCHEDULER_INTERVALS_MS.updateVideos = 5000
+ SCHEDULER_INTERVALS_MS.checkPlugins = 10000
REPEAT_JOBS[ 'videos-views' ] = { every: 5000 }
REDUNDANCY.VIDEOS.RANDOMIZED_FACTOR = 1
diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts
index 4a8a90ec8f..63cd47e635 100644
--- a/server/lib/plugins/plugin-index.ts
+++ b/server/lib/plugins/plugin-index.ts
@@ -27,13 +27,18 @@ async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList)
const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
- const { body } = await doRequest({ uri, qs, json: true })
+ try {
+ const { body } = await doRequest({ uri, qs, json: true })
- logger.debug('Got result from PeerTube index.', { body })
+ logger.debug('Got result from PeerTube index.', { body })
- await addInstanceInformation(body)
+ await addInstanceInformation(body)
- return body as ResultList<PeerTubePluginIndex>
+ return body as ResultList<PeerTubePluginIndex>
+ } catch (err) {
+ logger.error('Cannot list available plugins from index %s.', uri, { err })
+ return undefined
+ }
}
async function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
@@ -53,7 +58,7 @@ async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePlu
const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins/latest-version'
- const { body } = await doRequest({ uri, body: bodyRequest })
+ const { body } = await doRequest({ uri, body: bodyRequest, json: true, method: 'POST' })
return body
}
diff --git a/server/lib/schedulers/plugins-check-scheduler.ts b/server/lib/schedulers/plugins-check-scheduler.ts
index 9c60dbcd41..8dfdd51778 100644
--- a/server/lib/schedulers/plugins-check-scheduler.ts
+++ b/server/lib/schedulers/plugins-check-scheduler.ts
@@ -1,6 +1,5 @@
import { logger } from '../../helpers/logger'
import { AbstractScheduler } from './abstract-scheduler'
-import { retryTransactionWrapper } from '../../helpers/database-utils'
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
import { CONFIG } from '../../initializers/config'
import { PluginModel } from '../../models/server/plugin'
@@ -19,13 +18,13 @@ export class PluginsCheckScheduler extends AbstractScheduler {
}
protected async internalExecute () {
- return retryTransactionWrapper(this.checkLatestPluginsVersion.bind(this))
+ return this.checkLatestPluginsVersion()
}
private async checkLatestPluginsVersion () {
if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
- logger.info('Checkin latest plugins version.')
+ logger.info('Checking latest plugins version.')
const plugins = await PluginModel.listInstalled()
@@ -39,19 +38,28 @@ export class PluginsCheckScheduler extends AbstractScheduler {
}
const npmNames = Object.keys(pluginIndex)
- const results = await getLatestPluginsVersion(npmNames)
- for (const result of results) {
- const plugin = pluginIndex[result.npmName]
- if (!result.latestVersion) continue
+ try {
+ const results = await getLatestPluginsVersion(npmNames)
- if (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0) {
- plugin.latestVersion = result.latestVersion
- await plugin.save()
+ for (const result of results) {
+ const plugin = pluginIndex[ result.npmName ]
+ if (!result.latestVersion) continue
+
+ if (
+ !plugin.latestVersion ||
+ (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0)
+ ) {
+ plugin.latestVersion = result.latestVersion
+ await plugin.save()
+
+ logger.info('Plugin %s has a new latest version %s.', PluginModel.buildNpmName(plugin.name, plugin.type), plugin.latestVersion)
+ }
}
+ } catch (err) {
+ logger.error('Cannot get latest plugins version.', { npmNames, err })
}
}
-
}
static get Instance () {