diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/sync-channel.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/sync-channel.ts')
-rw-r--r-- | server/lib/sync-channel.ts | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/server/lib/sync-channel.ts b/server/lib/sync-channel.ts deleted file mode 100644 index 3a805a943..000000000 --- a/server/lib/sync-channel.ts +++ /dev/null | |||
@@ -1,111 +0,0 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { YoutubeDLWrapper } from '@server/helpers/youtube-dl' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
4 | import { buildYoutubeDLImport } from '@server/lib/video-pre-import' | ||
5 | import { UserModel } from '@server/models/user/user' | ||
6 | import { VideoImportModel } from '@server/models/video/video-import' | ||
7 | import { MChannel, MChannelAccountDefault, MChannelSync } from '@server/types/models' | ||
8 | import { VideoChannelSyncState, VideoPrivacy } from '@shared/models' | ||
9 | import { CreateJobArgument, JobQueue } from './job-queue' | ||
10 | import { ServerConfigManager } from './server-config-manager' | ||
11 | |||
12 | export async function synchronizeChannel (options: { | ||
13 | channel: MChannelAccountDefault | ||
14 | externalChannelUrl: string | ||
15 | videosCountLimit: number | ||
16 | channelSync?: MChannelSync | ||
17 | onlyAfter?: Date | ||
18 | }) { | ||
19 | const { channel, externalChannelUrl, videosCountLimit, onlyAfter, channelSync } = options | ||
20 | |||
21 | if (channelSync) { | ||
22 | channelSync.state = VideoChannelSyncState.PROCESSING | ||
23 | channelSync.lastSyncAt = new Date() | ||
24 | await channelSync.save() | ||
25 | } | ||
26 | |||
27 | try { | ||
28 | const user = await UserModel.loadByChannelActorId(channel.actorId) | ||
29 | const youtubeDL = new YoutubeDLWrapper( | ||
30 | externalChannelUrl, | ||
31 | ServerConfigManager.Instance.getEnabledResolutions('vod'), | ||
32 | CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION | ||
33 | ) | ||
34 | |||
35 | const targetUrls = await youtubeDL.getInfoForListImport({ latestVideosCount: videosCountLimit }) | ||
36 | |||
37 | logger.info( | ||
38 | 'Fetched %d candidate URLs for sync channel %s.', | ||
39 | targetUrls.length, channel.Actor.preferredUsername, { targetUrls } | ||
40 | ) | ||
41 | |||
42 | if (targetUrls.length === 0) { | ||
43 | if (channelSync) { | ||
44 | channelSync.state = VideoChannelSyncState.SYNCED | ||
45 | await channelSync.save() | ||
46 | } | ||
47 | |||
48 | return | ||
49 | } | ||
50 | |||
51 | const children: CreateJobArgument[] = [] | ||
52 | |||
53 | for (const targetUrl of targetUrls) { | ||
54 | if (await skipImport(channel, targetUrl, onlyAfter)) continue | ||
55 | |||
56 | const { job } = await buildYoutubeDLImport({ | ||
57 | user, | ||
58 | channel, | ||
59 | targetUrl, | ||
60 | channelSync, | ||
61 | importDataOverride: { | ||
62 | privacy: VideoPrivacy.PUBLIC | ||
63 | } | ||
64 | }) | ||
65 | |||
66 | children.push(job) | ||
67 | } | ||
68 | |||
69 | // Will update the channel sync status | ||
70 | const parent: CreateJobArgument = { | ||
71 | type: 'after-video-channel-import', | ||
72 | payload: { | ||
73 | channelSyncId: channelSync?.id | ||
74 | } | ||
75 | } | ||
76 | |||
77 | await JobQueue.Instance.createJobWithChildren(parent, children) | ||
78 | } catch (err) { | ||
79 | logger.error(`Failed to import ${externalChannelUrl} in channel ${channel.name}`, { err }) | ||
80 | channelSync.state = VideoChannelSyncState.FAILED | ||
81 | await channelSync.save() | ||
82 | } | ||
83 | } | ||
84 | |||
85 | // --------------------------------------------------------------------------- | ||
86 | |||
87 | async function skipImport (channel: MChannel, targetUrl: string, onlyAfter?: Date) { | ||
88 | if (await VideoImportModel.urlAlreadyImported(channel.id, targetUrl)) { | ||
89 | logger.debug('%s is already imported for channel %s, skipping video channel synchronization.', targetUrl, channel.name) | ||
90 | return true | ||
91 | } | ||
92 | |||
93 | if (onlyAfter) { | ||
94 | const youtubeDL = new YoutubeDLWrapper( | ||
95 | targetUrl, | ||
96 | ServerConfigManager.Instance.getEnabledResolutions('vod'), | ||
97 | CONFIG.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION | ||
98 | ) | ||
99 | |||
100 | const videoInfo = await youtubeDL.getInfoForDownload() | ||
101 | |||
102 | const onlyAfterWithoutTime = new Date(onlyAfter) | ||
103 | onlyAfterWithoutTime.setHours(0, 0, 0, 0) | ||
104 | |||
105 | if (videoInfo.originallyPublishedAtWithoutTime.getTime() < onlyAfterWithoutTime.getTime()) { | ||
106 | return true | ||
107 | } | ||
108 | } | ||
109 | |||
110 | return false | ||
111 | } | ||