Skip to content

Commit 73bcbce

Browse files
feat: add batch services support (#91)
Co-authored-by: Ryan Cartwright <[email protected]>
1 parent 3c501e1 commit 73bcbce

File tree

15 files changed

+712
-7
lines changed

15 files changed

+712
-7
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ generate:
4848
go run github.com/golang/mock/mockgen github.com/nitrictech/nitric/core/pkg/proto/storage/v1 StorageClient > mocks/storage.go
4949
go run github.com/golang/mock/mockgen github.com/nitrictech/nitric/core/pkg/proto/secrets/v1 SecretManagerClient > mocks/secrets.go
5050
go run github.com/golang/mock/mockgen github.com/nitrictech/nitric/core/pkg/proto/topics/v1 TopicsClient > mocks/topics.go
51+
go run github.com/golang/mock/mockgen github.com/nitrictech/nitric/core/pkg/proto/batch/v1 BatchClient > mocks/batch.go
5152
go run github.com/golang/mock/mockgen -package mock_v1 google.golang.org/grpc ClientConnInterface > mocks/grpc_clientconn.go
5253

5354
# Runs tests for coverage upload to codecov.io

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/golangci/golangci-lint v1.61.0
1111
github.com/google/addlicense v1.1.1
1212
github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc
13-
github.com/nitrictech/nitric/core v0.0.0-20240915234849-42c1e482ddab
13+
github.com/nitrictech/nitric/core v0.0.0-20241003062412-76ea6275fb0b
1414
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf
1515
github.com/onsi/ginkgo v1.16.5
1616
github.com/onsi/gomega v1.34.2
@@ -118,7 +118,7 @@ require (
118118
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
119119
github.com/polyfloyd/go-errorlint v1.6.0 // indirect
120120
github.com/prometheus/client_golang v1.14.0 // indirect
121-
github.com/prometheus/client_model v0.5.0 // indirect
121+
github.com/prometheus/client_model v0.6.0 // indirect
122122
github.com/prometheus/common v0.37.0 // indirect
123123
github.com/prometheus/procfs v0.8.0 // indirect
124124
github.com/quasilyte/go-ruleguard v0.4.3-0.20240823090925-0fe6f58b47b1 // indirect
@@ -220,5 +220,5 @@ require (
220220
go-simpler.org/musttag v0.12.2 // indirect
221221
go-simpler.org/sloglint v0.7.2 // indirect
222222
go.uber.org/automaxprocs v1.5.3 // indirect
223-
go.uber.org/goleak v1.2.1 // indirect
223+
go.uber.org/goleak v1.3.0 // indirect
224224
)

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,10 @@ github.com/nishanths/exhaustive v0.12.0 h1:vIY9sALmw6T/yxiASewa4TQcFsVYZQQRUQJhK
424424
github.com/nishanths/exhaustive v0.12.0/go.mod h1:mEZ95wPIZW+x8kC4TgC+9YCUgiST7ecevsVDTgc2obs=
425425
github.com/nishanths/predeclared v0.2.2 h1:V2EPdZPliZymNAn79T8RkNApBjMmVKh5XRpLm/w98Vk=
426426
github.com/nishanths/predeclared v0.2.2/go.mod h1:RROzoN6TnGQupbC+lqggsOlcgysk3LMK/HI84Mp280c=
427-
github.com/nitrictech/nitric/core v0.0.0-20240915234849-42c1e482ddab h1:59YTNUa6en385Y21SwMTaixbe31SwgzjkjEhxx0RuwQ=
428-
github.com/nitrictech/nitric/core v0.0.0-20240915234849-42c1e482ddab/go.mod h1:N274XVBjYhGEQoT42baWM6/lETBQYQhqPpqUuk2gmLc=
427+
github.com/nitrictech/nitric/core v0.0.0-20240913000004-5d21c28b00ba h1:ZIPl9waqhbqw3xB2+zpUI2T1kEHyMkOnZZMt6tdrEUM=
428+
github.com/nitrictech/nitric/core v0.0.0-20240913000004-5d21c28b00ba/go.mod h1:4LQH9hea9rX+0A+8G47NRk5nZuXCDqiwci1aZsHAkU8=
429+
github.com/nitrictech/nitric/core v0.0.0-20241003062412-76ea6275fb0b h1:ImQFk66gRM3v9A6qmPImOiV3HJMDAX93X5rplMKn6ok=
430+
github.com/nitrictech/nitric/core v0.0.0-20241003062412-76ea6275fb0b/go.mod h1:9bQnYPqLzq8CcPk5MHT3phg19CWJhDlFOfdIv27lwwM=
429431
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf h1:8MB8W8ylM8sCM2COGfiO39/tB6BTdiawLszaUGCNL5w=
430432
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf/go.mod h1:b2lzk2a4o1bvSrSCE6yvTldHuXCJymuDVhdMJGOSslw=
431433
github.com/nunnatsa/ginkgolinter v0.16.2 h1:8iLqHIZvN4fTLDC0Ke9tbSZVcyVHoBs0HIbnVSxfHJk=
@@ -481,6 +483,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
481483
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
482484
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
483485
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
486+
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
484487
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
485488
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
486489
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
@@ -642,6 +645,7 @@ go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
642645
go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0=
643646
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
644647
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
648+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
645649
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
646650
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
647651
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=

mocks/batch.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitric/apis/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ type Route interface {
4747
ApiName() string
4848
}
4949

50-
type Handler = handlers.Handler[Ctx]
51-
type Middleware = handlers.Middleware[Ctx]
50+
type (
51+
Handler = handlers.Handler[Ctx]
52+
Middleware = handlers.Middleware[Ctx]
53+
)
5254

5355
type route struct {
5456
path string

nitric/batch/batch.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2021 Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package batch
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/nitrictech/go-sdk/internal/handlers"
21+
"github.com/nitrictech/go-sdk/nitric/workers"
22+
batchpb "github.com/nitrictech/nitric/core/pkg/proto/batch/v1"
23+
v1 "github.com/nitrictech/nitric/core/pkg/proto/resources/v1"
24+
)
25+
26+
// JobPermission defines the available permissions on a job
27+
type JobPermission string
28+
29+
type Handler = handlers.Handler[Ctx]
30+
31+
const (
32+
// JobSubmit is required to call Submit on a job.
33+
JobSubmit JobPermission = "submit"
34+
)
35+
36+
type JobReference interface {
37+
// Allow requests the given permissions to the job.
38+
Allow(permission JobPermission, permissions ...JobPermission) *BatchClient
39+
40+
// Handler will register and start the job task handler that will be called for all task submitted to this job.
41+
// Valid function signatures for middleware are:
42+
//
43+
// func()
44+
// func() error
45+
// func(*batch.Ctx)
46+
// func(*batch.Ctx) error
47+
// Handler[batch.Ctx]
48+
Handler(handler interface{}, options ...HandlerOption)
49+
}
50+
51+
type jobReference struct {
52+
name string
53+
manager *workers.Manager
54+
registerChan <-chan workers.RegisterResult
55+
}
56+
57+
// NewJob creates a new job resource with the give name.
58+
func NewJob(name string) JobReference {
59+
job := &jobReference{
60+
name: name,
61+
manager: workers.GetDefaultManager(),
62+
}
63+
64+
job.registerChan = job.manager.RegisterResource(&v1.ResourceDeclareRequest{
65+
Id: &v1.ResourceIdentifier{
66+
Type: v1.ResourceType_Job,
67+
Name: name,
68+
},
69+
Config: &v1.ResourceDeclareRequest_Job{
70+
Job: &v1.JobResource{},
71+
},
72+
})
73+
74+
return job
75+
}
76+
77+
func (j *jobReference) Allow(permission JobPermission, permissions ...JobPermission) *BatchClient {
78+
allPerms := append([]JobPermission{permission}, permissions...)
79+
80+
actions := []v1.Action{}
81+
for _, perm := range allPerms {
82+
switch perm {
83+
case JobSubmit:
84+
actions = append(actions, v1.Action_JobSubmit)
85+
default:
86+
panic(fmt.Errorf("JobPermission %s unknown", perm))
87+
}
88+
}
89+
90+
registerResult := <-j.registerChan
91+
if registerResult.Err != nil {
92+
panic(registerResult.Err)
93+
}
94+
95+
err := j.manager.RegisterPolicy(registerResult.Identifier, actions...)
96+
if err != nil {
97+
panic(err)
98+
}
99+
100+
client, err := NewBatchClient(j.name)
101+
if err != nil {
102+
panic(err)
103+
}
104+
105+
return client
106+
}
107+
108+
func (j *jobReference) Handler(handler interface{}, opts ...HandlerOption) {
109+
options := &handlerOptions{}
110+
111+
for _, opt := range opts {
112+
opt(options)
113+
}
114+
115+
registrationRequest := &batchpb.RegistrationRequest{
116+
JobName: j.name,
117+
Requirements: &batchpb.JobResourceRequirements{},
118+
}
119+
120+
if options.cpus != nil {
121+
registrationRequest.Requirements.Cpus = *options.cpus
122+
}
123+
124+
if options.memory != nil {
125+
registrationRequest.Requirements.Memory = *options.memory
126+
}
127+
128+
if options.gpus != nil {
129+
registrationRequest.Requirements.Gpus = *options.gpus
130+
}
131+
132+
typedHandler, err := handlers.HandlerFromInterface[Ctx](handler)
133+
if err != nil {
134+
panic(err)
135+
}
136+
137+
jobOpts := &jobWorkerOpts{
138+
RegistrationRequest: registrationRequest,
139+
Handler: typedHandler,
140+
}
141+
142+
worker := newJobWorker(jobOpts)
143+
j.manager.AddWorker("JobWorker:"+j.name, worker)
144+
}

nitric/batch/batch_suite_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2023 Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package batch_test
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
func TestBatch(t *testing.T) {
25+
RegisterFailHandler(Fail)
26+
RunSpecs(t, "Batch (Jobs) Suite")
27+
}

0 commit comments

Comments
 (0)