Select Git revision
batch.go 3.27 KiB
package batch
import (
"context"
"encoding/json"
"io"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/batch"
"github.com/aws/aws-sdk-go-v2/service/batch/types"
"github.com/go-resty/resty/v2"
"github.com/google/uuid"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/s3"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/utils"
)
const (
JobDefinition = "batch-job-definition" // The job definition name on aws
binaryPath = "/root/home/workers" // The worker binary path in the docker container
BatchJobMessageTypeS3 BatchJobMessageType = "s3"
)
// Queue names are the same as the batch Job queue names in aws
const (
BatchJobQueueLow BatchJobQueue = "batch-job-queue-low"
BatchJobQueueMedium BatchJobQueue = "batch-job-queue-medium"
BatchJobQueueHigh BatchJobQueue = "batch-job-queue-high"
)
type BatchJobQueue string
type BatchJobMessageType string
type BatchJob struct {
Name string
Job any
FullJobDefinition string
FullJobQueue string
MessagesBucketName string
IsDebug bool
}
func SubmitJob(batchJob BatchJob) error {
if batchJob.IsDebug {
go func() {
resty.New().R().
SetBody(batchJob.Job).
Post("http://127.0.0.1:3000/batch/")
}()
time.Sleep(time.Second * 1)
return nil
}
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("af-south-1"))
if err != nil {
return err
}
batchClient := batch.NewFromConfig(cfg)
if batchJob.Name == "" {
id := uuid.New()
jobID := "job" + id.String()
batchJob.Name = jobID
}
err = uploadMessageToS3(batchJob)
if err != nil {