|
| 1 | +export const description = 'Running AI & Batch workloads with Nitric' |
| 2 | + |
| 3 | +# Batch |
| 4 | + |
| 5 | +Nitric provides functionality that allows you to run large-scale jobs in parallel across multiple virtual machines or compute resources. Unlike Nitric Services, which respond to real-time events (APIs, Schedules, etc.), Batch is intended to efficiently handle tasks that can be processed in batches, which means they don't need to run in real time but can be executed asynchronously. Batches can include tasks that require a lot of computing power, or access to GPU resources, such as machine learning model training, image processing, video transcoding, data processing, and data analysis. |
| 6 | + |
| 7 | +<Note> |
| 8 | + Batches are currently in [Preview](/reference/preview-features) and are |
| 9 | + currently only available in the following languages: JavaScript, Python, Go, |
| 10 | + and Dart, using the nitric/ [email protected], nitric/ [email protected] or later. |
| 11 | +</Note> |
| 12 | + |
| 13 | +Nitric Batch is designed to be used in conjunction with Nitric Services, allowing you to run long-running, computationally intensive tasks in parallel with your real-time services. This allows you to build complex applications that can handle both real-time and batch processing workloads. |
| 14 | + |
| 15 | +Batches are deployed to cloud services such as [AWS Batch](https://aws.amazon.com/batch/), [Azure Batch](https://azure.microsoft.com/en-au/products/batch), and [Google Cloud Batch](https://cloud.google.com/batch/docs). Nitric abstracts the underlying cloud provider, allowing you to run your batch jobs on any of the supported cloud providers without having to worry about the specifics of each provider. |
| 16 | + |
| 17 | +## Enabling Batches |
| 18 | + |
| 19 | +Batches are currently in [Preview](/reference/preview-features). To enable this feature in your project add the following to your `nitric.yaml` file |
| 20 | + |
| 21 | +```yaml |
| 22 | +preview: |
| 23 | + - batch-services |
| 24 | +``` |
| 25 | +
|
| 26 | +## Definitions |
| 27 | +
|
| 28 | +### Batch |
| 29 | +
|
| 30 | +A Batch is similar to a Nitric Service, but it's intended for work with a definitive start and a finish. Where a service is designed to be reactive, a batch is designed to be proactive and run a series of jobs in parallel. |
| 31 | +
|
| 32 | +### Job Definitions |
| 33 | +
|
| 34 | +A Job Definition describes a type of work to be done by a Nitric `Batch` |
| 35 | + |
| 36 | +### Job |
| 37 | + |
| 38 | +A Job is an instance of a Job Definition that is running within a `Batch`, Jobs can be started from other Nitric Services or Batches. |
| 39 | + |
| 40 | +## Limitations of Batches |
| 41 | + |
| 42 | +Jobs are designed to be long running HPC workloads and can take some time to spin up. They are not designed with reactivity in mind and are not suitable for responding to events from cloud resources. |
| 43 | + |
| 44 | +Jobs are unable to run the following: |
| 45 | + |
| 46 | +- Topic Subscriptions |
| 47 | +- Bucket Notifications |
| 48 | +- API & HTTP resources |
| 49 | +- Websocket message handlers |
| 50 | + |
| 51 | +Jobs can be used to read and write to/from all nitric resources, for example they can publish new messages to a Topic, read and write to a Bucket, or read and write to a Database. They just can't respond to real-time events from these resources. |
| 52 | + |
| 53 | +## Defining Batches |
| 54 | + |
| 55 | +Batches are defined similarly to services in a project's `nitric.yaml` file. For example: |
| 56 | + |
| 57 | +```yaml |
| 58 | +batch-services: |
| 59 | + - match: ./batches/*.ts |
| 60 | + start: yarn dev:services $SERVICE_PATH |
| 61 | +``` |
| 62 | + |
| 63 | +<Note>Batches can contain any number of Job Definitions.</Note> |
| 64 | + |
| 65 | +## Defining a Job |
| 66 | + |
| 67 | +Within a Batch we create Job Definitions, by creating a new Job with a unique name and defining a handler function that will be executed when the job is submitted. |
| 68 | + |
| 69 | +<CodeGroup> |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { job, JobContext } from '@nitric/sdk' |
| 73 | +
|
| 74 | +const analyze = job('analyze') |
| 75 | +
|
| 76 | +// Use `handler` to register the callback function that will run when a job is submitted |
| 77 | +analyze.handler( |
| 78 | + async (ctx: JobContext) => { |
| 79 | + // Do some work |
| 80 | + }, |
| 81 | + { cpus: 1, memory: 1024, gpus: 0 } |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +```python |
| 86 | +from nitric.resources import job |
| 87 | +from nitric.application import Nitric |
| 88 | +from nitric.context import JobContext |
| 89 | + |
| 90 | +analyze = job("analyze") |
| 91 | + |
| 92 | +# Create the callback function that will run when a job is submitted |
| 93 | +@analyze(cpus=1, memory=1024, gpus=0) |
| 94 | +async def generate_image(ctx: None): |
| 95 | + # Do some work |
| 96 | + |
| 97 | + |
| 98 | +Nitric.run() |
| 99 | +``` |
| 100 | + |
| 101 | +```go |
| 102 | +import ( |
| 103 | + "github.com/nitrictech/go-sdk/nitric" |
| 104 | + "github.com/nitrictech/go-sdk/nitric/batch" |
| 105 | +) |
| 106 | + |
| 107 | +func main() { |
| 108 | + analyze := nitric.NewJob("analyze") |
| 109 | + |
| 110 | + // Use `Handler` to register the callback function that will run when a job is submitted |
| 111 | + analyze.Handler(func(ctx *batch.Ctx) { |
| 112 | + // Do some work |
| 113 | + }, batch.WithCpus(1), batch.WithMemory(1024), batch.WithGpus(0)) |
| 114 | + |
| 115 | + nitric.Run() |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +```dart |
| 120 | +import 'package:nitric_sdk/nitric.dart'; |
| 121 | +
|
| 122 | +void main() { |
| 123 | + final job = Nitric.job("analyze"); |
| 124 | +
|
| 125 | + job.handler((ctx) async { |
| 126 | + print("New job submitted for ${ctx.req.jobName}: ${ctx.req.message}"); |
| 127 | +
|
| 128 | + return ctx; |
| 129 | + }, opts: JobResourceRequirements(cpus: 1, memory: 1024, gpus: 0)); |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +</CodeGroup> |
| 134 | + |
| 135 | +## Submitting Jobs for Execution |
| 136 | + |
| 137 | +Jobs may be submitted from Nitric `services` or other `batches` using the `submit` method on the job reference. When submitting a job you can provide a payload that will be passed to the job handler function. |
| 138 | + |
| 139 | +<CodeGroup> |
| 140 | + |
| 141 | +```javascript |
| 142 | +import * as nitric from '@nitric/sdk' |
| 143 | + |
| 144 | +const api = nitric.api('public') |
| 145 | +const analyze = nitric.job('analyze').allow('submit') |
| 146 | + |
| 147 | +api.post('/submit-job', async (ctx) => { |
| 148 | + await analyze.submit({ |
| 149 | + someKey: 'someValue', |
| 150 | + }) |
| 151 | +}) |
| 152 | +``` |
| 153 | + |
| 154 | +```python |
| 155 | +from nitric.resources import api, job |
| 156 | +from nitric.application import Nitric |
| 157 | + |
| 158 | +analyze = job("analyze").allow("submit") |
| 159 | +public_api = api("public") |
| 160 | + |
| 161 | +@public_api.post("/submit-job") |
| 162 | +async def submit_job(ctx): |
| 163 | + await analyze.submit( |
| 164 | + { |
| 165 | + "someKey": "someValue" |
| 166 | + } |
| 167 | + ) |
| 168 | + |
| 169 | +Nitric.run() |
| 170 | +``` |
| 171 | + |
| 172 | +```go |
| 173 | +import ( |
| 174 | + "context" |
| 175 | + |
| 176 | + "github.com/nitrictech/go-sdk/nitric" |
| 177 | + "github.com/nitrictech/go-sdk/nitric/apis" |
| 178 | + "github.com/nitrictech/go-sdk/nitric/batch" |
| 179 | +) |
| 180 | + |
| 181 | +func main() { |
| 182 | + api := nitric.NewApi("public") |
| 183 | + analyze := nitric.NewJob("analyze").Allow(batch.JobSubmit) |
| 184 | + |
| 185 | + api.Post("/submit-job", func(ctx *apis.Ctx) { |
| 186 | + analyze.Submit(context.Background(), map[string]interface{}{ |
| 187 | + "someKey": "someValue", |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + nitric.Run() |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +```dart |
| 196 | +import 'package:nitric_sdk/nitric.dart'; |
| 197 | +
|
| 198 | +void main() { |
| 199 | + final api = Nitric.api("public"); |
| 200 | + final analyze = Nitric.job("analyze").allow([JobPermission.submit]); |
| 201 | +
|
| 202 | + api.get("/submit-job", (ctx) async { |
| 203 | + analyze.submit({ |
| 204 | + "someKey": "someValue" |
| 205 | + }); |
| 206 | +
|
| 207 | + return ctx; |
| 208 | + }); |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +</CodeGroup> |
0 commit comments