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 (
	"fmt"

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

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

	analyse.Handler(nitric.JobResourceRequirements{
		Cpus:   1,
		Memory: 2048,
		Gpus:   0,
	}, func(ctx *batch.Ctx) {
		// do long running work
	})

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

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
    opts
    Optional
    Optional
    Type
    JobResourceRequirements
    Description
  • Name
    cpus
    Optional
    Optional
    Type
    float32
    Description

    The number of CPUs to allocate to the handler

  • Name
    gpus
    Optional
    Optional
    Type
    int64
    Description

    The number of GPUs to allocate to the handler

  • Name
    memory
    Optional
    Optional
    Type
    int64
    Description

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

Examples

Define a job handler

import (
	"fmt"

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

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

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

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}

Create a job handler with custom resource requirements

import (
	"fmt"

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

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

	analyse.Handler(nitric.JobResourceRequirements{
		Cpus:   1,
		Memory: 2048,
		Gpus:   0,
	}, func(ctx *batch.Ctx) {
		// do long running work
	})

	if err := nitric.Run(); err != nil {
		fmt.Println(err)
	}
}