Skip to content

Commit

Permalink
move r2-pump from GCP function to binary
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed Mar 5, 2024
1 parent 5d42894 commit 5fcc79b
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 14 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ jobs:
asset_name: algolia-pump.zip
asset_content_type: application/zip

- uses: actions/upload-release-asset@v1
name: release r2-pump function
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./functions/r2-pump/r2-pump.zip
asset_name: r2-pump.zip
asset_content_type: application/zip

- uses: actions/upload-release-asset@v1
name: release force-update function
env:
Expand Down Expand Up @@ -113,6 +103,16 @@ jobs:
asset_name: git-sync
asset_content_type: application/octet-stream

- uses: actions/upload-release-asset@v1
name: release r2-pump
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./bin/r2-pump
asset_name: r2-pump
asset_content_type: application/octet-stream

test:
name: Test
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define generate-func-make
endef

.PHONY: all
all: bin/process-version-host bin/git-sync bin/checker \
all: bin/process-version-host bin/git-sync bin/checker bin/r2-pump \
;$(foreach n,${CLOUD_FUNCTIONS},$(call generate-func-make,$n))

bin/checker:
Expand All @@ -22,6 +22,9 @@ bin/process-version-host:
bin/process-version:
go build $(GO_BUILD_ARGS) -o bin/process-version ./cmd/process-version

bin/r2-pump:
go build $(GO_BUILD_ARGS) -o bin/r2-pump ./cmd/r2-pump

.PHONY: schema
schema:
./bin/packages human > schema_human.json
Expand Down
74 changes: 74 additions & 0 deletions cmd/r2-pump/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Wrapper around a Golang GCP function allowing to be running as a binary
package main

import (
"context"
"encoding/json"
"log"
"os"
"runtime"

"github.com/cdnjs/tools/gcp"
"github.com/cdnjs/tools/sentry"

r2_pump "github.com/cdnjs/tools/functions/r2-pump"

"cloud.google.com/go/pubsub"
"github.com/pkg/errors"
)

var (
PROJECT = os.Getenv("PROJECT")
SUBSCRIPTION = os.Getenv("SUBSCRIPTION")
)

func init() {
sentry.Init()
}

func main() {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, PROJECT)
if err != nil {
log.Fatalf("could not create pubsub Client: %v", err)
}
sub := client.Subscription(SUBSCRIPTION)
sub.ReceiveSettings.Synchronous = true
sub.ReceiveSettings.MaxOutstandingMessages = 5
sub.ReceiveSettings.NumGoroutines = runtime.NumCPU()

for {
log.Printf("started consuming messages\n")
if err := consume(ctx, client, sub); err != nil {
log.Fatalf("could not pull messages: %s", err)
}
}
}

type Message struct {
GCSEvent gcp.GCSEvent `json:"gcsEvent"`
}

func consume(ctx context.Context, client *pubsub.Client, sub *pubsub.Subscription) error {
err := sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) {
log.Printf("received message: %s\n", msg.Data)

msg.Ack()
if err := processMessage(ctx, msg.Data); err != nil {
log.Printf("failed to process message: %s\n", err)
}
})
if err != nil {
return errors.Wrap(err, "could not receive from subscription")
}
return nil
}

func processMessage(ctx context.Context, data []byte) error {
var message Message
if err := json.Unmarshal(data, &message); err != nil {
return errors.Wrap(err, "failed to parse")
}

return r2_pump.Invoke(ctx, message.GCSEvent)
}
14 changes: 11 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ require (
github.com/GoogleCloudPlatform/functions-framework-go v1.2.0 // indirect
github.com/agnivade/levenshtein v1.1.1
github.com/algolia/algoliasearch-client-go/v3 v3.4.0
github.com/aws/aws-sdk-go-v2 v1.25.2 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.5 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.5 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.51.2 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/cdnjs/tools/functions/r2-pump v0.0.0-20240208115153-5d42894d8a8c // indirect
github.com/cloudevents/sdk-go v0.10.0 // indirect
github.com/cloudflare/cloudflare-go v0.12.1
github.com/cloudflare/cloudflare-go v0.69.0
github.com/containerd/containerd v1.4.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
Expand All @@ -19,18 +24,21 @@ require (
github.com/docker/go-units v0.4.0 // indirect
github.com/getsentry/sentry-go v0.6.1
github.com/go-git/go-git/v5 v5.3.0
github.com/gobwas/glob v0.2.3 // indirect
github.com/gobwas/glob v0.2.3
github.com/google/go-github v17.0.0+incompatible
github.com/karrick/godirwalk v1.15.6
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/net v0.20.0
golang.org/x/oauth2 v0.0.0-20210413134643-5e61552d6c78
golang.org/x/tools v0.17.0 // indirect
google.golang.org/api v0.45.0
google.golang.org/genproto v0.0.0-20210423144448-3a41ef94ed2b // indirect
google.golang.org/grpc v1.37.0 // indirect
)

replace github.com/cdnjs/tools => ./
Loading

0 comments on commit 5fcc79b

Please sign in to comment.