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/controllers/api/jobs.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/controllers/api/jobs.ts')
-rw-r--r-- | server/controllers/api/jobs.ts | 109 |
1 files changed, 0 insertions, 109 deletions
diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts deleted file mode 100644 index c701bc970..000000000 --- a/server/controllers/api/jobs.ts +++ /dev/null | |||
@@ -1,109 +0,0 @@ | |||
1 | import { Job as BullJob } from 'bullmq' | ||
2 | import express from 'express' | ||
3 | import { HttpStatusCode, Job, JobState, JobType, ResultList, UserRight } from '@shared/models' | ||
4 | import { isArray } from '../../helpers/custom-validators/misc' | ||
5 | import { JobQueue } from '../../lib/job-queue' | ||
6 | import { | ||
7 | apiRateLimiter, | ||
8 | asyncMiddleware, | ||
9 | authenticate, | ||
10 | ensureUserHasRight, | ||
11 | jobsSortValidator, | ||
12 | openapiOperationDoc, | ||
13 | paginationValidatorBuilder, | ||
14 | setDefaultPagination, | ||
15 | setDefaultSort | ||
16 | } from '../../middlewares' | ||
17 | import { listJobsValidator } from '../../middlewares/validators/jobs' | ||
18 | |||
19 | const jobsRouter = express.Router() | ||
20 | |||
21 | jobsRouter.use(apiRateLimiter) | ||
22 | |||
23 | jobsRouter.post('/pause', | ||
24 | authenticate, | ||
25 | ensureUserHasRight(UserRight.MANAGE_JOBS), | ||
26 | asyncMiddleware(pauseJobQueue) | ||
27 | ) | ||
28 | |||
29 | jobsRouter.post('/resume', | ||
30 | authenticate, | ||
31 | ensureUserHasRight(UserRight.MANAGE_JOBS), | ||
32 | resumeJobQueue | ||
33 | ) | ||
34 | |||
35 | jobsRouter.get('/:state?', | ||
36 | openapiOperationDoc({ operationId: 'getJobs' }), | ||
37 | authenticate, | ||
38 | ensureUserHasRight(UserRight.MANAGE_JOBS), | ||
39 | paginationValidatorBuilder([ 'jobs' ]), | ||
40 | jobsSortValidator, | ||
41 | setDefaultSort, | ||
42 | setDefaultPagination, | ||
43 | listJobsValidator, | ||
44 | asyncMiddleware(listJobs) | ||
45 | ) | ||
46 | |||
47 | // --------------------------------------------------------------------------- | ||
48 | |||
49 | export { | ||
50 | jobsRouter | ||
51 | } | ||
52 | |||
53 | // --------------------------------------------------------------------------- | ||
54 | |||
55 | async function pauseJobQueue (req: express.Request, res: express.Response) { | ||
56 | await JobQueue.Instance.pause() | ||
57 | |||
58 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
59 | } | ||
60 | |||
61 | function resumeJobQueue (req: express.Request, res: express.Response) { | ||
62 | JobQueue.Instance.resume() | ||
63 | |||
64 | return res.sendStatus(HttpStatusCode.NO_CONTENT_204) | ||
65 | } | ||
66 | |||
67 | async function listJobs (req: express.Request, res: express.Response) { | ||
68 | const state = req.params.state as JobState | ||
69 | const asc = req.query.sort === 'createdAt' | ||
70 | const jobType = req.query.jobType | ||
71 | |||
72 | const jobs = await JobQueue.Instance.listForApi({ | ||
73 | state, | ||
74 | start: req.query.start, | ||
75 | count: req.query.count, | ||
76 | asc, | ||
77 | jobType | ||
78 | }) | ||
79 | const total = await JobQueue.Instance.count(state, jobType) | ||
80 | |||
81 | const result: ResultList<Job> = { | ||
82 | total, | ||
83 | data: await Promise.all(jobs.map(j => formatJob(j, state))) | ||
84 | } | ||
85 | |||
86 | return res.json(result) | ||
87 | } | ||
88 | |||
89 | async function formatJob (job: BullJob, state?: JobState): Promise<Job> { | ||
90 | const error = isArray(job.stacktrace) && job.stacktrace.length !== 0 | ||
91 | ? job.stacktrace[0] | ||
92 | : null | ||
93 | |||
94 | return { | ||
95 | id: job.id, | ||
96 | state: state || await job.getState(), | ||
97 | type: job.queueName as JobType, | ||
98 | data: job.data, | ||
99 | parent: job.parent | ||
100 | ? { id: job.parent.id } | ||
101 | : undefined, | ||
102 | progress: job.progress as number, | ||
103 | priority: job.opts.priority, | ||
104 | error, | ||
105 | createdAt: new Date(job.timestamp), | ||
106 | finishedOn: new Date(job.finishedOn), | ||
107 | processedOn: new Date(job.processedOn) | ||
108 | } | ||
109 | } | ||