598 lines
22 KiB
TypeScript
598 lines
22 KiB
TypeScript
/**
|
|
* A PeerTube instance can be served by several Node.js processes sharing the same PostgreSQL and Redis
|
|
*
|
|
* - `primary` behaves like a regular PeerTube:
|
|
* - runs the migrations
|
|
* - runs all the schedulers
|
|
* - handle the live server
|
|
* - runs the tracker
|
|
* - runs every job worker
|
|
* - sends its configuration to secondary servers
|
|
* - `secondary` is an "help" process for the primary:
|
|
* - serves the API, the ActivityPub endpoints (including the inbox) and the files in object storage, and tracks views
|
|
* - answers 421 to the endpoints only the primary can serve (see the `primaryOnly` middleware), so the reverse proxy replays them on it
|
|
* - consumes an allow list of job types
|
|
* - never runs migrations
|
|
* - never runs schedulers that flush data to PostgreSQL
|
|
* - fetches a small subset of the configuration from a YAML file, and the complete configuration from the primary
|
|
* - requires object storage: it never uses the files of the primary on the file system
|
|
*
|
|
* Every process owns its storage directories.
|
|
*
|
|
* The role is read from `--role` on the command line, or from the `PEERTUBE_PROCESS_ROLE`
|
|
* It is parsed manually rather than commander because of ESM hoisting and configuration importation
|
|
*/
|
|
|
|
import { createCommand } from '@commander-js/extra-typings'
|
|
import { getServerCLIOptions, setServerCLIOptions } from './core/initializers/cli-options.js'
|
|
import { registerOpentelemetryTracing } from './core/lib/opentelemetry/tracing.js'
|
|
|
|
const options = createCommand()
|
|
.option('--no-client', 'Start PeerTube without client interface')
|
|
.option('--no-plugins', 'Start PeerTube without plugins/themes enabled')
|
|
.option('--benchmark-startup', 'Automatically stop server when initialized')
|
|
// Declared so commander does not reject it, but we read it directly from `process.argv`
|
|
// Because it's used to alter the config, that is built early in the initialization process (ESM hoisting)
|
|
.option(
|
|
'--role <role>',
|
|
'Process role: "primary" (default) runs the whole instance, "secondary" helps the primary to serve the requests'
|
|
)
|
|
.parse(process.argv)
|
|
.opts()
|
|
|
|
setServerCLIOptions({
|
|
client: options.client,
|
|
plugins: options.plugins,
|
|
benchmarkStartup: options.benchmarkStartup === true
|
|
})
|
|
|
|
await registerOpentelemetryTracing()
|
|
|
|
process.title = 'peertube'
|
|
|
|
// ----------- Core checker -----------
|
|
import { checkFFmpeg, checkMissedConfig, checkNodeVersion } from './core/initializers/checker-before-init.js'
|
|
|
|
// Do not use barrels because we don't want to load all modules here (we need to initialize database first)
|
|
import { initI18n, useI18n } from '@server/helpers/i18n.js'
|
|
import { createLogger, flushLogs } from './core/helpers/logger.js'
|
|
import { CONFIG } from './core/initializers/config.js'
|
|
import { API_VERSION, WEBSERVER, loadLanguages } from './core/initializers/constants.js'
|
|
|
|
const logger = createLogger()
|
|
|
|
const missed = checkMissedConfig()
|
|
if (missed.length !== 0) {
|
|
logger.error('Your configuration files miss keys: ' + missed)
|
|
process.exit(-1)
|
|
}
|
|
|
|
checkFFmpeg(CONFIG)
|
|
.catch(err => {
|
|
logger.error('Error in ffmpeg check.', { err })
|
|
process.exit(-1)
|
|
})
|
|
|
|
try {
|
|
checkNodeVersion()
|
|
} catch (err) {
|
|
logger.error('Error in NodeJS check.', { err })
|
|
process.exit(-1)
|
|
}
|
|
|
|
import {
|
|
checkActivityPubUrls,
|
|
checkConfig,
|
|
checkFFmpegVersion,
|
|
checkObjectStorageBucketsConnectivity
|
|
} from './core/initializers/checker-after-init.js'
|
|
|
|
try {
|
|
checkConfig()
|
|
} catch (err) {
|
|
logger.error('Config error.', { err })
|
|
process.exit(-1)
|
|
}
|
|
|
|
// ----------- Database -----------
|
|
|
|
// Initialize database and models
|
|
import { checkDatabaseConnectionOrDie, initDatabaseModels } from './core/initializers/database.js'
|
|
checkDatabaseConnectionOrDie()
|
|
|
|
import { migrate } from './core/initializers/migrator.js'
|
|
import { getProcessRole, isSecondaryProcess } from './core/initializers/process-role.js'
|
|
|
|
// Only the primary process migrates: two processes booting at the same time would race on the schema
|
|
const migrated = isSecondaryProcess()
|
|
? Promise.resolve()
|
|
: migrate()
|
|
|
|
migrated
|
|
.then(() => initDatabaseModels(false))
|
|
.then(() => startApplication())
|
|
.catch(err => {
|
|
logger.error('Cannot start application.', { err })
|
|
process.exit(-1)
|
|
})
|
|
|
|
// ----------- Initialize -----------
|
|
Promise.all([
|
|
initI18n(),
|
|
loadLanguages()
|
|
]).catch(err => logger.error('Cannot load i18n/languages', { err }))
|
|
|
|
// Express configuration
|
|
import cookieParser from 'cookie-parser'
|
|
import cors from 'cors'
|
|
import express from 'express'
|
|
import { frameguard, xContentTypeOptions } from 'helmet'
|
|
import anonymize from 'ip-anonymize'
|
|
import morgan, { token } from 'morgan'
|
|
|
|
const app = express().disable('x-powered-by')
|
|
|
|
// Trust our proxy (IP forwarding...)
|
|
app.set('trust proxy', CONFIG.TRUST_PROXY)
|
|
|
|
import { cacheRequestIp } from './core/middlewares/request-ip-cache.js'
|
|
app.use(cacheRequestIp)
|
|
|
|
// Tag every logger call made while handling a request with a request id, first so every other middleware benefits from it
|
|
import { requestLoggerContext } from './core/middlewares/logger-context.js'
|
|
app.use(requestLoggerContext)
|
|
|
|
// Database context shared by the middlewares and the controller of a request
|
|
import { requestDbContext } from './core/middlewares/replica-reads.js'
|
|
app.use(requestDbContext)
|
|
|
|
app.use((_req, res, next) => {
|
|
// OpenTelemetry
|
|
res.locals.requestStart = Date.now()
|
|
|
|
if (CONFIG.SECURITY.POWERED_BY_HEADER.ENABLED === true) {
|
|
res.setHeader('x-powered-by', 'PeerTube')
|
|
}
|
|
|
|
return next()
|
|
})
|
|
|
|
app.use(useI18n)
|
|
|
|
// Security middleware
|
|
import { baseCSP } from './core/middlewares/csp.js'
|
|
|
|
if (CONFIG.CSP.ENABLED) {
|
|
app.use(baseCSP)
|
|
}
|
|
|
|
app.use(xContentTypeOptions())
|
|
|
|
if (CONFIG.SECURITY.FRAMEGUARD.ENABLED) {
|
|
app.use(frameguard({
|
|
action: 'deny' // we only allow it for /videos/embed, see server/controllers/client.ts
|
|
}))
|
|
}
|
|
|
|
// ----------- PeerTube modules -----------
|
|
import { HttpStatusCode } from '@peertube/peertube-models'
|
|
import { isTestOrDevInstance } from '@peertube/peertube-node-utils'
|
|
import { OpenTelemetryMetrics } from '@server/lib/opentelemetry/metrics.js'
|
|
import { RemoveExpiredUserExportsScheduler } from '@server/lib/schedulers/remove-expired-user-exports-scheduler.js'
|
|
import { UpdateTokenSessionScheduler } from '@server/lib/schedulers/update-token-session-scheduler.js'
|
|
import { VideoChannelSyncLatestScheduler } from '@server/lib/schedulers/video-channel-sync-latest-scheduler.js'
|
|
import { ServerConfigManager } from '@server/lib/server-config-manager.js'
|
|
import { StatsManager } from '@server/lib/stat-manager.js'
|
|
import { VideoStatsManager } from '@server/lib/stats/video-stats-manager.js'
|
|
import { initUploadxStorages } from '@server/lib/uploadx.js'
|
|
import { ApplicationModel, clearServerActorCache } from '@server/models/application/application.js'
|
|
import { ModelCache } from '@server/models/shared/model-cache.js'
|
|
import { WatchedWordsListModel } from '@server/models/watched-words/watched-words-list.js'
|
|
import {
|
|
activityPubRouter,
|
|
apiRouter,
|
|
clientsRouter,
|
|
createWebsocketTrackerServer,
|
|
downloadRouter,
|
|
feedsRouter,
|
|
lazyStaticRouter,
|
|
miscRouter,
|
|
objectStorageProxyRouter,
|
|
pluginsRouter,
|
|
servicesRouter,
|
|
sitemapRouter,
|
|
staticRouter,
|
|
trackerRouter,
|
|
wellKnownRouter
|
|
} from './core/controllers/index.js'
|
|
import { isHTTPSignatureDigestValid } from './core/helpers/peertube-crypto.js'
|
|
import { ConfigDistribution } from './core/initializers/config/config-distribution.js'
|
|
import { installPrimary, installSecondary } from './core/initializers/installer.js'
|
|
import { TokensCache } from './core/lib/auth/tokens-cache.js'
|
|
import { initDatabaseReadReplicas } from './core/lib/database-replicas/database-replicas.js'
|
|
import { Emailer } from './core/lib/emailer.js'
|
|
import { resetFilesCacheOfOtherStorage } from './core/lib/files-cache/reset-files-cache.js'
|
|
import { updateStreamingPlaylistsInfohashesIfNeeded } from './core/lib/hls.js'
|
|
import { HorizontalScalabilityStorage } from './core/lib/horizontal-scalability-storage.js'
|
|
import { ClientHtml } from './core/lib/html/client-html.js'
|
|
import { JobQueue } from './core/lib/job-queue/index.js'
|
|
import { LiveManager } from './core/lib/live/index.js'
|
|
import { PeerTubeSocket } from './core/lib/peertube-socket.js'
|
|
import { Hooks } from './core/lib/plugins/hooks.js'
|
|
import { PluginManager } from './core/lib/plugins/plugin-manager.js'
|
|
import { Redis } from './core/lib/redis/index.js'
|
|
import { removeRedundanciesOfOtherStorage } from './core/lib/redundancy.js'
|
|
import { ActorFollowScheduler } from './core/lib/schedulers/actor-follow-scheduler.js'
|
|
import { AutoFollowIndexInstances } from './core/lib/schedulers/auto-follow-index-instances.js'
|
|
import { BlocklistSubscriptionsScheduler } from './core/lib/schedulers/blocklist-subscriptions-scheduler.js'
|
|
import { GeoIPUpdateScheduler } from './core/lib/schedulers/geo-ip-update-scheduler.js'
|
|
import { LocalVideoStatsBufferScheduler } from './core/lib/schedulers/local-video-stats-buffer-scheduler.js'
|
|
import { ManualMigrationScriptsScheduler } from './core/lib/schedulers/manual-migration-scripts-scheduler.js'
|
|
import { PeerTubeVersionCheckScheduler } from './core/lib/schedulers/peertube-version-check-scheduler.js'
|
|
import { PluginsCheckScheduler } from './core/lib/schedulers/plugins-check-scheduler.js'
|
|
import { ProcessHeartbeatScheduler } from './core/lib/schedulers/process-heartbeat-scheduler.js'
|
|
import { RemoveDanglingResumableUploadsScheduler } from './core/lib/schedulers/remove-dangling-resumable-uploads-scheduler.js'
|
|
import { RemoveDanglingStagingFilesScheduler } from './core/lib/schedulers/remove-dangling-staging-files-scheduler.js'
|
|
import { RemoveOldAuditLogsScheduler } from './core/lib/schedulers/remove-old-audit-logs-scheduler.js'
|
|
import { RemoveOldHistoryScheduler } from './core/lib/schedulers/remove-old-history-scheduler.js'
|
|
import { RemoveOldStatsScheduler } from './core/lib/schedulers/remove-old-stats-scheduler.js'
|
|
import { RemoveOldUserLoginDevicesScheduler } from './core/lib/schedulers/remove-old-user-login-devices-scheduler.js'
|
|
import { RunnerJobWatchDogScheduler } from './core/lib/schedulers/runner-job-watch-dog-scheduler.js'
|
|
import { UpdateVideosScheduler } from './core/lib/schedulers/update-videos-scheduler.js'
|
|
import { VideoFilesLifecycleScheduler } from './core/lib/schedulers/video-files-lifecycle-scheduler.js'
|
|
import { VideosRedundancyScheduler } from './core/lib/schedulers/videos-redundancy-scheduler.js'
|
|
import { WatchedWordsSubscriptionsScheduler } from './core/lib/schedulers/watched-words-subscriptions-scheduler.js'
|
|
import { YoutubeDlUpdateScheduler } from './core/lib/schedulers/youtube-dl-update-scheduler.js'
|
|
import { registerGracefulShutdown, shutdownAndExit } from './core/lib/shutdown.js'
|
|
import { updateTorrentsTrackersIfNeeded } from './core/lib/torrent-trackers.js'
|
|
import { watchYoutubeDLCookies } from './core/lib/youtube-dl-cookies.js'
|
|
import { advertiseDoNotTrack } from './core/middlewares/dnt.js'
|
|
import { apiFailMiddleware } from './core/middlewares/error.js'
|
|
import { primaryOnly } from './core/middlewares/primary-only.js'
|
|
|
|
// ----------- App -----------
|
|
|
|
// Enable CORS for develop
|
|
if (isTestOrDevInstance()) {
|
|
app.use(cors({
|
|
origin: '*',
|
|
exposedHeaders: [ 'Retry-After', 'X-Request-Id' ]
|
|
}))
|
|
}
|
|
|
|
// HTTP logging
|
|
if (CONFIG.LOG.LOG_HTTP_REQUESTS) {
|
|
token('remote-addr', (req: express.Request) => {
|
|
if (CONFIG.LOG.ANONYMIZE_IP === true || req.get('DNT') === '1') {
|
|
return anonymize(req.ip, 16, 16)
|
|
}
|
|
|
|
return req.ip
|
|
})
|
|
|
|
app.use(morgan('combined', {
|
|
stream: {
|
|
write: (str: string) => logger.info(str.trim(), { tags: [ 'http' ] })
|
|
},
|
|
skip: req => CONFIG.LOG.LOG_PING_REQUESTS === false && req.originalUrl === '/api/v1/ping'
|
|
}))
|
|
}
|
|
|
|
// Add .fail() helper to response
|
|
app.use(apiFailMiddleware)
|
|
|
|
// For body requests
|
|
app.use(express.urlencoded({ extended: false }))
|
|
app.use(express.json({
|
|
type: [ 'application/json', 'application/*+json' ],
|
|
limit: '500kb',
|
|
verify: (req: express.Request, res: express.Response, buf: Buffer) => {
|
|
const valid = isHTTPSignatureDigestValid(buf, req)
|
|
|
|
if (valid !== true) {
|
|
const err: Error & { status?: number } = new Error('Invalid digest')
|
|
err.status = HttpStatusCode.FORBIDDEN_403
|
|
|
|
throw err
|
|
}
|
|
|
|
if (req.originalUrl.startsWith('/plugins/')) {
|
|
req.rawBody = buf
|
|
}
|
|
}
|
|
}))
|
|
|
|
// W3C DNT Tracking Status
|
|
app.use(advertiseDoNotTrack)
|
|
|
|
// ----------- Open Telemetry -----------
|
|
|
|
OpenTelemetryMetrics.Instance.init(app)
|
|
|
|
// ----------- Views, routes and static files -----------
|
|
|
|
const cliOptions = getServerCLIOptions()
|
|
|
|
const secondary = isSecondaryProcess()
|
|
|
|
app.use([ '/tracker', '/static' ], primaryOnly)
|
|
|
|
app.use('/api/' + API_VERSION, apiRouter)
|
|
|
|
// Services (oembed...)
|
|
app.use('/services', servicesRouter)
|
|
|
|
if (CONFIG.FEDERATION.ENABLED) {
|
|
app.use('/', activityPubRouter)
|
|
}
|
|
|
|
app.use('/', feedsRouter)
|
|
app.use('/', trackerRouter)
|
|
app.use('/', sitemapRouter)
|
|
|
|
// Static files
|
|
app.use('/', staticRouter)
|
|
app.use('/', wellKnownRouter)
|
|
app.use('/', miscRouter)
|
|
app.use('/', downloadRouter)
|
|
app.use('/', lazyStaticRouter)
|
|
app.use('/', objectStorageProxyRouter)
|
|
|
|
// Cookies for plugins and HTML
|
|
app.use(cookieParser())
|
|
|
|
// Plugins & themes
|
|
app.use('/', pluginsRouter)
|
|
|
|
// Client files, last valid routes!
|
|
if (cliOptions.client) app.use('/', clientsRouter)
|
|
|
|
// ----------- Errors -----------
|
|
|
|
// Catch unmatched routes
|
|
app.use((_req, res: express.Response) => {
|
|
res.status(HttpStatusCode.NOT_FOUND_404).end()
|
|
})
|
|
|
|
// Catch thrown errors
|
|
app.use((err, req, res: express.Response, _next) => {
|
|
// Handling Sequelize error traces
|
|
const sql = err?.parent ? err.parent.sql : undefined
|
|
|
|
// Help us to debug SequelizeConnectionAcquireTimeoutError errors
|
|
const activeRequests = err?.name === 'SequelizeConnectionAcquireTimeoutError' && typeof (process as any)._getActiveRequests === 'function'
|
|
? (process as any)._getActiveRequests()
|
|
: undefined
|
|
|
|
// Remove too big metadata and alter original error to keep type
|
|
err.body = undefined
|
|
|
|
logger.error('Error in controller.', { err, sql, activeRequests, url: req.originalUrl })
|
|
|
|
return res.fail({
|
|
status: err.status || HttpStatusCode.INTERNAL_SERVER_ERROR_500,
|
|
message: err.message,
|
|
type: err.name
|
|
})
|
|
})
|
|
|
|
const { server, trackerServer } = createWebsocketTrackerServer(app)
|
|
|
|
server.requestTimeout = CONFIG.HTTP_TIMEOUTS.REQUEST
|
|
|
|
// Register it before the application is started so the process can always be stopped, even during a long migration
|
|
registerGracefulShutdown(server)
|
|
|
|
// ----------- Run -----------
|
|
|
|
async function startApplication () {
|
|
const port = CONFIG.LISTEN.PORT
|
|
const hostname = CONFIG.LISTEN.HOSTNAME
|
|
|
|
if (!secondary) {
|
|
await installPrimary()
|
|
|
|
// Check activity pub urls are valid
|
|
checkActivityPubUrls()
|
|
.catch(err => {
|
|
logger.error('Error in ActivityPub URLs checker.', { err })
|
|
process.exit(-1)
|
|
})
|
|
} else {
|
|
await installSecondary()
|
|
}
|
|
|
|
checkFFmpegVersion()
|
|
.catch(err => logger.error('Cannot check ffmpeg version', { err }))
|
|
|
|
try {
|
|
await checkObjectStorageBucketsConnectivity()
|
|
} catch (err) {
|
|
logger.error('Cannot run with the current object storage configuration.', { err })
|
|
|
|
await flushLogs()
|
|
process.exit(1)
|
|
}
|
|
|
|
await initUploadxStorages()
|
|
|
|
Redis.Instance.init()
|
|
|
|
// Needs Redis, to know the last write position of users
|
|
initDatabaseReadReplicas()
|
|
|
|
// The primary publishes its configuration here, a secondary subscribes to the changes
|
|
// Secondary already fetched it while the module graph was loading
|
|
await ConfigDistribution.Instance.init()
|
|
|
|
// Secondary that cannot reach the files of the primary refuses to start
|
|
await HorizontalScalabilityStorage.Instance.init()
|
|
|
|
// Propagating token revocations to evict them from the LRU cache
|
|
await TokensCache.Instance.listenForInvalidations()
|
|
|
|
// Keep model cache in sync with the changes handled by the other ones
|
|
await ModelCache.Instance.listenForInvalidations()
|
|
await WatchedWordsListModel.listenForRegexCacheInvalidations()
|
|
await ServerConfigManager.Instance.listenForHomepageChanges()
|
|
|
|
ModelCache.Instance.registerCacheTypeClearedHandler('server-account', () => {
|
|
clearServerActorCache()
|
|
|
|
ClientHtml.invalidateCache()
|
|
})
|
|
|
|
JobQueue.Instance.init()
|
|
await JobQueue.Instance.listenForStateChanges()
|
|
await JobQueue.Instance.listenForJobCancels()
|
|
|
|
// A secondary only pushes emails to the job queue, the primary is the process that sends them
|
|
if (!secondary) Emailer.Instance.init()
|
|
|
|
await Promise.all([
|
|
secondary
|
|
? Promise.resolve()
|
|
: Emailer.Instance.checkConnection(),
|
|
ServerConfigManager.Instance.init()
|
|
])
|
|
|
|
// These schedulers are only supported in the primary
|
|
if (!secondary) {
|
|
ActorFollowScheduler.Instance.enable()
|
|
UpdateVideosScheduler.Instance.enable()
|
|
VideosRedundancyScheduler.Instance.enable()
|
|
RemoveOldHistoryScheduler.Instance.enable()
|
|
RemoveOldAuditLogsScheduler.Instance.enable()
|
|
RemoveOldStatsScheduler.Instance.enable()
|
|
PluginsCheckScheduler.Instance.enable()
|
|
PeerTubeVersionCheckScheduler.Instance.enable()
|
|
AutoFollowIndexInstances.Instance.enable()
|
|
BlocklistSubscriptionsScheduler.Instance.enable()
|
|
WatchedWordsSubscriptionsScheduler.Instance.enable()
|
|
RemoveDanglingResumableUploadsScheduler.Instance.enable()
|
|
RemoveDanglingStagingFilesScheduler.Instance.enable()
|
|
VideoChannelSyncLatestScheduler.Instance.enable()
|
|
LocalVideoStatsBufferScheduler.Instance.enable()
|
|
RunnerJobWatchDogScheduler.Instance.enable()
|
|
RemoveExpiredUserExportsScheduler.Instance.enable()
|
|
}
|
|
|
|
// These ones must also be run on the secondary
|
|
GeoIPUpdateScheduler.Instance.enable()
|
|
YoutubeDlUpdateScheduler.Instance.enable() // Each process has its own binary, used by video imports
|
|
UpdateTokenSessionScheduler.Instance.enable()
|
|
RemoveOldUserLoginDevicesScheduler.Instance.enable()
|
|
ManualMigrationScriptsScheduler.Instance.enable()
|
|
VideoFilesLifecycleScheduler.Instance.enable()
|
|
|
|
OpenTelemetryMetrics.Instance.registerMetrics({ trackerServer })
|
|
|
|
PluginManager.Instance.init(server)
|
|
// Before PeerTubeSocket init
|
|
PluginManager.Instance.registerWebSocketRouter()
|
|
|
|
PeerTubeSocket.Instance.init(server)
|
|
|
|
// The secondary ingests views but never owns the flush/federation loops (primary role)
|
|
VideoStatsManager.Instance.init({ enableDatabaseFlush: !secondary })
|
|
|
|
// Primary only
|
|
if (!secondary) {
|
|
// The inbox stats of every process are counted since the start of the primary
|
|
await StatsManager.Instance.resetInboxStats()
|
|
|
|
// Before serving lazy static files
|
|
try {
|
|
await resetFilesCacheOfOtherStorage()
|
|
} catch (err) {
|
|
logger.error('Cannot reset the cache of remote files.', { err })
|
|
}
|
|
|
|
updateStreamingPlaylistsInfohashesIfNeeded()
|
|
.catch(err => logger.error('Cannot update streaming playlist infohashes.', { err }))
|
|
|
|
updateTorrentsTrackersIfNeeded()
|
|
.catch(err => logger.error('Cannot update the trackers of local torrent files.', { err }))
|
|
|
|
removeRedundanciesOfOtherStorage()
|
|
.catch(err => logger.error('Cannot remove redundancies of the previous storage.', { err }))
|
|
|
|
LiveManager.Instance.init()
|
|
await LiveManager.Instance.listenForSessionStopRequests()
|
|
watchYoutubeDLCookies()
|
|
|
|
if (CONFIG.LIVE.ENABLED) await LiveManager.Instance.run()
|
|
}
|
|
|
|
// Make server listening
|
|
server.listen(port, hostname, async () => {
|
|
// Plugins are registered by both roles so that filter/action hooks behave the same way
|
|
// The primary owns which plugins are installed and the secondary keeps in sync from the primary in its own plugin directory
|
|
if (cliOptions.plugins) {
|
|
try {
|
|
let syncedNpmNames: Set<string>
|
|
|
|
if (secondary) {
|
|
// Registered below, after the native plugins rebuild
|
|
syncedNpmNames = await PluginManager.Instance.syncPlugins({ register: false })
|
|
} else {
|
|
await PluginManager.Instance.removeUnsecurePluginsIfNeededBeforeRegistration()
|
|
}
|
|
|
|
await PluginManager.Instance.rebuildNativePluginsIfNeeded()
|
|
|
|
await PluginManager.Instance.registerPluginsAndThemes()
|
|
|
|
if (secondary) {
|
|
// Registration reads the database again, so it can see plugins the primary installed after the sync
|
|
const notRegistered = await PluginManager.Instance.listPluginsOnlyRegisteredByPrimary(syncedNpmNames)
|
|
if (notRegistered.length !== 0) return exitOnPluginDivergence(notRegistered)
|
|
|
|
// Only after the initial registration, so a notification cannot race it
|
|
await PluginManager.Instance.listenForPluginChanges({ onDivergedFromPrimary: exitOnPluginDivergence })
|
|
} else {
|
|
await PluginManager.Instance.listenForPluginSettingsChanges()
|
|
}
|
|
} catch (err) {
|
|
logger.error('Cannot register plugins and themes.', { err })
|
|
}
|
|
} else if (!secondary) {
|
|
// The secondaries would otherwise compare their plugins with the ones of a previous run
|
|
Redis.Instance.deletePrimaryRegisteredPlugins()
|
|
.catch(err => logger.error('Cannot clear the plugins registered by a previous run.', { err }))
|
|
}
|
|
|
|
if (!secondary) {
|
|
ApplicationModel.updateConfigPart()
|
|
.catch(err => logger.error('Cannot update the stored configuration part.', { err }))
|
|
}
|
|
|
|
JobQueue.Instance.start()
|
|
.catch(err => {
|
|
logger.error('Cannot start job queue.', { err })
|
|
process.exit(-1)
|
|
})
|
|
|
|
// Once the process is ready to serve requests
|
|
await ProcessHeartbeatScheduler.Instance.register()
|
|
|
|
logger.info('HTTP server listening on %s:%d as %s process', hostname, port, getProcessRole())
|
|
logger.info('Web server: %s', WEBSERVER.URL)
|
|
|
|
Hooks.runAction('action:application.listening')
|
|
|
|
if (cliOptions.benchmarkStartup) process.exit(0)
|
|
})
|
|
}
|
|
|
|
// A secondary that failed to register a plugin the primary runs would not serve the same platform
|
|
// We must exit the program
|
|
function exitOnPluginDivergence (npmNames: string[]) {
|
|
logger.error(
|
|
`This process failed to register ${npmNames.join(', ')}, which the primary process runs. ` +
|
|
'Exiting so it is restarted and retries, see the plugin errors above.'
|
|
)
|
|
|
|
shutdownAndExit(1)
|
|
}
|