Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 2e21df0

Browse files
tjholmjyecuschHomelessDinosaur
authored
docs(batch): add batch services docs and language references
Co-authored-by: Jye Cusch <[email protected]> Co-authored-by: Ryan Cartwright <[email protected]>
1 parent a87dc56 commit 2e21df0

File tree

16 files changed

+951
-0
lines changed

16 files changed

+951
-0
lines changed

dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CORS
1313
ECR
1414
GCP
1515
GCR
16+
HPC
1617
IAM
1718
OpenID
1819
JSON
@@ -82,6 +83,7 @@ roadmap
8283
scaffolded
8384
scalable
8485
serverless
86+
transcoding
8587
triages
8688
undeploy
8789
uuid

src/nav.config.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ClockIcon,
55
CloudIcon,
66
CommandLineIcon,
7+
CpuChipIcon,
78
GlobeAltIcon,
89
HomeIcon,
910
LockClosedIcon,
@@ -68,6 +69,11 @@ const buildingBlockLinks = [
6869
href: '/http',
6970
icon: ServerIcon,
7071
},
72+
{
73+
title: 'AI & Batch',
74+
href: '/batch',
75+
icon: CpuChipIcon,
76+
},
7177
{
7278
title: 'Key Value Stores',
7379
href: '/keyvalue',
@@ -694,6 +700,20 @@ const fullNav: FullNav = {
694700
links: buildingBlockLinks.filter((link) => link.href !== '/http'),
695701
},
696702
],
703+
batch: [
704+
{
705+
links: [
706+
{
707+
title: 'Overview',
708+
href: '/batch',
709+
},
710+
],
711+
},
712+
{
713+
title: 'More Building Blocks',
714+
links: buildingBlockLinks.filter((link) => link.href !== '/batch'),
715+
},
716+
],
697717
keyvalue: [
698718
{
699719
links: [
@@ -1170,6 +1190,23 @@ const fullNav: FullNav = {
11701190
},
11711191
],
11721192
},
1193+
{
1194+
title: 'Batch Jobs',
1195+
links: [
1196+
{
1197+
title: 'job()',
1198+
href: '/reference/nodejs/batch/job',
1199+
},
1200+
{
1201+
title: 'job.handler()',
1202+
href: '/reference/nodejs/batch/job-handler',
1203+
},
1204+
{
1205+
title: 'job.send()',
1206+
href: '/reference/nodejs/batch/job-submit',
1207+
},
1208+
],
1209+
},
11731210
{
11741211
title: 'HTTP',
11751212
links: [
@@ -1636,6 +1673,23 @@ const fullNav: FullNav = {
16361673
},
16371674
],
16381675
},
1676+
{
1677+
title: 'Batch Jobs',
1678+
links: [
1679+
{
1680+
title: 'job()',
1681+
href: '/reference/python/batch/job',
1682+
},
1683+
{
1684+
title: 'job.handler()',
1685+
href: '/reference/python/batch/job-handler',
1686+
},
1687+
{
1688+
title: 'job.send()',
1689+
href: '/reference/python/batch/job-submit',
1690+
},
1691+
],
1692+
},
16391693
{
16401694
title: 'Key Value Stores',
16411695
links: [
@@ -1867,6 +1921,23 @@ const fullNav: FullNav = {
18671921
},
18681922
],
18691923
},
1924+
{
1925+
title: 'Batch Jobs',
1926+
links: [
1927+
{
1928+
title: 'job()',
1929+
href: '/reference/dart/batch/job',
1930+
},
1931+
{
1932+
title: 'job.handler()',
1933+
href: '/reference/dart/batch/job-handler',
1934+
},
1935+
{
1936+
title: 'job.send()',
1937+
href: '/reference/dart/batch/job-submit',
1938+
},
1939+
],
1940+
},
18701941
{
18711942
title: 'Key Value Stores',
18721943
links: [
@@ -2498,6 +2569,10 @@ const fullNav: FullNav = {
24982569
title: 'NewApi()',
24992570
href: '/reference/go/api/api',
25002571
},
2572+
{
2573+
title: 'NewJob()',
2574+
href: '/reference/go/batch/job',
2575+
},
25012576
{
25022577
title: 'NewKv()',
25032578
href: '/reference/go/keyvalue/keyvalue',
@@ -2581,6 +2656,19 @@ const fullNav: FullNav = {
25812656
},
25822657
],
25832658
},
2659+
{
2660+
title: 'Batch Jobs',
2661+
links: [
2662+
{
2663+
title: 'Job.Handler()',
2664+
href: '/reference/go/batch/job-handler',
2665+
},
2666+
{
2667+
title: 'Job.Send()',
2668+
href: '/reference/go/batch/job-submit',
2669+
},
2670+
],
2671+
},
25842672
{
25852673
title: 'Key Value Stores',
25862674
links: [

src/pages/batch.mdx

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)