From 8a924a0161c1a6783fc015bfe28c1342c0cc7a46 Mon Sep 17 00:00:00 2001
From: Jan Semmelink <jan@uafrica.com>
Date: Wed, 13 Oct 2021 11:53:15 +0200
Subject: [PATCH] In cron, read env CRON_MESSAGE_TYPE for routing

---
 cron/lambda.go | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/cron/lambda.go b/cron/lambda.go
index ea2fb6f..6378b55 100644
--- a/cron/lambda.go
+++ b/cron/lambda.go
@@ -3,6 +3,7 @@ package cron
 import (
 	"context"
 	"math/rand"
+	"os"
 	"time"
 
 	"github.com/aws/aws-lambda-go/lambdacontext"
@@ -16,9 +17,17 @@ func (cron Cron) Handler(lambdaCtx context.Context) (err error) {
 	lc, _ := lambdacontext.FromContext(lambdaCtx)
 	requestID := lc.AwsRequestID
 
-	cronName, cronFunc := cron.router.Route(lc.InvokedFunctionArn)
+	//if env CRON_MESSAGE_TYPE is defined, this instance only process that type
+	//if not, try to use ARN from the event context which allows one instance to
+	//call any handler
+	routeKey := os.Getenv("CRON_MESSAGE_TYPE")
+	if routeKey == "" {
+		routeKey = lc.InvokedFunctionArn
+	}
+
+	cronName, cronFunc := cron.router.Route(routeKey)
 	if cronFunc == nil {
-		return errors.Errorf("request-id:%s unknown cron function(%s)", requestID, lc.InvokedFunctionArn)
+		return errors.Errorf("request-id:%s unknown cron function(%s)", requestID, routeKey)
 	}
 
 	//got a handler, prepare to run:
-- 
GitLab