Node.js - job.handler()

Job handlers are the code that is run when a job request is submitted. These handlers should be written in a separate file to your services.

import { job, JobContext } from '@nitric/sdk'
const analyze = job('analyze')
analyze.handler(
async (ctx: JobContext) => {
// Do some work
return ctx
},
{ cpus: 1, memory: 1024, gpus: 0 },
)

Defining Batches

Batches are defined in different files to services and referenced in a project's nitric.yaml file. For example:

batch-services:
- match: ./services/*.ts
start: yarn dev:services $SERVICE_PATH

Parameters

  • Name
    handler
    Required
    Required
    Type
    JobMiddleware or JobMiddleware[]
    Description

    One or more middleware services to use as the handler which will run on the defined frequency.

  • Name
    opts
    Optional
    Optional
    Type
    JobResourceRequirements
    Description
  • Name
    cpus
    Optional
    Optional
    Type
    number
    Description

    The number of CPUs to allocate to the handler

  • Name
    gpus
    Optional
    Optional
    Type
    number
    Description

    The number of GPUs to allocate to the handler

  • Name
    memory
    Optional
    Optional
    Type
    number
    Description

    The amount of memory (MB) to allocate to the handler

Examples

Define a job handler with default resource requirements

import { job, JobContext } from '@nitric/sdk'
const analyze = job('analyze')
analyze.handler(async (ctx: JobContext) => {
// Do some work
return ctx
})

Create a job handler with custom resource requirements

import { job, JobContext } from '@nitric/sdk'
const analyze = job('analyze')
analyze.handler(
async (ctx: JobContext) => {
// Do some work
return ctx
},
{ cpus: 1, memory: 2048, gpus: 0 },
)
Last updated on Nov 28, 2024