-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron.go
130 lines (108 loc) · 2.88 KB
/
cron.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"log"
"os"
"runtime"
"strconv"
"strings"
"github.com/arifmahmudrana/csv-serve/cassandra"
"github.com/arifmahmudrana/csv-serve/csv"
"github.com/arifmahmudrana/csv-serve/memcached"
"github.com/arifmahmudrana/csv-serve/redis"
"github.com/robfig/cron/v3"
)
var (
workers = runtime.NumCPU()
)
const version = "1.0.0"
type application struct {
infoLog, errorLog *log.Logger
version string
db cassandra.CassandraRepository
m memcached.MemcachedRepository
r redis.RedisRepository
}
func (app *application) ConnectCassandra() {
cassandraMaxRetryConnect, err := strconv.Atoi(os.Getenv("CASSANDRA_MAX_RETRY_CONNECT"))
if err != nil {
app.errorLog.Fatal(err)
}
db, err := cassandra.NewCassandraRepository(
os.Getenv("CASSANDRA_USER"),
os.Getenv("CASSANDRA_PASSWORD"),
cassandraMaxRetryConnect,
strings.Split(os.Getenv("CASSANDRA_DB_HOST"), ",")...)
if err != nil {
app.errorLog.Fatal(err)
}
app.db = db
}
func (app *application) ProcessCSV() {
app.infoLog.Println("Running CRON to process CSV file...")
if err := app.db.Truncate(cassandra.TBL_PROMOTIONS); err != nil {
app.errorLog.Fatal(err)
}
r, err := os.Open(os.Getenv("DATA_FILE_PATH"))
if err != nil {
app.errorLog.Fatal(err)
}
defer r.Close()
csvRepository := csv.NewCSVRepository(r, workers, app.db.InsertPromotions)
err = csvRepository.Process()
if err == nil {
app.infoLog.Println("CRON run successfully")
go func() {
app.infoLog.Println("CRON deleting all memcached!!")
if err := app.m.DeleteAll(); err != nil {
app.errorLog.Println("Memcache: ", err)
return
}
app.infoLog.Println("CRON deleting all memcached successfully!!")
}()
go func() {
app.infoLog.Println("CRON deleting all redis!!")
if err := app.r.DeleteAll(); err != nil {
app.errorLog.Println("Redis: ", err)
return
}
app.infoLog.Println("CRON deleting all redis successfully!!")
}()
return
}
if os.Getenv("CRON_EXIT_ON_ERROR") == "true" {
app.errorLog.Fatal(err)
}
app.errorLog.Println(err)
}
func (app *application) ConnectMemcached() {
m, err := memcached.NewMemcachedRepository(
strings.Split(os.Getenv("MEMCACHED_SERVER"), ",")...)
if err != nil {
app.errorLog.Fatal(err)
}
app.m = m
}
func (app *application) ConnectRedis() {
r, err := redis.NewRedisRepository(
os.Getenv("REDIS_SERVER"), os.Getenv("REDIS_PREFIX"))
if err != nil {
app.errorLog.Fatal(err)
}
app.r = r
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
app := &application{
infoLog: log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime),
errorLog: log.New(os.Stdout, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile),
version: version,
}
app.ConnectCassandra()
defer app.db.Close()
app.ConnectMemcached()
app.ConnectRedis()
defer app.r.Close()
c := cron.New()
c.AddFunc(os.Getenv("CRON_SCHEDULE"), app.ProcessCSV)
c.Run()
}