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

Commit 1ddf3ee

Browse files
committed
fix go batch docs
1 parent c2766f2 commit 1ddf3ee

File tree

5 files changed

+84
-69
lines changed

5 files changed

+84
-69
lines changed

src/pages/batch.mdx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,25 @@ export const description = 'Running AI & Batch workloads with Nitric'
44

55
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.
66

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+
713
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.
814

915
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.
1016

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+
1126
## Definitions
1227
1328
### Batch
@@ -42,7 +57,7 @@ Batches are defined similarly to services in a project's `nitric.yaml` file. For
4257
```yaml
4358
batch-services:
4459
- match: ./batches/*.ts
45-
start: yarn dev:batches $SERVICE_PATH
60+
start: yarn dev:services $SERVICE_PATH
4661
```
4762

4863
<Note>Batches can contain any number of Job Definitions.</Note>
@@ -83,6 +98,24 @@ async def generate_image(ctx: None):
8398
Nitric.run()
8499
```
85100

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+
86119
```dart
87120
import 'package:nitric_sdk/nitric.dart';
88121
@@ -136,6 +169,29 @@ async def submit_job(ctx):
136169
Nitric.run()
137170
```
138171

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+
139195
```dart
140196
import 'package:nitric_sdk/nitric.dart';
141197

src/pages/reference/go/batch/job-handler.mdx

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,18 @@ Job handlers are the code that is run when a job request is submitted. These han
77

88
```go
99
import (
10-
"fmt"
11-
1210
"github.com/nitrictech/go-sdk/nitric"
1311
"github.com/nitrictech/go-sdk/nitric/batch"
1412
)
1513

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

19-
analyze.Handler(nitric.JobResourceRequirements{
20-
Cpus: 1,
21-
Memory: 2048,
22-
Gpus: 0,
23-
}, func(ctx *batch.Ctx) {
17+
analyse.Handler(func(ctx *batch.Ctx) {
2418
// do long running work
25-
})
19+
}, batch.WithCpus(1), batch.WithMemory(2048), batch.WithGpus(1))
2620

27-
if err := nitric.Run(); err != nil {
28-
fmt.Println(err)
29-
}
21+
nitric.Run()
3022
}
3123
```
3224

@@ -46,14 +38,14 @@ batch-services:
4638
<Property name="handler" required type="JobHandler">
4739
The middleware service to use as the handler for Job requests.
4840
</Property>
49-
<Property name="opts" type="JobResourceRequirements" nested>
50-
<Property name="cpus" type="float32">
41+
<Property name="options" type="...JobOption" nested>
42+
<Property name="WithCpus" type="float32">
5143
The number of CPUs to allocate to the handler
5244
</Property>
53-
<Property name="gpus" type="int64">
45+
<Property name="WithGpus" type="int64">
5446
The number of GPUs to allocate to the handler
5547
</Property>
56-
<Property name="memory" type="int64">
48+
<Property name="WithMemory" type="int64">
5749
The amount of memory (MB) to allocate to the handler
5850
</Property>
5951
</Property>
@@ -65,48 +57,36 @@ batch-services:
6557
6658
```go
6759
import (
68-
"fmt"
69-
7060
"github.com/nitrictech/go-sdk/nitric"
7161
"github.com/nitrictech/go-sdk/nitric/batch"
7262
)
7363

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

77-
analyze.Handler(nitric.JobResourceRequirements{}, func(ctx *batch.Ctx) {
67+
analyse.Handler(func(ctx *batch.Ctx) {
7868
// do long running work
7969
})
8070

81-
if err := nitric.Run(); err != nil {
82-
fmt.Println(err)
83-
}
71+
nitric.Run()
8472
}
8573
```
8674

8775
### Create a job handler with custom resource requirements
8876

8977
```go
9078
import (
91-
"fmt"
92-
9379
"github.com/nitrictech/go-sdk/nitric"
9480
"github.com/nitrictech/go-sdk/nitric/batch"
9581
)
9682

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

100-
analyze.Handler(nitric.JobResourceRequirements{
101-
Cpus: 1,
102-
Memory: 2048,
103-
Gpus: 0,
104-
}, func(ctx *batch.Ctx) {
86+
analyse.Handler(func(ctx *batch.Ctx) {
10587
// do long running work
106-
})
88+
}, batch.WithCpus(1), batch.WithMemory(2048), batch.WithGpus(1))
10789

108-
if err := nitric.Run(); err != nil {
109-
fmt.Println(err)
110-
}
90+
nitric.Run()
11191
}
11292
```

