aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-24 13:07:33 +0200
committerChocobozzz <me@florianbigard.com>2018-09-24 13:38:39 +0200
commite5565833f62b97f62ea75eba5b479963ae78b873 (patch)
tree835793ce464f9666b0ceae79f3d278cc4e007b32 /server/initializers
parentd1a63fc7ac58a1db00d8ca4f43aadba02eb9b084 (diff)
downloadPeerTube-e5565833f62b97f62ea75eba5b479963ae78b873.tar.gz
PeerTube-e5565833f62b97f62ea75eba5b479963ae78b873.tar.zst
PeerTube-e5565833f62b97f62ea75eba5b479963ae78b873.zip
Improve redundancy: add 'min_lifetime' configuration
Diffstat (limited to 'server/initializers')
-rw-r--r--server/initializers/checker-after-init.ts115
-rw-r--r--server/initializers/checker-before-init.ts (renamed from server/initializers/checker.ts)110
-rw-r--r--server/initializers/constants.ts11
-rw-r--r--server/initializers/index.ts1
-rw-r--r--server/initializers/installer.ts2
5 files changed, 135 insertions, 104 deletions
diff --git a/server/initializers/checker-after-init.ts b/server/initializers/checker-after-init.ts
new file mode 100644
index 000000000..588526235
--- /dev/null
+++ b/server/initializers/checker-after-init.ts
@@ -0,0 +1,115 @@
1import * as config from 'config'
2import { promisify0, isProdInstance, parseDuration, isTestInstance } from '../helpers/core-utils'
3import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client'
6import { parse } from 'url'
7import { CONFIG } from './constants'
8import { logger } from '../helpers/logger'
9import { getServerActor } from '../helpers/utils'
10import { RecentlyAddedStrategy, VideosRedundancy } from '../../shared/models/redundancy'
11import { isArray } from '../helpers/custom-validators/misc'
12import { uniq } from 'lodash'
13
14async function checkActivityPubUrls () {
15 const actor = await getServerActor()
16
17 const parsed = parse(actor.url)
18 if (CONFIG.WEBSERVER.HOST !== parsed.host) {
19 const NODE_ENV = config.util.getEnv('NODE_ENV')
20 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
21
22 logger.warn(
23 'It seems PeerTube was started (and created some data) with another domain name. ' +
24 'This means you will not be able to federate! ' +
25 'Please use %s %s npm run update-host to fix this.',
26 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
27 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
28 )
29 }
30}
31
32// Some checks on configuration files
33// Return an error message, or null if everything is okay
34function checkConfig () {
35 const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
36
37 // NSFW policy
38 {
39 const available = [ 'do_not_list', 'blur', 'display' ]
40 if (available.indexOf(defaultNSFWPolicy) === -1) {
41 return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
42 }
43 }
44
45 // Redundancies
46 const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
47 if (isArray(redundancyVideos)) {
48 const available = [ 'most-views', 'trending', 'recently-added' ]
49 for (const r of redundancyVideos) {
50 if (available.indexOf(r.strategy) === -1) {
51 return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
52 }
53
54 // Lifetime should not be < 10 hours
55 if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
56 return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
57 }
58 }
59
60 const filtered = uniq(redundancyVideos.map(r => r.strategy))
61 if (filtered.length !== redundancyVideos.length) {
62 return 'Redundancy video entries should have unique strategies'
63 }
64
65 const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
66 if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
67 return 'Min views in recently added strategy is not a number'
68 }
69 }
70
71 if (isProdInstance()) {
72 const configStorage = config.get('storage')
73 for (const key of Object.keys(configStorage)) {
74 if (configStorage[key].startsWith('storage/')) {
75 logger.warn(
76 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
77 key
78 )
79 }
80 }
81 }
82
83 return null
84}
85
86// We get db by param to not import it in this file (import orders)
87async function clientsExist () {
88 const totalClients = await OAuthClientModel.countTotal()
89
90 return totalClients !== 0
91}
92
93// We get db by param to not import it in this file (import orders)
94async function usersExist () {
95 const totalUsers = await UserModel.countTotal()
96
97 return totalUsers !== 0
98}
99
100// We get db by param to not import it in this file (import orders)
101async function applicationExist () {
102 const totalApplication = await ApplicationModel.countTotal()
103
104 return totalApplication !== 0
105}
106
107// ---------------------------------------------------------------------------
108
109export {
110 checkConfig,
111 clientsExist,
112 usersExist,
113 applicationExist,
114 checkActivityPubUrls
115}
diff --git a/server/initializers/checker.ts b/server/initializers/checker-before-init.ts
index 5b068caa1..4f46d406a 100644
--- a/server/initializers/checker.ts
+++ b/server/initializers/checker-before-init.ts
@@ -1,78 +1,8 @@
1import * as config from 'config' 1import * as config from 'config'
2import { promisify0, isProdInstance } from '../helpers/core-utils' 2import { promisify0 } from '../helpers/core-utils'
3import { UserModel } from '../models/account/user'
4import { ApplicationModel } from '../models/application/application'
5import { OAuthClientModel } from '../models/oauth/oauth-client'
6import { parse } from 'url'
7import { CONFIG } from './constants'
8import { logger } from '../helpers/logger'
9import { getServerActor } from '../helpers/utils'
10import { RecentlyAddedStrategy, VideosRedundancy } from '../../shared/models/redundancy'
11import { isArray } from '../helpers/custom-validators/misc' 3import { isArray } from '../helpers/custom-validators/misc'
12import { uniq } from 'lodash'
13
14async function checkActivityPubUrls () {
15 const actor = await getServerActor()
16
17 const parsed = parse(actor.url)
18 if (CONFIG.WEBSERVER.HOST !== parsed.host) {
19 const NODE_ENV = config.util.getEnv('NODE_ENV')
20 const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
21
22 logger.warn(
23 'It seems PeerTube was started (and created some data) with another domain name. ' +
24 'This means you will not be able to federate! ' +
25 'Please use %s %s npm run update-host to fix this.',
26 NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
27 NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
28 )
29 }
30}
31 4
32// Some checks on configuration files 5// ONLY USE CORE MODULES IN THIS FILE!
33// Return an error message, or null if everything is okay
34function checkConfig () {
35 const defaultNSFWPolicy = config.get<string>('instance.default_nsfw_policy')
36
37 // NSFW policy
38 if ([ 'do_not_list', 'blur', 'display' ].indexOf(defaultNSFWPolicy) === -1) {
39 return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy
40 }
41
42 // Redundancies
43 const redundancyVideos = config.get<VideosRedundancy[]>('redundancy.videos.strategies')
44 if (isArray(redundancyVideos)) {
45 for (const r of redundancyVideos) {
46 if ([ 'most-views', 'trending', 'recently-added' ].indexOf(r.strategy) === -1) {
47 return 'Redundancy video entries should have "most-views" strategy instead of ' + r.strategy
48 }
49 }
50
51 const filtered = uniq(redundancyVideos.map(r => r.strategy))
52 if (filtered.length !== redundancyVideos.length) {
53 return 'Redundancy video entries should have unique strategies'
54 }
55
56 const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
57 if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
58 return 'Min views in recently added strategy is not a number'
59 }
60 }
61
62 if (isProdInstance()) {
63 const configStorage = config.get('storage')
64 for (const key of Object.keys(configStorage)) {
65 if (configStorage[key].startsWith('storage/')) {
66 logger.warn(
67 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
68 key
69 )
70 }
71 }
72 }
73
74 return null
75}
76 6
77// Check the config files 7// Check the config files
78function checkMissedConfig () { 8function checkMissedConfig () {
@@ -109,6 +39,14 @@ function checkMissedConfig () {
109 } 39 }
110 } 40 }
111 41
42 const redundancyVideos = config.get<any>('redundancy.videos.strategies')
43 if (isArray(redundancyVideos)) {
44 for (const r of redundancyVideos) {
45 if (!r.size) miss.push('redundancy.videos.strategies.size')
46 if (!r.min_lifetime) miss.push('redundancy.videos.strategies.min_lifetime')
47 }
48 }
49
112 const missingAlternatives = requiredAlternatives.filter( 50 const missingAlternatives = requiredAlternatives.filter(
113 set => !set.find(alternative => !alternative.find(key => !config.has(key))) 51 set => !set.find(alternative => !alternative.find(key => !config.has(key)))
114 ) 52 )
@@ -163,36 +101,10 @@ async function checkFFmpegEncoders (): Promise<Map<string, boolean>> {
163 } 101 }
164} 102}
165 103
166// We get db by param to not import it in this file (import orders)
167async function clientsExist () {
168 const totalClients = await OAuthClientModel.countTotal()
169
170 return totalClients !== 0
171}
172
173// We get db by param to not import it in this file (import orders)
174async function usersExist () {
175 const totalUsers = await UserModel.countTotal()
176
177 return totalUsers !== 0
178}
179
180// We get db by param to not import it in this file (import orders)
181async function applicationExist () {
182 const totalApplication = await ApplicationModel.countTotal()
183
184 return totalApplication !== 0
185}
186
187// --------------------------------------------------------------------------- 104// ---------------------------------------------------------------------------
188 105
189export { 106export {
190 checkConfig,
191 checkFFmpeg, 107 checkFFmpeg,
192 checkFFmpegEncoders, 108 checkFFmpegEncoders,
193 checkMissedConfig, 109 checkMissedConfig
194 clientsExist,
195 usersExist,
196 applicationExist,
197 checkActivityPubUrls
198} 110}
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts
index 03424ffb8..947cbda28 100644
--- a/server/initializers/constants.ts
+++ b/server/initializers/constants.ts
@@ -601,7 +601,6 @@ const MEMOIZE_TTL = {
601 601
602const REDUNDANCY = { 602const REDUNDANCY = {
603 VIDEOS: { 603 VIDEOS: {
604 EXPIRES_AFTER_MS: 48 * 3600 * 1000, // 2 days
605 RANDOMIZED_FACTOR: 5 604 RANDOMIZED_FACTOR: 5
606 } 605 }
607} 606}
@@ -750,10 +749,16 @@ function updateWebserverConfig () {
750 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP) 749 CONFIG.WEBSERVER.HOST = sanitizeHost(CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT, REMOTE_SCHEME.HTTP)
751} 750}
752 751
753function buildVideosRedundancy (objs: VideosRedundancy[]): VideosRedundancy[] { 752function buildVideosRedundancy (objs: any[]): VideosRedundancy[] {
754 if (!objs) return [] 753 if (!objs) return []
755 754
756 return objs.map(obj => Object.assign(obj, { size: bytes.parse(obj.size) })) 755 return objs.map(obj => {
756 return Object.assign(obj, {
757 minLifetime: parseDuration(obj.min_lifetime),
758 size: bytes.parse(obj.size),
759 minViews: obj.min_views
760 })
761 })
757} 762}
758 763
759function buildLanguages () { 764function buildLanguages () {
diff --git a/server/initializers/index.ts b/server/initializers/index.ts
index 332702774..fe9190a9c 100644
--- a/server/initializers/index.ts
+++ b/server/initializers/index.ts
@@ -1,6 +1,5 @@
1// Constants first, database in second! 1// Constants first, database in second!
2export * from './constants' 2export * from './constants'
3export * from './database' 3export * from './database'
4export * from './checker'
5export * from './installer' 4export * from './installer'
6export * from './migrator' 5export * from './migrator'
diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts
index 818bb04a2..c952ad46c 100644
--- a/server/initializers/installer.ts
+++ b/server/initializers/installer.ts
@@ -5,7 +5,7 @@ import { createApplicationActor, createUserAccountAndChannel } from '../lib/user
5import { UserModel } from '../models/account/user' 5import { UserModel } from '../models/account/user'
6import { ApplicationModel } from '../models/application/application' 6import { ApplicationModel } from '../models/application/application'
7import { OAuthClientModel } from '../models/oauth/oauth-client' 7import { OAuthClientModel } from '../models/oauth/oauth-client'
8import { applicationExist, clientsExist, usersExist } from './checker' 8import { applicationExist, clientsExist, usersExist } from './checker-after-init'
9import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants' 9import { CACHE, CONFIG, LAST_MIGRATION_VERSION } from './constants'
10import { sequelizeTypescript } from './database' 10import { sequelizeTypescript } from './database'
11import { remove, ensureDir } from 'fs-extra' 11import { remove, ensureDir } from 'fs-extra'