1
0
Fork 0
peertube/server/lib/schedulers/plugins-check-scheduler.ts

75 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-07-26 09:04:37 -04:00
import { chunk } from 'lodash'
import { compareSemVer } from '@shared/core-utils'
import { logger } from '../../helpers/logger'
import { CONFIG } from '../../initializers/config'
2021-07-26 09:04:37 -04:00
import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants'
import { PluginModel } from '../../models/server/plugin'
2021-03-11 10:54:52 -05:00
import { Notifier } from '../notifier'
2021-07-26 09:04:37 -04:00
import { getLatestPluginsVersion } from '../plugins/plugin-index'
import { AbstractScheduler } from './abstract-scheduler'
export class PluginsCheckScheduler extends AbstractScheduler {
private static instance: AbstractScheduler
protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.checkPlugins
private constructor () {
super()
}
protected async internalExecute () {
2019-07-16 08:52:24 -04:00
return this.checkLatestPluginsVersion()
}
private async checkLatestPluginsVersion () {
if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
2019-07-16 08:52:24 -04:00
logger.info('Checking latest plugins version.')
const plugins = await PluginModel.listInstalled()
// Process 10 plugins in 1 HTTP request
const chunks = chunk(plugins, 10)
for (const chunk of chunks) {
// Find plugins according to their npm name
const pluginIndex: { [npmName: string]: PluginModel} = {}
for (const plugin of chunk) {
pluginIndex[PluginModel.buildNpmName(plugin.name, plugin.type)] = plugin
}
const npmNames = Object.keys(pluginIndex)
2019-07-16 08:52:24 -04:00
try {
const results = await getLatestPluginsVersion(npmNames)
2019-07-16 08:52:24 -04:00
for (const result of results) {
2020-01-31 10:56:52 -05:00
const plugin = pluginIndex[result.npmName]
2019-07-16 08:52:24 -04:00
if (!result.latestVersion) continue
if (
!plugin.latestVersion ||
(plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0)
) {
plugin.latestVersion = result.latestVersion
await plugin.save()
2021-03-11 10:54:52 -05:00
// Notify if there is an higher plugin version available
if (compareSemVer(plugin.version, result.latestVersion) < 0) {
Notifier.Instance.notifyOfNewPluginVersion(plugin)
}
logger.info('Plugin %s has a new latest version %s.', result.npmName, plugin.latestVersion)
2019-07-16 08:52:24 -04:00
}
}
2019-07-16 08:52:24 -04:00
} catch (err) {
logger.error('Cannot get latest plugins version.', { npmNames, err })
}
}
}
static get Instance () {
return this.instance || (this.instance = new this())
}
}