|
| 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 | +} |
0 commit comments