Skip to content
Snippets Groups Projects
Select Git revision
  • b497e343222b726d8e0044586af798e793b93539
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

lambda.go

Blame
  • lambda.go 1.11 KiB
    package cron
    
    import (
    	"context"
    	"math/rand"
    	"time"
    
    	"github.com/aws/aws-lambda-go/lambdacontext"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/service"
    )
    
    type LambdaCronHandler func(lambdaCtx context.Context) error
    
    func (cron Cron) Handler(lambdaCtx context.Context) error {
    	lc, _ := lambdacontext.FromContext(lambdaCtx)
    	requestID := lc.AwsRequestID
    
    	cronName, cronFunc := cron.router.Route(lc.InvokedFunctionArn)
    	if cronFunc == nil {
    		return errors.Errorf("request-id:%s unknown cron function(%s)", lc.InvokedFunctionArn)
    	}
    
    	//got a handler, prepare to run:
    	rand.Seed(time.Now().Unix())
    
    	ctx := Context{
    		Context: service.NewContext(lambdaCtx, map[string]interface{}{
    			"env":        cron.env,
    			"request-id": requestID,
    			"cron":       cronName,
    		}),
    		Name: cronName,
    	}
    
    	//report handler crashes
    	defer cron.crashReporter.Catch(ctx)
    
    	//todo: set log level, trigger log on conditions, sync at end of transaction - after log level was determined
    	ctx.Infof("Start CRON Handler")
    
    	if err := cronFunc(ctx); err != nil {
    		return errors.Wrapf(err, "Cron(%s) failed", cronName)
    	}
    	return nil
    }