]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/schedulers/plugins-check-scheduler.ts
Only use basehref
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / plugins-check-scheduler.ts
1 import { logger } from '../../helpers/logger'
2 import { AbstractScheduler } from './abstract-scheduler'
3 import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
4 import { CONFIG } from '../../initializers/config'
5 import { PluginModel } from '../../models/server/plugin'
6 import { chunk } from 'lodash'
7 import { getLatestPluginsVersion } from '../plugins/plugin-index'
8 import { compareSemVer } from '../../../shared/core-utils/miscs/miscs'
9
10 export class PluginsCheckScheduler extends AbstractScheduler {
11
12 private static instance: AbstractScheduler
13
14 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.checkPlugins
15
16 private constructor () {
17 super()
18 }
19
20 protected async internalExecute () {
21 return this.checkLatestPluginsVersion()
22 }
23
24 private async checkLatestPluginsVersion () {
25 if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
26
27 logger.info('Checking latest plugins version.')
28
29 const plugins = await PluginModel.listInstalled()
30
31 // Process 10 plugins in 1 HTTP request
32 const chunks = chunk(plugins, 10)
33 for (const chunk of chunks) {
34 // Find plugins according to their npm name
35 const pluginIndex: { [npmName: string]: PluginModel} = {}
36 for (const plugin of chunk) {
37 pluginIndex[PluginModel.buildNpmName(plugin.name, plugin.type)] = plugin
38 }
39
40 const npmNames = Object.keys(pluginIndex)
41
42 try {
43 const results = await getLatestPluginsVersion(npmNames)
44
45 for (const result of results) {
46 const plugin = pluginIndex[result.npmName]
47 if (!result.latestVersion) continue
48
49 if (
50 !plugin.latestVersion ||
51 (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0)
52 ) {
53 plugin.latestVersion = result.latestVersion
54 await plugin.save()
55
56 logger.info('Plugin %s has a new latest version %s.', result.npmName, plugin.latestVersion)
57 }
58 }
59 } catch (err) {
60 logger.error('Cannot get latest plugins version.', { npmNames, err })
61 }
62 }
63 }
64
65 static get Instance () {
66 return this.instance || (this.instance = new this())
67 }
68 }