-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: coji <[email protected]>
- Loading branch information
Showing
6 changed files
with
113 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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,32 @@ | ||
import { translate } from "../libs/translation"; | ||
import { addNumbersToContent } from "../utils/addNumbersToContent"; | ||
import { extractArticle } from "../utils/extractArticle"; | ||
import { extractNumberedElements } from "../utils/extractNumberedElements"; | ||
import { fetchWithRetry } from "../utils/fetchWithRetry"; | ||
|
||
interface TranslateJobParams { | ||
url: string; | ||
targetLanguage: string; | ||
apiKey: string; | ||
userId: number; | ||
} | ||
export const translateJob = async (params: TranslateJobParams) => { | ||
const html = await fetchWithRetry(params.url); | ||
const { content, title } = extractArticle(html, params.url); | ||
const numberedContent = addNumbersToContent(content); | ||
const extractedNumberedElements = extractNumberedElements( | ||
numberedContent, | ||
title, | ||
); | ||
const targetLanguage = params.targetLanguage; | ||
|
||
await translate( | ||
params.apiKey, | ||
params.userId, | ||
targetLanguage, | ||
title, | ||
numberedContent, | ||
extractedNumberedElements, | ||
params.url, | ||
); | ||
}; |
47 changes: 0 additions & 47 deletions
47
web/app/feature/translate/libs/userTranslationqueueService.ts
This file was deleted.
Oops, something went wrong.
This file contains 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,38 @@ | ||
import { Queue } from "~/utils/queue.server"; | ||
import { translateJob } from "./jobs/translate-job.server"; | ||
|
||
type TranslateJobData = { | ||
url: string; | ||
targetLanguage: string; | ||
apiKey: string; | ||
userId: number; | ||
}; | ||
|
||
export const getTranslateUserQueue = (userId: number) => { | ||
return Queue<TranslateJobData>(`translation-user-${userId}`, { | ||
processor: async (job) => { | ||
console.log(`Starting job ${job.id} for user ${userId}`); | ||
try { | ||
await translateJob(job.data); | ||
console.log(`Job ${job.id} completed successfully for user ${userId}`); | ||
} catch (error) { | ||
console.error( | ||
`Error processing job ${job.id} for user ${userId}:`, | ||
error, | ||
); | ||
throw error; | ||
} | ||
}, | ||
onComplete: async (job, queue) => { | ||
const waitingCount = await queue.getWaitingCount(); | ||
const activeCount = await queue.getActiveCount(); | ||
const delayedCount = await queue.getDelayedCount(); | ||
const totalCount = waitingCount + activeCount + delayedCount; | ||
if (totalCount === 0) { | ||
console.log( | ||
`All translation jobs for user ${userId} have been completed.`, | ||
); | ||
} | ||
}, | ||
}); | ||
}; |
This file contains 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 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,35 @@ | ||
import { Queue as BullQueue, type Job, type Processor, Worker } from "bullmq"; | ||
import { RedisConfig } from "./redis-config"; | ||
|
||
type RegisteredQueue = { | ||
queue: BullQueue; | ||
worker: Worker; | ||
}; | ||
|
||
declare global { | ||
var __registeredQueues: Record<string, RegisteredQueue> | undefined; | ||
} | ||
|
||
if (!global.__registeredQueues) { | ||
global.__registeredQueues = {}; | ||
} | ||
const registeredQueues = global.__registeredQueues; | ||
|
||
export function Queue<Payload>( | ||
name: string, | ||
handlers: { | ||
processor: Processor<Payload>; | ||
onComplete: (job: Job<Payload>, queue: BullQueue<Payload>) => void; | ||
}, | ||
): BullQueue<Payload> { | ||
if (registeredQueues[name]) { | ||
return registeredQueues[name].queue; | ||
} | ||
const queue = new BullQueue<Payload>(name, { connection: RedisConfig }); | ||
const worker = new Worker<Payload>(name, handlers.processor, { | ||
connection: RedisConfig, | ||
}); | ||
worker.on("completed", (job) => handlers.onComplete(job, queue)); | ||
registeredQueues[name] = { queue, worker }; | ||
return queue; | ||
} |
This file contains 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,4 @@ | ||
export const RedisConfig = { | ||
host: process.env.REDIS_HOST, | ||
port: process.env.REDIS_PORT ? Number(process.env.REDIS_PORT) : 6379, | ||
}; |