Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
cdff38c1
Commit
cdff38c1
authored
1 year ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Add batch submit job
parent
4a1a9360
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
batch/batch.go
+126
-0
126 additions, 0 deletions
batch/batch.go
with
126 additions
and
0 deletions
batch/batch.go
0 → 100644
+
126
−
0
View file @
cdff38c1
package
batch
import
(
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/batch"
"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"
"io"
"time"
)
const
(
binaryPath
=
"/root/home/workers"
// The worker binary path in the docker container
)
type
BatchJobQueue
string
type
BatchJobMessageType
string
// 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"
)
const
(
BatchJobMessageTypeS3
BatchJobMessageType
=
"s3"
)
func
SubmitJob
(
name
*
string
,
job
any
,
fullJobDefinition
string
,
fullJobQueue
string
,
messagesBucketName
string
,
isDebug
bool
)
error
{
if
isDebug
{
go
func
()
{
resty
.
New
()
.
R
()
.
SetBody
(
job
)
.
Post
(
"http://127.0.0.1:3000/batch/"
)
}()
time
.
Sleep
(
time
.
Second
*
1
)
return
nil
}
options
:=
session
.
Options
{
Config
:
aws
.
Config
{
Region
:
utils
.
ValueToPointer
(
"af-south-1"
),
},
}
sess
,
err
:=
session
.
NewSessionWithOptions
(
options
)
batchClient
:=
batch
.
New
(
sess
)
if
name
==
nil
{
id
:=
uuid
.
New
()
jobID
:=
"job"
+
id
.
String
()
name
=
utils
.
ValueToPointer
(
jobID
)
}
err
=
uploadMessageToS3
(
*
name
,
job
,
messagesBucketName
,
isDebug
)
if
err
!=
nil
{
return
err
}
command
:=
[]
*
string
{
utils
.
ValueToPointer
(
binaryPath
),
name
,
}
environmentOverwrite
:=
[]
*
batch
.
KeyValuePair
{{
Name
:
utils
.
ValueToPointer
(
"BATCH_MESSAGE_TYPE"
),
Value
:
utils
.
ValueToPointer
(
string
(
BatchJobMessageTypeS3
)),
},
}
input
:=
&
batch
.
SubmitJobInput
{
JobDefinition
:
utils
.
ValueToPointer
(
fullJobDefinition
),
JobName
:
name
,
JobQueue
:
utils
.
ValueToPointer
(
fullJobQueue
),
ContainerOverrides
:
&
batch
.
ContainerOverrides
{
Command
:
command
,
Environment
:
environmentOverwrite
,
},
}
_
,
err
=
batchClient
.
SubmitJob
(
input
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
uploadMessageToS3
(
name
string
,
job
any
,
bucketName
string
,
isDebug
bool
)
error
{
jobBytes
,
err
:=
json
.
Marshal
(
job
)
if
err
!=
nil
{
return
err
}
// Upload message
_
,
err
=
s3
.
GetSession
(
isDebug
)
.
UploadWithSettingsRevised
(
jobBytes
,
bucketName
,
s3
.
S3UploadSettings
{
FileName
:
name
,
})
if
err
!=
nil
{
return
err
}
return
nil
}
func
RetrieveMessageFromS3
(
filename
string
,
messagesBucketName
string
,
isDebug
bool
)
([]
byte
,
error
)
{
// get the file contents
rawObject
,
err
:=
s3
.
GetSession
(
isDebug
)
.
GetObject
(
messagesBucketName
,
filename
,
isDebug
)
if
err
!=
nil
{
return
[]
byte
{},
err
}
defer
rawObject
.
Body
.
Close
()
// Read the file, unzip the data and unmarshall the json
var
bodyBytes
[]
byte
bodyBytes
,
err
=
io
.
ReadAll
(
rawObject
.
Body
)
if
err
!=
nil
{
logs
.
ErrorWithMsg
(
"Could not read file"
,
err
)
return
bodyBytes
,
err
}
return
bodyBytes
,
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment