ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugins-check-scheduler.ts 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { compareSemVer } from '@peertube/peertube-core-utils'
  2. import chunk from 'lodash-es/chunk.js'
  3. import { logger } from '../../helpers/logger.js'
  4. import { CONFIG } from '../../initializers/config.js'
  5. import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants.js'
  6. import { PluginModel } from '../../models/server/plugin.js'
  7. import { Notifier } from '../notifier/index.js'
  8. import { getLatestPluginsVersion } from '../plugins/plugin-index.js'
  9. import { AbstractScheduler } from './abstract-scheduler.js'
  10. export class PluginsCheckScheduler extends AbstractScheduler {
  11. private static instance: AbstractScheduler
  12. protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.CHECK_PLUGINS
  13. private constructor () {
  14. super()
  15. }
  16. protected async internalExecute () {
  17. return this.checkLatestPluginsVersion()
  18. }
  19. private async checkLatestPluginsVersion () {
  20. if (CONFIG.PLUGINS.INDEX.ENABLED === false) return
  21. logger.info('Checking latest plugins version.')
  22. const plugins = await PluginModel.listInstalled()
  23. // Process 10 plugins in 1 HTTP request
  24. const chunks = chunk(plugins, 10)
  25. for (const chunk of chunks) {
  26. // Find plugins according to their npm name
  27. const pluginIndex: { [npmName: string]: PluginModel } = {}
  28. for (const plugin of chunk) {
  29. pluginIndex[PluginModel.buildNpmName(plugin.name, plugin.type)] = plugin
  30. }
  31. const npmNames = Object.keys(pluginIndex)
  32. try {
  33. const results = await getLatestPluginsVersion(npmNames)
  34. for (const result of results) {
  35. const plugin = pluginIndex[result.npmName]
  36. if (!result.latestVersion) continue
  37. if (
  38. !plugin.latestVersion ||
  39. (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0)
  40. ) {
  41. plugin.latestVersion = result.latestVersion
  42. await plugin.save()
  43. // Notify if there is an higher plugin version available
  44. if (compareSemVer(plugin.version, result.latestVersion) < 0) {
  45. Notifier.Instance.notifyOfNewPluginVersion(plugin)
  46. }
  47. logger.info('Plugin %s has a new latest version %s.', result.npmName, plugin.latestVersion)
  48. }
  49. }
  50. } catch (err) {
  51. logger.error('Cannot get latest plugins version.', { npmNames, err })
  52. }
  53. }
  54. }
  55. static get Instance () {
  56. return this.instance || (this.instance = new this())
  57. }
  58. }