]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/schedulers/plugins-check-scheduler.ts
Reduce AP context size on specific activities
[github/Chocobozzz/PeerTube.git] / server / lib / schedulers / plugins-check-scheduler.ts
CommitLineData
6702a1b2
C
1import { logger } from '../../helpers/logger'
2import { AbstractScheduler } from './abstract-scheduler'
6702a1b2
C
3import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
4import { CONFIG } from '../../initializers/config'
5import { PluginModel } from '../../models/server/plugin'
6import { chunk } from 'lodash'
7import { getLatestPluginsVersion } from '../plugins/plugin-index'
8import { compareSemVer } from '../../../shared/core-utils/miscs/miscs'
9
10export 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 () {
e0ce715a 21 return this.checkLatestPluginsVersion()
6702a1b2
C
22 }
23
24 private async checkLatestPluginsVersion () {
25 if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
26
e0ce715a 27 logger.info('Checking latest plugins version.')
6702a1b2
C
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)
6702a1b2 41
e0ce715a
C
42 try {
43 const results = await getLatestPluginsVersion(npmNames)
6702a1b2 44
e0ce715a 45 for (const result of results) {
a1587156 46 const plugin = pluginIndex[result.npmName]
e0ce715a
C
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
d133f385 56 logger.info('Plugin %s has a new latest version %s.', result.npmName, plugin.latestVersion)
e0ce715a 57 }
6702a1b2 58 }
e0ce715a
C
59 } catch (err) {
60 logger.error('Cannot get latest plugins version.', { npmNames, err })
6702a1b2
C
61 }
62 }
6702a1b2
C
63 }
64
65 static get Instance () {
66 return this.instance || (this.instance = new this())
67 }
68}