Skip to content

Commit

Permalink
added task switching and closing on the termination
Browse files Browse the repository at this point in the history
  • Loading branch information
nad2000 committed Aug 8, 2019
1 parent bda0282 commit 2c7ba39
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion deployment/dev.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "aws_sqs_queue" "event_queue" {
}

# data "aws_api_gateway_api_key" "webhook_end_point_api_key" {
# id = "ru3mpjgse6"
# # id = "ru3mpjgse6"
# }


Expand Down
27 changes: 27 additions & 0 deletions handler/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"errors"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"unicode"

Expand Down Expand Up @@ -75,6 +77,31 @@ func init() {
ErrorOutputPaths: []string{"stderr"},
}.Build()
log = logger.Sugar()

go func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGPIPE, syscall.SIGKILL, syscall.SIGTERM)

for {
select {
case <-time.Tick(time.Minute * 10):
if taskID != 0 && taskRecordCount > 0 && time.Now().Sub(taskCreatedAt).Minutes() > taskRetentionMin {
taskSetUpWG.Add(1)
go (&Task{ID: taskID}).activateTask()
taskSetUpWG.Add(1)
go newTask()
taskSetUpWG.Done()
}
case <-sc:
if taskID != 0 && taskRecordCount > 0 && time.Now().Sub(taskCreatedAt).Minutes() > taskRetentionMin {
taskSetUpWG.Add(1)
go (&Task{ID: taskID}).activateTask()
taskSetUpWG.Done()
}
break
}
}
}()
}

func setup() {
Expand Down
15 changes: 0 additions & 15 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package main
import (
"context"
"os"
"os/signal"
"syscall"

"github.com/aws/aws-lambda-go/lambda"

Expand Down Expand Up @@ -38,18 +36,5 @@ func main() {
log = logger.Sugar()
}

sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGPIPE)

go func() {
<-sc
if taskID != 0 {
log.Info("====================== SIGPIPE ======================================================")
log.Infof("task (ID: %d) activated", taskID)
log.Info("====================== SIGPIPE ======================================================")
logger.Sync()
}
}()

lambda.Start(HandleRequest)
}

0 comments on commit 2c7ba39

Please sign in to comment.