All files / backend/src/jobs index.ts

25% Statements 2/8
100% Branches 0/0
0% Functions 0/4
25% Lines 2/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                2x 2x                                                                                                      
import { jobsManager } from './job-manager.js'
import path from 'path'
import { fileURLToPath } from 'url'
import type { GlobOptions } from 'glob'
import type { CreateThumbnailParams } from './create-thumbnail-job.js'
import type { GlobJobParams } from './glob-job.js'
import type { CommunityTemplate } from '@quiqr/types'
 
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
 
/**
 * Get the absolute path to a compiled job file.
 * In production, jobs are in dist/jobs/*.js
 */
function getJobPath(filename: string): string {
  return path.join(__dirname, filename)
}
 
/**
 * Create a thumbnail image from a source file.
 * Uses shared background job to avoid duplicate work.
 */
export function createThumbnailJob(src: string, dest: string): Promise<string> {
  const params: CreateThumbnailParams = { src, dest }
  return jobsManager.runSharedBackgroundJob<string>(
    `create-thumbnail-job:${src}->${dest}`,
    getJobPath('create-thumbnail-job.js'),
    params
  )
}
 
/**
 * Run a glob pattern match to find files.
 * Each call runs in its own worker thread.
 */
export function globJob(expression: string, options?: GlobOptions): Promise<string[]> {
  const params: GlobJobParams = { expression, options }
  return jobsManager.runBackgroundJob<string[]>(
    `glob-job:${expression}(${JSON.stringify(options)})`,
    getJobPath('glob-job.js'),
    params
  )
}
 
/**
 * Fetch and validate community templates from the Quiqr repository.
 * Each call runs in its own worker thread.
 */
export function updateCommunityTemplatesJob(): Promise<CommunityTemplate[]> {
  return jobsManager.runBackgroundJob<CommunityTemplate[]>(
    'update-community-templates-job',
    getJobPath('update-community-templates-job.js')
  )
}
 
// Export the manager and types
export { jobsManager, JobsManager } from './job-manager.js'
export { BackgroundJobRunner } from './background-job-runner.js'
export type { CreateThumbnailParams, GlobJobParams }