mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
initial commit
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { Connection, Client } from '@temporalio/client'
|
||||
import { WorkflowTask, WorkflowTaskType } from '@/generated/prisma/client'
|
||||
import { Executor } from './executor'
|
||||
|
||||
export const TASK_QUEUE_DB = 'db_queue'
|
||||
|
||||
export class TemporalExecutor implements Executor {
|
||||
private client: Client | null = null
|
||||
private connection: Connection | null = null
|
||||
|
||||
constructor(private address: string = 'localhost:7233') {}
|
||||
|
||||
async submit(task: WorkflowTask): Promise<string> {
|
||||
const client = await this.getClient()
|
||||
const workflowId = `${task.type}-${task.id}`
|
||||
|
||||
let workflowName: string
|
||||
switch (task.type) {
|
||||
case WorkflowTaskType.ai_transcription:
|
||||
workflowName = 'aiTranscriptionWorkflow'
|
||||
break
|
||||
case WorkflowTaskType.ai_embedding:
|
||||
workflowName = 'aiEmbeddingWorkflow'
|
||||
break
|
||||
case WorkflowTaskType.ai_metadata_autofill:
|
||||
workflowName = 'aiAutofillWorkflow'
|
||||
break
|
||||
case WorkflowTaskType.chat:
|
||||
workflowName = 'aiChatWorkflow'
|
||||
break
|
||||
case WorkflowTaskType.transcode:
|
||||
workflowName = 'transcodeWorkflow'
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown task type: ${task.type}`)
|
||||
}
|
||||
|
||||
const handle = await client.workflow.start(workflowName, {
|
||||
taskQueue: TASK_QUEUE_DB,
|
||||
workflowId,
|
||||
args: [task],
|
||||
})
|
||||
|
||||
return handle.workflowId
|
||||
}
|
||||
|
||||
start(): void {
|
||||
// Client doesn't need explicit start
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.connection) {
|
||||
this.connection.close()
|
||||
this.connection = null
|
||||
this.client = null
|
||||
}
|
||||
}
|
||||
|
||||
private async getClient(): Promise<Client> {
|
||||
if (this.client) return this.client
|
||||
this.connection = await Connection.connect({ address: this.address })
|
||||
this.client = new Client({ connection: this.connection })
|
||||
return this.client
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user