Переглянути джерело

Check latest plugins version

Chocobozzz 4 роки тому
батько
коміт
e0ce715a1d

+ 2 - 0
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()

+ 6 - 0
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)
 }

+ 13 - 2
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 {

+ 1 - 0
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

+ 10 - 5
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
 }

+ 19 - 11
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 () {