From e0ce715a1ded6e84c877004dae3e354c8716fb06 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Jul 2019 14:52:24 +0200 Subject: [PATCH] Check latest plugins version --- server.ts | 2 ++ server/controllers/api/plugins.ts | 6 ++++ server/helpers/requests.ts | 15 ++++++++-- server/initializers/constants.ts | 1 + server/lib/plugins/plugin-index.ts | 15 ++++++---- .../lib/schedulers/plugins-check-scheduler.ts | 30 ++++++++++++------- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/server.ts b/server.ts index f6fae3718..67abf4b5c 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 114cc49b6..bb410e891 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 2e30c94a1..80476d649 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 ( 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((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 06e8c070b..367ae8d45 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 4a8a90ec8..63cd47e63 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 + return body as ResultList + } catch (err) { + logger.error('Cannot list available plugins from index %s.', uri, { err }) + return undefined + } } async function addInstanceInformation (result: ResultList) { @@ -53,7 +58,7 @@ async function getLatestPluginsVersion (npmNames: string[]): Promise