Skip to content

Commit 0a72a90

Browse files
committed
Merge branch 'main' into grady/workinguploads
2 parents 6505758 + 9e88ed4 commit 0a72a90

File tree

14 files changed

+80
-35
lines changed

14 files changed

+80
-35
lines changed

azure/azblob/azblob.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,6 @@ func (c *Client) DeleteBlob(ctx context.Context, uri string) error {
101101
return nil
102102
}
103103

104-
func (c *Client) DeleteBlobs(ctx context.Context, uris []string) error {
105-
// TODO: Implement parallel delete if slow.
106-
for _, uri := range uris {
107-
if err := c.DeleteBlob(ctx, uri); err != nil {
108-
return err
109-
}
110-
}
111-
return nil
112-
}
113-
114104
// SignedUploadURL returns a URL that is allowed to upload to the given URI.
115105
// See https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/storage/azblob@v1.0.0/sas#example-package-UserDelegationSAS
116106
func (c *Client) SignedUploadURL(ctx context.Context, uri string) (string, error) {

cmd/runner/BUILD.bazel

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@ go_library(
2525
],
2626
)
2727

28-
go_binary(
29-
name = "runner",
30-
embed = [":runner_lib"],
31-
visibility = ["//visibility:public"],
32-
)
33-
3428
pkg_tar(
3529
name = "runner_tar",
3630
srcs = [":runner"],
@@ -72,3 +66,9 @@ oci_tarball(
7266
image = ":image",
7367
repo_tags = [],
7468
)
69+
70+
go_binary(
71+
name = "runner",
72+
embed = [":runner_lib"],
73+
visibility = ["//visibility:public"],
74+
)

cmd/runner/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ If you do want to actually run the full `runner` image on Azure, you can use:
1212
# Run the backend, tell it to create tasks as real Azure Container Apps Jobs.
1313
bazel run //scripts:run_apiserver -- --use_azure_runner
1414
```
15+
16+
### Creating a new docker image to run locally
17+
18+
```bash
19+
# Build the runner binary
20+
bazel build --@io_bazel_rules_go//go/config:pure //cmd/runner:image_tarball
21+
# Load the new image into docker, which will output a SHA256 value
22+
docker load < bazel-bin/cmd/runner/image_tarball/tarball.tar
23+
# Tag the runner image in order for it to be picked up locally. Don't push this to the registry!
24+
docker tag <SHA from previous step> rmisa.azurecr.io/runner
25+
```

cmd/runner/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ func (h *handler) parsePortfolio(ctx context.Context, taskID task.ID, req *task.
256256
id := uuid.New().String()
257257
// TODO: Probably set the CSV extension in the signed upload URL instead.
258258
destPath := filepath.Join("/", "mnt", "raw_portfolios", fmt.Sprintf("%s.csv", id))
259-
260259
if err := h.downloadBlob(ctx, string(srcURI), destPath); err != nil {
261260
return fmt.Errorf("failed to download raw portfolio blob: %w", err)
262261
}
@@ -292,7 +291,7 @@ func (h *handler) parsePortfolio(ctx context.Context, taskID task.ID, req *task.
292291
// NOTE: This code could benefit from some concurrency, but I'm opting not to prematurely optimize.
293292
var out []*task.ParsePortfolioResponseItem
294293
for _, p := range paths {
295-
lineCount, err := countLines(p)
294+
lineCount, err := countCSVLines(p)
296295
if err != nil {
297296
return fmt.Errorf("failed to count lines in file %q: %w", p, err)
298297
}
@@ -342,7 +341,7 @@ func (h *handler) parsePortfolio(ctx context.Context, taskID task.ID, req *task.
342341
return nil
343342
}
344343

345-
func countLines(path string) (int, error) {
344+
func countCSVLines(path string) (int, error) {
346345
file, err := os.Open(path)
347346
if err != nil {
348347
return 0, fmt.Errorf("opening file failed: %w", err)
@@ -356,7 +355,7 @@ func countLines(path string) (int, error) {
356355
if err := scanner.Err(); err != nil {
357356
return 0, fmt.Errorf("scanner.error returned: %w", err)
358357
}
359-
return lineCount, nil
358+
return lineCount - 1, nil // Subtract 1 for the header row
360359
}
361360

362361
func createReportReq() (*task.CreateReportRequest, error) {

cmd/runner/taskrunner/taskrunner.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,22 @@ func New(cfg *Config) (*TaskRunner, error) {
8383
}
8484

8585
func (tr *TaskRunner) ParsePortfolio(ctx context.Context, req *task.ParsePortfolioRequest) (task.ID, task.RunnerID, error) {
86-
var taskBuffer bytes.Buffer
87-
if err := json.NewEncoder(&taskBuffer).Encode(req); err != nil {
86+
var buf bytes.Buffer
87+
if err := json.NewEncoder(&buf).Encode(req); err != nil {
8888
return "", "", fmt.Errorf("failed to encode ParsePortfolioRequest: %w", err)
8989
}
90-
tr.logger.Info("triggering parse portfolio task", zap.Any("req", req))
90+
value := buf.String()
91+
if len(value) > 128*1024 {
92+
return "", "", fmt.Errorf("ParsePortfolioRequest is too large: %d bytes > 128 kb", len(value))
93+
}
9194
return tr.run(ctx, []task.EnvVar{
9295
{
9396
Key: "TASK_TYPE",
9497
Value: string(task.ParsePortfolio),
9598
},
9699
{
97100
Key: "PARSE_PORTFOLIO_REQUEST",
98-
Value: taskBuffer.String(),
101+
Value: value,
99102
},
100103
})
101104
}

cmd/server/pactasrv/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ go_library(
99
"initiative_user_relationship.go",
1010
"pacta_version.go",
1111
"pactasrv.go",
12+
"parallel.go",
1213
"portfolio.go",
1314
"upload.go",
1415
"user.go",

cmd/server/pactasrv/incomplete_upload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *Server) DeleteIncompleteUpload(ctx context.Context, request api.DeleteI
4343
if err != nil {
4444
return nil, oapierr.Internal("failed to delete incomplete upload", zap.Error(err))
4545
}
46-
if err := s.Blob.DeleteBlobs(ctx, []string{string(blobURI)}); err != nil {
46+
if err := s.deleteBlobs(ctx, []string{string(blobURI)}); err != nil {
4747
return nil, oapierr.Internal("failed to delete blob", zap.Error(err))
4848
}
4949
return api.DeleteIncompleteUpload204Response{}, nil

cmd/server/pactasrv/pactasrv.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ type DB interface {
9393
type Blob interface {
9494
Scheme() blob.Scheme
9595

96-
// For uploading portfolios
9796
SignedUploadURL(ctx context.Context, uri string) (string, error)
98-
// For downloading reports
9997
SignedDownloadURL(ctx context.Context, uri string) (string, error)
100-
DeleteBlobs(ctx context.Context, uris []string) error
98+
DeleteBlob(ctx context.Context, uri string) error
10199
}
102100

103101
type Server struct {

cmd/server/pactasrv/parallel.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pactasrv
2+
3+
import (
4+
"context"
5+
"fmt"
6+
)
7+
8+
type blobDeleter interface {
9+
DeleteBlob(ctx context.Context, uri string) error
10+
}
11+
12+
func deleteBlobs(ctx context.Context, bd blobDeleter, uris []string) error {
13+
// Implement parallel delete if slow - not prematurely optimizing.
14+
for i, uri := range uris {
15+
if err := bd.DeleteBlob(ctx, uri); err != nil {
16+
return fmt.Errorf("deleting blob %d/%d: %w", i, len(uris), err)
17+
}
18+
}
19+
return nil
20+
}
21+
22+
func (s *Server) deleteBlobs(ctx context.Context, uris []string) error {
23+
return deleteBlobs(ctx, s.Blob, uris)
24+
}

cmd/server/pactasrv/portfolio.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *Server) DeletePortfolio(ctx context.Context, request api.DeletePortfoli
4242
if err != nil {
4343
return nil, oapierr.Internal("failed to delete portfolio", zap.Error(err))
4444
}
45-
if err := s.Blob.DeleteBlobs(ctx, asStrs(blobURIs)); err != nil {
45+
if err := s.deleteBlobs(ctx, asStrs(blobURIs)); err != nil {
4646
return nil, oapierr.Internal("failed to delete blob", zap.Error(err))
4747
}
4848
return api.DeletePortfolio204Response{}, nil

db/sqldb/migrations/0005_json_blob_type.down.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ ALTER TABLE blob
1616
ALTER file_type TYPE file_type
1717
USING file_type::file_type;
1818

19-
COMMIT;
19+
COMMIT;

db/sqldb/sqldb_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func TestSchemaHistory(t *testing.T) {
8686
{ID: 2, Version: 2}, // 0002_create_user_table
8787
{ID: 3, Version: 3}, // 0003_domain_types
8888
{ID: 4, Version: 4}, // 0004_audit_log_tweaks
89+
{ID: 5, Version: 5}, // 0005_json_blob_type
8990
}
9091

9192
if diff := cmp.Diff(want, got); diff != "" {

pacta/enum_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package pacta
22

3-
import "testing"
3+
import (
4+
"path/filepath"
5+
"testing"
6+
)
47

58
func TestParseAuthMechanism(t *testing.T) {
69
testParseEnum(t, AuthnMechanismValues, ParseAuthnMechanism)
@@ -12,6 +15,20 @@ func TestParseLanguage(t *testing.T) {
1215

1316
func TestParseFileType(t *testing.T) {
1417
testParseEnum(t, FileTypeValues, ParseFileType)
18+
otherCases := []string{
19+
"hello/world.json",
20+
"hello/world/hithere.JsOn",
21+
" hello/world/hithere.json ",
22+
}
23+
for _, c := range otherCases {
24+
ft, err := ParseFileType(filepath.Ext(c))
25+
if err != nil {
26+
t.Errorf("expected successful parse, got %w", err)
27+
}
28+
if ft != FileType_JSON {
29+
t.Errorf("expected JSON, got %v", ft)
30+
}
31+
}
1532
}
1633

1734
// need

pacta/pacta.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ const (
204204
FileType_CSV = "csv"
205205
FileType_YAML = "yaml"
206206
FileType_ZIP = "zip"
207-
FileType_JSON = "json"
208207
FileType_HTML = "html"
208+
FileType_JSON = "json"
209209
)
210210

211211
var FileTypeValues = []FileType{
@@ -214,6 +214,7 @@ var FileTypeValues = []FileType{
214214
FileType_ZIP,
215215
FileType_JSON,
216216
FileType_HTML,
217+
FileType_JSON,
217218
}
218219

219220
func ParseFileType(s string) (FileType, error) {
@@ -228,12 +229,12 @@ func ParseFileType(s string) (FileType, error) {
228229
return FileType_YAML, nil
229230
case "zip":
230231
return FileType_ZIP, nil
231-
case "json":
232-
return FileType_JSON, nil
233232
case "html":
234233
return FileType_HTML, nil
234+
case "json":
235+
return FileType_JSON, nil
235236
}
236-
return "", fmt.Errorf("unknown FileType: %q", s)
237+
return "", fmt.Errorf("unknown pacta.FileType: %q", s)
237238
}
238239

239240
type BlobURI string

0 commit comments

Comments
 (0)