-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
74 lines (60 loc) · 1.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//+build !test,!heroku
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dougEfresh/lambdazap"
)
var lambdazapper *lambdazap.LambdaLogContext
// HandleRequest handle "AWS lambda" request with a single event message or
// a batch of event messages.
func HandleRequest(ctx context.Context, e Event) {
defer func() {
logger.Sync()
wg.Wait()
logger.Sync()
}()
e.handle()
return
}
func main() {
if os.Getenv("_LAMBDA_SERVER_PORT") != "" {
lambdazapper = lambdazap.New().With(lambdazap.AwsRequestID)
logger.With(lambdazapper.NonContextValues()...)
log = logger.Sugar()
}
lambda.Start(HandleRequest)
}
func init() {
go func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGPIPE, syscall.SIGKILL, syscall.SIGTERM)
TASK_HANDLING:
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()
}
log.Info("service terminated")
break TASK_HANDLING
}
}
close(sc)
logger.Sync()
}()
}