additional backup queue implementation GitOrigin-RevId: 03754e57a6c6798a07dcca6a5248dec61b3cdc7a
99 lines
2.6 KiB
JavaScript
99 lines
2.6 KiB
JavaScript
import Queue from 'bull'
|
|
import logger from '@overleaf/logger'
|
|
import config from 'config'
|
|
import metrics from '@overleaf/metrics'
|
|
import {
|
|
backupProject,
|
|
initializeProjects,
|
|
configureBackup,
|
|
} from './backup.mjs'
|
|
|
|
const CONCURRENCY = 10
|
|
const redisOptions = config.get('redis.queue')
|
|
const TIME_BUCKETS = [10, 100, 500, 1000, 5000, 10000, 30000, 60000]
|
|
|
|
// Configure backup settings to match worker concurrency
|
|
configureBackup({ concurrency: 5, batchConcurrency: 5 })
|
|
|
|
// Create a Bull queue named 'backup'
|
|
const backupQueue = new Queue('backup', {
|
|
redis: redisOptions,
|
|
})
|
|
|
|
// Log queue events
|
|
backupQueue.on('active', job => {
|
|
logger.info({ job }, 'job is now active')
|
|
})
|
|
|
|
backupQueue.on('completed', (job, result) => {
|
|
metrics.inc('backup_worker_job', 1, { status: 'completed' })
|
|
logger.info({ job, result }, 'job completed')
|
|
})
|
|
|
|
backupQueue.on('failed', (job, err) => {
|
|
metrics.inc('backup_worker_job', 1, { status: 'failed' })
|
|
logger.error({ job, err }, 'job failed')
|
|
})
|
|
|
|
backupQueue.on('waiting', jobId => {
|
|
logger.info({ jobId }, 'job is waiting')
|
|
})
|
|
|
|
backupQueue.on('error', error => {
|
|
logger.error({ error }, 'queue error')
|
|
})
|
|
|
|
// Process jobs
|
|
backupQueue.process(CONCURRENCY, async job => {
|
|
const { projectId, startDate, endDate } = job.data
|
|
|
|
if (projectId) {
|
|
return await runBackup(projectId)
|
|
} else if (startDate && endDate) {
|
|
return await runInit(startDate, endDate)
|
|
} else {
|
|
throw new Error('invalid job data')
|
|
}
|
|
})
|
|
|
|
async function runBackup(projectId) {
|
|
const timer = new metrics.Timer(
|
|
'backup_worker_job_duration',
|
|
1,
|
|
{},
|
|
TIME_BUCKETS
|
|
)
|
|
try {
|
|
logger.info({ projectId }, 'processing backup for project')
|
|
await backupProject(projectId, {})
|
|
timer.done()
|
|
return `backup completed ${projectId}`
|
|
} catch (err) {
|
|
logger.error({ projectId, err }, 'backup failed')
|
|
throw err // Re-throw to mark job as failed
|
|
}
|
|
}
|
|
|
|
async function runInit(startDate, endDate) {
|
|
try {
|
|
logger.info({ startDate, endDate }, 'initializing projects')
|
|
await initializeProjects({ 'start-date': startDate, 'end-date': endDate })
|
|
return `initialization completed ${startDate} - ${endDate}`
|
|
} catch (err) {
|
|
logger.error({ startDate, endDate, err }, 'initialization failed')
|
|
throw err
|
|
}
|
|
}
|
|
|
|
export async function drainQueue() {
|
|
logger.info({ queue: backupQueue.name }, 'pausing queue')
|
|
await backupQueue.pause(true) // pause this worker and wait for jobs to finish
|
|
logger.info({ queue: backupQueue.name }, 'closing queue')
|
|
await backupQueue.close()
|
|
}
|
|
|
|
export async function healthCheck() {
|
|
const count = await backupQueue.count()
|
|
metrics.gauge('backup_worker_queue_length', count)
|
|
}
|