-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (73 loc) · 1.8 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
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
// nolint
var (
stop = make(chan os.Signal, 1)
exit = os.Exit
stderr io.Writer = os.Stderr
)
func main() {
logger := log.New(stderr, "", log.Lshortfile)
ctx, cancel := context.WithCancel(context.Background())
errorCnt := 0
defer cancel()
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
closeTime, err := time.ParseDuration(fmt.Sprintf("%ss", os.Getenv("COFFEE_SHOP_CLOSE_TIME")))
if err != nil {
logger.Println("please specify number, in seconds, for envvar COFFEE_SHOP_CLOSE_TIME")
errorCnt++
}
shutdown, err := time.ParseDuration(fmt.Sprintf("%ss", os.Getenv("COFFEE_SHOP_SHUTDOWN")))
if err != nil {
logger.Println("please specify number, in seconds, for envvar COFFEE_SHOP_SHUTDOWN")
errorCnt++
}
customers, err := strconv.Atoi(os.Getenv("COFFEE_SHOP_CUSTOMERS"))
if err != nil {
logger.Println("please specify number of customers for envvar COFFEE_SHOP_CUSTOMERS")
errorCnt++
}
baristas, err := strconv.Atoi(os.Getenv("COFFEE_SHOP_BARISTAS"))
if err != nil {
logger.Println("please specify number of baristas for envvar COFFEE_SHOP_BARISTAS")
}
if errorCnt != 0 {
exit(1)
return
}
s := NewStore(logger)
s.CloseAfter(closeTime)
s.Customers(RandomGroupOfCustomers(customers))
s.Baristas(RandomGroupOfBaristas(baristas))
complete, err := s.Open(ctx)
if err != nil {
exit(1)
return
}
time.Sleep(1 * time.Second)
select {
case <-stop:
logger.Println("I received a signal to close the store")
cancel()
select {
case <-time.After(shutdown):
logger.Println("Shutdown time reached, closing anyway")
<-complete
case <-complete:
logger.Println("Store closed")
}
case <-complete:
logger.Println("Store closed")
}
exit(0)
}