-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an adapter for converting batch job IDs to log name IDs (#295)
- Loading branch information
Max Siegieda
authored
Nov 1, 2018
1 parent
2c7ee02
commit dc4beb4
Showing
4 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// package batchtologid takes an AWS Batch Job ID and returns the associated AWS CloudWatch Log Name | ||
package batchtologid | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/ReconfigureIO/platform/models" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/batch" | ||
) | ||
|
||
type Adapter struct { | ||
BatchRepo models.BatchRepo | ||
AWS interface { | ||
DescribeJobs( | ||
*batch.DescribeJobsInput, | ||
) ( | ||
*batch.DescribeJobsOutput, | ||
error, | ||
) | ||
} | ||
PollingPeriod time.Duration | ||
} | ||
|
||
// Do takes a batch job ID and returns the log name associated with that job. It | ||
// attempts to do this by querying batchRepo. It first waits for the batch job | ||
// to become started, which is a blocking operation. It then queries the batch | ||
// repo for the log name. If this is not available, it queries AWS for the log | ||
// name. | ||
func (a *Adapter) Do(ctx context.Context, batchID string) (string, error) { | ||
err := models.BatchAwaitStarted(ctx, a.BatchRepo, batchID, a.PollingPeriod) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
logname, err := a.BatchRepo.GetLogName(batchID) | ||
if err != nil { | ||
log.Printf("bidToLid: BatchRepo.GetLogName: %v \n", err) | ||
return "", err | ||
} | ||
if logname != "" { | ||
return logname, nil | ||
} | ||
|
||
resp, err := a.AWS.DescribeJobs(&batch.DescribeJobsInput{ | ||
Jobs: aws.StringSlice([]string{batchID}), | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
if len(resp.Jobs) == 0 { | ||
return "", fmt.Errorf("bidToLid: There is no AWS Batch Job with ID %v", batchID) | ||
} | ||
|
||
if resp.Jobs[0].Container.LogStreamName == nil { | ||
return "", errors.New("BatchToLogID: Adapter.Do: Got nil LogStreamName from AWS Batch") | ||
} | ||
err = a.BatchRepo.SetLogName(batchID, *resp.Jobs[0].Container.LogStreamName) | ||
if err != nil { | ||
log.Printf("bidToLid: BatchRepo.SetLogName: %v \n", err) | ||
} | ||
return *resp.Jobs[0].Container.LogStreamName, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package batchtologid | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ReconfigureIO/platform/models" | ||
"github.com/aws/aws-sdk-go/service/batch" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
var logName = "foobarLogName" | ||
var batchID = "foobarBatchID" | ||
|
||
type fakeAWS struct{} | ||
|
||
func (aws *fakeAWS) DescribeJobs(input *batch.DescribeJobsInput) (*batch.DescribeJobsOutput, error) { | ||
return &batch.DescribeJobsOutput{ | ||
Jobs: []*batch.JobDetail{ | ||
&batch.JobDetail{ | ||
Container: &batch.ContainerDetail{ | ||
LogStreamName: &logName, | ||
}, | ||
}, | ||
}, | ||
}, nil | ||
} | ||
|
||
func TestBatchToLogID(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
batchRepo := models.NewMockBatchRepo(mockCtrl) | ||
batchRepo.EXPECT().HasStarted(batchID).Return(true, nil) | ||
batchRepo.EXPECT().GetLogName(batchID).Return("", nil) | ||
batchRepo.EXPECT().SetLogName(batchID, logName).Return(nil) | ||
|
||
b2l := Adapter{ | ||
BatchRepo: batchRepo, | ||
AWS: &fakeAWS{}, | ||
PollingPeriod: time.Microsecond, | ||
} | ||
|
||
ctxtimeout, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) | ||
defer cancel() | ||
|
||
returned, err := b2l.Do(ctxtimeout, batchID) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if returned != logName { | ||
t.Errorf("Returned log name did not match expected value. Returned: %v Expected: %v \n", returned, logName) | ||
} | ||
} |