diff options
Diffstat (limited to 'server/lib/schedulers/plugins-check-scheduler.ts')
-rw-r--r-- | server/lib/schedulers/plugins-check-scheduler.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/server/lib/schedulers/plugins-check-scheduler.ts b/server/lib/schedulers/plugins-check-scheduler.ts new file mode 100644 index 000000000..9c60dbcd4 --- /dev/null +++ b/server/lib/schedulers/plugins-check-scheduler.ts | |||
@@ -0,0 +1,60 @@ | |||
1 | import { logger } from '../../helpers/logger' | ||
2 | import { AbstractScheduler } from './abstract-scheduler' | ||
3 | import { retryTransactionWrapper } from '../../helpers/database-utils' | ||
4 | import { SCHEDULER_INTERVALS_MS } from '../../initializers/constants' | ||
5 | import { CONFIG } from '../../initializers/config' | ||
6 | import { PluginModel } from '../../models/server/plugin' | ||
7 | import { chunk } from 'lodash' | ||
8 | import { getLatestPluginsVersion } from '../plugins/plugin-index' | ||
9 | import { compareSemVer } from '../../../shared/core-utils/miscs/miscs' | ||
10 | |||
11 | export class PluginsCheckScheduler extends AbstractScheduler { | ||
12 | |||
13 | private static instance: AbstractScheduler | ||
14 | |||
15 | protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.checkPlugins | ||
16 | |||
17 | private constructor () { | ||
18 | super() | ||
19 | } | ||
20 | |||
21 | protected async internalExecute () { | ||
22 | return retryTransactionWrapper(this.checkLatestPluginsVersion.bind(this)) | ||
23 | } | ||
24 | |||
25 | private async checkLatestPluginsVersion () { | ||
26 | if (CONFIG.PLUGINS.INDEX.ENABLED === false) return | ||
27 | |||
28 | logger.info('Checkin latest plugins version.') | ||
29 | |||
30 | const plugins = await PluginModel.listInstalled() | ||
31 | |||
32 | // Process 10 plugins in 1 HTTP request | ||
33 | const chunks = chunk(plugins, 10) | ||
34 | for (const chunk of chunks) { | ||
35 | // Find plugins according to their npm name | ||
36 | const pluginIndex: { [npmName: string]: PluginModel} = {} | ||
37 | for (const plugin of chunk) { | ||
38 | pluginIndex[PluginModel.buildNpmName(plugin.name, plugin.type)] = plugin | ||
39 | } | ||
40 | |||
41 | const npmNames = Object.keys(pluginIndex) | ||
42 | const results = await getLatestPluginsVersion(npmNames) | ||
43 | |||
44 | for (const result of results) { | ||
45 | const plugin = pluginIndex[result.npmName] | ||
46 | if (!result.latestVersion) continue | ||
47 | |||
48 | if (plugin.latestVersion !== result.latestVersion && compareSemVer(plugin.latestVersion, result.latestVersion) < 0) { | ||
49 | plugin.latestVersion = result.latestVersion | ||
50 | await plugin.save() | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | |||
55 | } | ||
56 | |||
57 | static get Instance () { | ||
58 | return this.instance || (this.instance = new this()) | ||
59 | } | ||
60 | } | ||