-
Notifications
You must be signed in to change notification settings - Fork 7
Add jobs phase 2 learning console with polling list sync #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
249948e
feat(jobs): add phase 2 learning console
fengzai6 470e28b
fix(jobs): sync polled job status back to the list
fengzai6 361d098
fix(jobs): address CI lint and review regressions
fengzai6 f1f442c
style(jobs): format remaining job unit tests with prettier
fengzai6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { SetMetadata } from '@nestjs/common'; | ||
|
|
||
| export const SKIP_TIMEOUT_KEY = 'skipTimeout'; | ||
|
|
||
| export const SkipTimeout = () => SetMetadata(SKIP_TIMEOUT_KEY, true); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
apps/server/src/shared/jobs/board/job-board.auth.middleware.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { REFRESH_TOKEN_KEY, TokenType } from '@/common/constants/auth'; | ||
| import { JwtPayload } from '@/modules/auth/strategies/jwt-auth.strategy'; | ||
| import { UsersService } from '@/modules/users/users.service'; | ||
| import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
| import { JwtService } from '@nestjs/jwt'; | ||
| import { NextFunction, Request, Response } from 'express'; | ||
|
|
||
| @Injectable() | ||
| export class JobBoardAuthMiddleware implements NestMiddleware { | ||
| constructor( | ||
| private readonly jwtService: JwtService, | ||
| private readonly usersService: UsersService, | ||
| ) {} | ||
|
|
||
| async use(req: Request, res: Response, next: NextFunction) { | ||
| const tokenResult = this.extractToken(req); | ||
| if (!tokenResult) { | ||
| this.reject(res); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const payload = this.jwtService.verify<JwtPayload>(tokenResult.token); | ||
| if (payload.type !== tokenResult.tokenType) { | ||
| this.reject(res); | ||
| return; | ||
| } | ||
|
|
||
| await this.usersService.findOne({ id: payload.sub }); | ||
| next(); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } catch { | ||
| this.reject(res); | ||
| } | ||
| } | ||
|
|
||
| private extractToken(req: Request) { | ||
| const bearerToken = this.extractBearerToken(req.headers.authorization); | ||
| if (bearerToken) { | ||
| return { token: bearerToken, tokenType: TokenType.ACCESS }; | ||
| } | ||
|
|
||
| const refreshToken = req.cookies?.[REFRESH_TOKEN_KEY] as string | undefined; | ||
| if (refreshToken) { | ||
| return { token: refreshToken, tokenType: TokenType.REFRESH }; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private extractBearerToken(authorization?: string): string | null { | ||
| if (!authorization?.startsWith('Bearer ')) return null; | ||
| const token = authorization.slice('Bearer '.length).trim(); | ||
| return token || null; | ||
| } | ||
|
|
||
| private reject(res: Response) { | ||
| res.status(401).json({ | ||
| statusCode: 401, | ||
| message: 'Unauthorized', | ||
| code: 'UNAUTHORIZED', | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { AppConfigModule } from '@/config/config.module'; | ||
| import { getConfig } from '@/config/configuration'; | ||
| import { UsersModule } from '@/modules/users/users.module'; | ||
| import { InjectQueue } from '@nestjs/bullmq'; | ||
| import { Module, OnModuleInit } from '@nestjs/common'; | ||
| import { ConfigService } from '@nestjs/config'; | ||
| import { HttpAdapterHost } from '@nestjs/core'; | ||
| import { JwtModule } from '@nestjs/jwt'; | ||
| import { createBullBoard } from '@bull-board/api'; | ||
| import { BullMQAdapter } from '@bull-board/api/bullMQAdapter'; | ||
| import { ExpressAdapter } from '@bull-board/express'; | ||
| import { Queue } from 'bullmq'; | ||
| import { Express } from 'express'; | ||
| import { DEFAULT_JOB_QUEUE } from '../constants/job.constants'; | ||
| import { JobQueueModule } from '../queue/job-queue.module'; | ||
| import { IBullJobData } from '../types/job.types'; | ||
| import { JobBoardAuthMiddleware } from './job-board.auth.middleware'; | ||
|
|
||
| const JOB_BOARD_PATH = '/admin/queues'; | ||
|
|
||
| @Module({ | ||
| imports: [ | ||
| UsersModule, | ||
| JobQueueModule, | ||
| JwtModule.registerAsync({ | ||
| imports: [AppConfigModule], | ||
| inject: [ConfigService], | ||
| useFactory: (configService: ConfigService) => { | ||
| const { jwt } = getConfig(configService); | ||
| return { | ||
| secret: jwt.secret, | ||
| signOptions: { expiresIn: jwt.accessExpiresIn }, | ||
| }; | ||
| }, | ||
| }), | ||
| ], | ||
| providers: [JobBoardAuthMiddleware], | ||
| }) | ||
| export class JobBoardModule implements OnModuleInit { | ||
| constructor( | ||
| private readonly httpAdapterHost: HttpAdapterHost, | ||
| private readonly authMiddleware: JobBoardAuthMiddleware, | ||
| @InjectQueue(DEFAULT_JOB_QUEUE) | ||
| private readonly defaultQueue: Queue<IBullJobData>, | ||
| ) {} | ||
|
|
||
| onModuleInit() { | ||
| const serverAdapter = new ExpressAdapter(); | ||
| serverAdapter.setBasePath(JOB_BOARD_PATH); | ||
|
|
||
| createBullBoard({ | ||
| queues: [new BullMQAdapter(this.defaultQueue)], | ||
| serverAdapter, | ||
| }); | ||
|
|
||
| const app: Express = | ||
| this.httpAdapterHost.httpAdapter.getInstance<Express>(); | ||
| app.use(JOB_BOARD_PATH, this.authMiddleware.use.bind(this.authMiddleware)); | ||
| app.use(JOB_BOARD_PATH, serverAdapter.getRouter()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { Observable, Subject, filter } from 'rxjs'; | ||
| import { IJobSseEvent } from '../types/job.types'; | ||
|
|
||
| @Injectable() | ||
| export class JobEventsService { | ||
| private readonly events$ = new Subject<IJobSseEvent>(); | ||
| private sequence = 0; | ||
|
|
||
| publish(event: Omit<IJobSseEvent, 'id'>): IJobSseEvent { | ||
| const nextEvent: IJobSseEvent = { | ||
| ...event, | ||
| id: String(++this.sequence), | ||
| }; | ||
|
|
||
| this.events$.next(nextEvent); | ||
| return nextEvent; | ||
| } | ||
|
|
||
| subscribe(jobId: string): Observable<IJobSseEvent> { | ||
| return this.events$.asObservable().pipe( | ||
| filter((event) => { | ||
| return event.data.id === jobId; | ||
| }), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { | ||
| JOB_SSE_EVENT, | ||
| JOB_STATUS, | ||
| JobStatus, | ||
| } from '../constants/job.constants'; | ||
| import { IJobSseEvent } from '../types/job.types'; | ||
|
|
||
| export const resolveJobSseEventName = (status: JobStatus) => { | ||
| if (status === JOB_STATUS.COMPLETED) return JOB_SSE_EVENT.COMPLETED; | ||
| if (status === JOB_STATUS.FAILED) return JOB_SSE_EVENT.FAILED; | ||
| if (status === JOB_STATUS.CANCELLED) return JOB_SSE_EVENT.CANCELLED; | ||
| return JOB_SSE_EVENT.UPDATED; | ||
| }; | ||
|
|
||
| export const formatSseEvent = (event: IJobSseEvent) => { | ||
| return [ | ||
| `event: ${event.event}`, | ||
| `id: ${event.id}`, | ||
| `data: ${JSON.stringify(event.data)}`, | ||
| '', | ||
| '', | ||
| ].join('\n'); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.