Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/platformatic/job-queue/llms.txt

Use this file to discover all available pages before exploring further.

Platformatic Job Queue exports utility functions to help with job ID generation and content-based deduplication.

generateId

Generate a random unique ID for jobs.
function generateId(): string
Uses Node.js’s crypto.randomUUID() to create a unique identifier.

Usage

import { generateId } from '@platformatic/job-queue'

const jobId = generateId()
await queue.enqueue(jobId, { task: 'process-image' })

When to Use

Use generateId() when you want the queue to handle each enqueue as a separate job, even if the payload is identical.
// Each call creates a new job
await queue.enqueue(generateId(), { email: 'user@example.com' })
await queue.enqueue(generateId(), { email: 'user@example.com' })
// Result: 2 separate jobs processed

contentId

Generate a content-based ID by hashing the payload. Useful for deduplication based on content rather than explicit IDs.
function contentId(payload: unknown): string
Creates a SHA-256 hash of the JSON-stringified payload and returns the first 16 characters.

Usage

import { contentId } from '@platformatic/job-queue'

const payload = { email: 'user@example.com', template: 'welcome' }
const jobId = contentId(payload)

await queue.enqueue(jobId, payload)

When to Use

Use contentId() for automatic content-based deduplication. Jobs with identical payloads will get the same ID, preventing duplicate processing.
const payload1 = { url: 'https://example.com/page1' }
const payload2 = { url: 'https://example.com/page1' }

// Both generate the same ID
const id1 = contentId(payload1) // "a1b2c3d4e5f6g7h8"
const id2 = contentId(payload2) // "a1b2c3d4e5f6g7h8"

await queue.enqueue(id1, payload1)
await queue.enqueue(id2, payload2) // Duplicate - will return cached result
The hash is deterministic: the same payload always produces the same ID. However, key order matters in objects:
  • {a: 1, b: 2} produces a different hash than {b: 2, a: 1}

Comparison

FunctionUse CaseDeduplication
generateId()Always process each enqueue as a new jobNo - each call gets unique ID
contentId()Deduplicate based on payload contentYes - identical payloads get same ID
Custom IDsBusiness logic-driven (e.g., user ID + action)Controlled by your ID scheme

Complete Example

import { Queue, MemoryStorage, generateId, contentId } from '@platformatic/job-queue'

const queue = new Queue({
  storage: new MemoryStorage(),
  concurrency: 5
})

queue.execute(async (job) => {
  console.log(`Processing ${job.id}`)
  return { processed: true }
})

await queue.start()

// Use generateId for unique jobs
await queue.enqueue(generateId(), { type: 'send-email' })
await queue.enqueue(generateId(), { type: 'send-email' })
// Result: 2 separate jobs

// Use contentId for content-based deduplication
const payload = { url: 'https://example.com/scrape' }
await queue.enqueue(contentId(payload), payload)
await queue.enqueue(contentId(payload), payload)
// Result: 1 job processed, second returns cached result

// Use custom IDs for business logic
await queue.enqueue(`user-${userId}-welcome`, { userId, template: 'welcome' })

await queue.stop()

Implementation Details

From src/utils/id.ts:6:
export function generateId(): string {
  return randomUUID()
}

export function contentId(payload: unknown): string {
  return createHash('sha256')
    .update(JSON.stringify(payload))
    .digest('hex')
    .slice(0, 16)
}

See Also

Build docs developers (and LLMs) love