mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 05:47:37 +00:00
feat(transcode): support idle-timeout auto-drain for KEDA scaled jobs
This commit is contained in:
+40
-18
@@ -14,18 +14,29 @@ async function run() {
|
||||
// Initialize workflows and activities
|
||||
initTranscodeWorkflows()
|
||||
|
||||
const isOnce = process.argv.includes('--once') || process.env.SHUMAI_WORKER_ONCE === 'true'
|
||||
if (isOnce) {
|
||||
workflowService.getExecutor().setOnceMode(true)
|
||||
console.log('🚀 Starting Transcode Worker in --once mode (single-task execution)...')
|
||||
let idleTimeoutMs = process.env.SHUMAI_WORKER_IDLE_TIMEOUT_MS
|
||||
? parseInt(process.env.SHUMAI_WORKER_IDLE_TIMEOUT_MS, 10)
|
||||
: 0
|
||||
|
||||
const idleTimeoutArg = process.argv.find((arg) => arg.startsWith('--idle-timeout='))
|
||||
if (idleTimeoutArg) {
|
||||
const val = parseInt(idleTimeoutArg.split('=')[1], 10)
|
||||
if (!isNaN(val)) idleTimeoutMs = val
|
||||
}
|
||||
|
||||
if (idleTimeoutMs > 0) {
|
||||
console.log(
|
||||
`🚀 Starting Transcode Worker with idle timeout (${idleTimeoutMs / 1000}s auto-drain)...`,
|
||||
)
|
||||
} else {
|
||||
console.log('🚀 Starting Transcode Worker...')
|
||||
console.log('🚀 Starting Transcode Worker (persistent daemon mode)...')
|
||||
}
|
||||
|
||||
await workflowService.startWorkers(TaskQueueTranscode)
|
||||
|
||||
let isShuttingDown = false
|
||||
let idleTimer: Timer | null = null
|
||||
let activeWorkflows = 0
|
||||
|
||||
const shutdown = async () => {
|
||||
if (isShuttingDown) return
|
||||
@@ -43,20 +54,30 @@ async function run() {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
if (isOnce) {
|
||||
const idleTimeoutMs = process.env.SHUMAI_WORKER_IDLE_TIMEOUT_MS
|
||||
? parseInt(process.env.SHUMAI_WORKER_IDLE_TIMEOUT_MS, 10)
|
||||
: 15_000
|
||||
if (idleTimeoutMs > 0) {
|
||||
const resetIdleTimer = () => {
|
||||
if (idleTimer) {
|
||||
clearTimeout(idleTimer)
|
||||
idleTimer = null
|
||||
}
|
||||
if (activeWorkflows === 0 && !isShuttingDown) {
|
||||
idleTimer = setTimeout(async () => {
|
||||
console.log(
|
||||
`[Transcode Worker] Inactive for ${idleTimeoutMs / 1000}s with no active workflows, shutting down.`,
|
||||
)
|
||||
await shutdown()
|
||||
}, idleTimeoutMs)
|
||||
}
|
||||
}
|
||||
|
||||
idleTimer = setTimeout(async () => {
|
||||
console.log(
|
||||
`[Transcode Worker] No tasks received within idle timeout (${idleTimeoutMs / 1000}s), shutting down.`,
|
||||
)
|
||||
await shutdown()
|
||||
}, idleTimeoutMs)
|
||||
// Start idle timer immediately on startup in case no tasks are enqueued
|
||||
resetIdleTimer()
|
||||
|
||||
workflowService.getExecutor().on('workflowStarted', (task) => {
|
||||
console.log(`[Transcode Worker] Claimed task ${task.id}, cancelling idle timer.`)
|
||||
activeWorkflows++
|
||||
console.log(
|
||||
`[Transcode Worker] Claimed task ${task.id} (active: ${activeWorkflows}), cancelling idle timer.`,
|
||||
)
|
||||
if (idleTimer) {
|
||||
clearTimeout(idleTimer)
|
||||
idleTimer = null
|
||||
@@ -64,8 +85,9 @@ async function run() {
|
||||
})
|
||||
|
||||
workflowService.getExecutor().on('workflowFinished', async ({ taskId }) => {
|
||||
console.log(`[Transcode Worker] Completed task ${taskId} in --once mode, shutting down.`)
|
||||
await shutdown()
|
||||
activeWorkflows = Math.max(0, activeWorkflows - 1)
|
||||
console.log(`[Transcode Worker] Completed task ${taskId} (active: ${activeWorkflows}).`)
|
||||
resetIdleTimer()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user