-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move r2-pump from GCP function to binary
- Loading branch information
Showing
5 changed files
with
212 additions
and
14 deletions.
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,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) | ||
} |
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
Oops, something went wrong.