Skip to content

Instantly share code, notes, and snippets.

@frame-lang
Last active May 30, 2022 14:20
Show Gist options
  • Save frame-lang/c3ee8fd4e64098ca23a32e7e7e2432f1 to your computer and use it in GitHub Desktop.
Save frame-lang/c3ee8fd4e64098ca23a32e7e7e2432f1 to your computer and use it in GitHub Desktop.
// Package p contains a Pub/Sub Cloud Function.
package trafficlight
import (
"context"
"log"
"os"
"strconv"
"time"
"cloud.google.com/go/pubsub"
"github.com/gomodule/redigo/redis"
)
type AttributeType struct {
ConnectionID string `json:"connectionID"`
Event string `json:"event"`
}
type PubSubMessage struct {
Data []byte `json:"data"`
Attributes AttributeType `json:"attributes"`
}
var (
topic *pubsub.Topic
client *pubsub.Client
connectionID string
redisPool *redis.Pool
cloudFunctionID string
)
func init() {
// For testing
cloudFunctionID = strconv.FormatInt(time.Now().UTC().UnixNano(), 10)
log.Println("A new cloud function is being inilialized: ", cloudFunctionID)
// err is pre-declared to avoid shadowing client.
var err error
// client is initialized with context.Background() because it should
// persist between function invocations.
client, err = pubsub.NewClient(ctx, os.Getenv("PROJECTID"))
if err != nil {
log.Fatalf("pubsub.NewClient: %v", err)
}
topic = client.Topic(os.Getenv("TOPICID"))
// Initialize Redis
var redisError error
redisPool, redisError = initializeRedis()
if redisError != nil {
log.Printf("initializeRedis: %v", err)
return
}
}
func EntryPoint(ctx context.Context, m PubSubMessage) error {
connectionID = m.Attributes.ConnectionID
var event string = m.Attributes.Event
var isInit bool = false
log.Println("Connection ID ->", connectionID, ", Event ->", event, "Cloud function ID ->", cloudFunctionID)
if event == "init" {
isInit = true
}
trafficLightMom := NewTrafficLightMom(isInit)
if event == "tick" {
trafficLightMom.Tick()
} else if event == "error" {
trafficLightMom.SystemError()
} else if event == "restart" {
trafficLightMom.SystemRestart()
} else if event == "end" {
trafficLightMom.End()
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment