Skip to content
Snippets Groups Projects
Select Git revision
  • e6db4ae450eef9c94b26234d19057bab93168f9b
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

batch.go

Blame
  • 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 {