Go - 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 (
	"github.com/nitrictech/go-sdk/nitric"
	"github.com/nitrictech/go-sdk/nitric/batch"
)

func main() {
	analyze := nitric.NewJob("analyze")

	analyse.Handler(func(ctx *batch.Ctx) {
		// do long running work
	}, batch.WithCpus(1), batch.WithMemory(2048), batch.WithGpus(1))

	nitric.Run()
}

Defining Batches

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

batch-services:
  - match: ./batches/*.go
    start: go run $SERVICE_PATH

Parameters

  • Name
    handler
    Required
    Required
    Type
    JobHandler
    Description

    The middleware service to use as the handler for Job requests.

  • Name
    options
    Optional
    Optional
    Type
    ...JobOption
    Description
  • Name
    WithCpus
    Optional
    Optional
    Type
    float32
    Description

    The number of CPUs to allocate to the handler

  • Name
    WithGpus
    Optional
    Optional
    Type
    int64
    Description

    The number of GPUs to allocate to the handler

  • Name
    WithMemory
    Optional
    Optional
    Type
    int64
    Description

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

Examples

Define a job handler

import (
	"github.com/nitrictech/go-sdk/nitric"
	"github.com/nitrictech/go-sdk/nitric/batch"
)

func main() {
	analyze := nitric.NewJob("analyze")

	analyse.Handler(func(ctx *batch.Ctx) {
		// do long running work
	})

	nitric.Run()
}

Create a job handler with custom resource requirements

import (
	"github.com/nitrictech/go-sdk/nitric"
	"github.com/nitrictech/go-sdk/nitric/batch"
)

func main() {
	analyze := nitric.NewJob("analyze")

	analyse.Handler(func(ctx *batch.Ctx) {
		// do long running work
	}, batch.WithCpus(1), batch.WithMemory(2048), batch.WithGpus(1))

	nitric.Run()
}