src/pages/reference/go/batch/job-submit.mdx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,19 @@ Jobs may be submitted from Nitric `services` or other `batches` using the `submi
88
```go
99
import (
1010
"context"
11-
"fmt"
1211

1312
"github.com/nitrictech/go-sdk/nitric"
13+
"github.com/nitrictech/go-sdk/nitric/batch"
1414
)
1515

1616
func main() {
17-
analyze, err := nitric.NewJob("analyze").Allow(nitric.JobSubmit)
18-
if err != nil {
19-
fmt.Println(err)
20-
}
17+
analyse := nitric.NewJob("analyse").Allow(batch.JobSubmit)
2118

2219
analyze.Submit(context.TODO(), map[string]interface{}{
2320
"message": "message contents",
2421
})
2522

26-
if err := nitric.Run(); err != nil {
27-
fmt.Println(err)
28-
}
23+
nitric.Run()
2924
}
3025
```
3126

@@ -44,23 +39,18 @@ func main() {
4439
```go
4540
import (
4641
"context"
47-
"fmt"
4842

4943
"github.com/nitrictech/go-sdk/nitric"
44+
"github.com/nitrictech/go-sdk/nitric/batch"
5045
)
5146

5247
func main() {
53-
analyze, err := nitric.NewJob("analyze").Allow(nitric.JobSubmit)
54-
if err != nil {
55-
fmt.Println(err)
56-
}
48+
analyse := nitric.NewJob("analyse").Allow(batch.JobSubmit)
5749

5850
analyze.Submit(context.TODO(), map[string]interface{}{
5951
"message": "message contents",
6052
})
6153

62-
if err := nitric.Run(); err != nil {
63-
fmt.Println(err)
64-
}
54+
nitric.Run()
6555
}
6656
```

src/pages/reference/go/batch/job.mdx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ Creates a new Batch Job.
77

88
```go
99
import (
10-
"fmt"
11-
1210
"github.com/nitrictech/go-sdk/nitric"
1311
)
1412

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

18-
if err := nitric.Run(); err != nil {
19-
fmt.Println(err)
20-
}
16+
nitric.Run()
2117
}
2218
```
2319

@@ -36,37 +32,29 @@ func main() {
3632

3733
```go
3834
import (
39-
"fmt"
40-
4135
"github.com/nitrictech/go-sdk/nitric"
4236
)
4337

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

47-
if err := nitric.Run(); err != nil {
48-
fmt.Println(err)
49-
}
41+
nitric.Run()
5042
}
5143
```
5244

5345
### Create a Job with permissions to submit jobs
5446

5547
```go
56-
import (
57-
"fmt"
48+
package main
5849

50+
import (
5951
"github.com/nitrictech/go-sdk/nitric"
52+
"github.com/nitrictech/go-sdk/nitric/batch"
6053
)
6154

6255
func main() {
63-
analyze, err := nitric.NewJob("analyze").Allow(nitric.JobSubmit)
64-
if err != nil {
65-
fmt.Println(err)
66-
}
67-
68-
if err := nitric.Run(); err != nil {
69-
fmt.Println(err)
70-
}
56+
analyse := nitric.NewJob("analyse").Allow(batch.JobSubmit)
57+
58+
nitric.Run()
7159
}
7260
```

src/pages/reference/preview-features/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Each preview feature will have its own documentation page, which will include in
1515
| ------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
1616
| [docker-providers](/reference/providers/install/docker) | CLI v1.39.0 | [feedback](https://github.com/nitrictech/nitric/issues/605) |
1717
| [sql-databases](/sql) | CLI v1.42.0<br/>AWS Provider v1.6.0<br/>Azure Provider v1.9.0<br/>AWS Terraform Provider v1.8.0 | [feedback](https://github.com/nitrictech/cli/issues/732) |
18+
| [batch-services](/batch) | CLI v1.54.0<br/>AWS Provider v1.14.0<br/>GCP Provider v1.14.0 | [feedback]( |
1819

1920
## Released Preview Features
2021

0 commit comments

Comments
 (0)