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'
10 export class PluginsCheckScheduler extends AbstractScheduler {
12 private static instance: AbstractScheduler
14 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.checkPlugins
16 private constructor () {
20 protected async internalExecute () {
21 return this.checkLatestPluginsVersion()
24 private async checkLatestPluginsVersion () {
25 if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
27 logger.info('Checking latest plugins version.')
29 const plugins = await PluginModel.listInstalled()
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
40 const npmNames = Object.keys(pluginIndex)
43 const results = await getLatestPluginsVersion(npmNames)
45 for (const result of results) {
46 const plugin = pluginIndex[result.npmName]
47 if (!result.latestVersion) continue
50 !plugin.latestVersion ||
51 (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0)
53 plugin.latestVersion = result.latestVersion
56 logger.info('Plugin %s has a new latest version %s.', result.npmName, plugin.latestVersion)
60 logger.error('Cannot get latest plugins version.', { npmNames, err })
65 static get Instance () {
66 return this.instance || (this.instance = new this())