-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (72 loc) · 2.06 KB
/
main.go
File metadata and controls
92 lines (72 loc) · 2.06 KB
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
87
88
89
90
91
92
package main
import (
"context"
"flag"
"log"
"os"
"gopkg.in/yaml.v2"
"github.com/sinderpl/AsyncTaskProcessor/api"
"github.com/sinderpl/AsyncTaskProcessor/queue"
"github.com/sinderpl/AsyncTaskProcessor/storage"
"github.com/sinderpl/AsyncTaskProcessor/task"
)
const defaultConfig = "config/ConfigurationLocal.yml"
var (
cfg = Config{}
)
type Config struct {
Api struct {
ListenAddr string `yaml:"listenAddr"`
} `yaml:"api"`
Queue struct {
MaxBufferSize int `yaml:"maxBufferSize"`
WorkerPoolSize int `yaml:"workerPoolSize,omitempty"`
MaxTaskRetry int `yaml:"maxTaskRetry"`
} `yaml:"queue"`
Storage struct {
Host string `yaml:"host"`
User string `yaml:"user"`
DBName string `yaml:"dbname"`
Password string `yaml:"password"`
} `yaml:"storage"`
}
func main() {
cfgPath := flag.String("cfg", defaultConfig, "specify which config file to point to")
flag.Parse()
cfgFile, err := os.ReadFile(*cfgPath)
if err != nil {
log.Fatalf("Failed to read cfg file: %v", err)
}
if err := yaml.Unmarshal(cfgFile, &cfg); err != nil {
log.Fatalf("Failed to unmarshal YAML cfg data: %v", err)
}
// TODO graceful shutdown
mainCtx := context.Background()
storage, err := storage.NewPostgresStore(cfg.Storage.Host, cfg.Storage.User, cfg.Storage.DBName, cfg.Storage.Password)
if err != nil {
log.Fatalf("Failed to initialise database: %v", err)
}
err = storage.Init()
if err != nil {
log.Fatalf("Failed to run database migration: %v", err)
}
taskChan := make(chan []*task.Task)
q, err := queue.CreateQueue(mainCtx,
queue.WithMainQueue(&taskChan),
queue.WithMaxBufferSize(cfg.Queue.MaxBufferSize),
queue.WithMaxWorkerPoolSize(cfg.Queue.WorkerPoolSize),
queue.WithMaxTaskRetry(cfg.Queue.MaxTaskRetry),
queue.WithStorage(storage))
if err != nil {
log.Fatalf("failed to initialize queue: %v", err)
}
q.Start()
server := api.CreateApiServer(
api.WithListenAddr(cfg.Api.ListenAddr),
api.WithQueue(&taskChan),
api.WithStorage(storage))
err = server.Run()
if err != nil {
log.Fatalf("failed to start up sever: %v", err)
}
}