-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
58 lines (44 loc) · 1.22 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
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/Sirupsen/logrus"
"github.com/bitly/go-nsq"
"github.com/optonaut/cron-server/config"
"github.com/optonaut/shared-server/utils"
"github.com/optonaut/shared-server/workerdata"
)
func job() {
fmt.Println("Every hour")
}
func main() {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
cfg := config.Parse()
producer, err := nsq.NewProducer(cfg.NSQd.Host+":"+cfg.NSQd.TCPPort, nsq.NewConfig())
if err != nil {
logrus.WithField("error", err).Fatal("Could not connect to nsq")
}
producer.SetLogger(utils.Logger{logrus.WithFields(logrus.Fields{})}, nsq.LogLevelInfo)
for {
if err := producer.Ping(); err != nil {
logrus.WithField("error", err).Error("Trying to connect to nsq...")
time.Sleep(500 * time.Millisecond)
} else {
break
}
}
logrus.Info("Connected to nsq")
t := time.NewTicker(time.Hour)
for {
payload := workerdata.CronHourlyPayload{
Time: time.Now(),
}
data, _ := json.Marshal(payload)
if err := producer.Publish(workerdata.CronHourlyTopic, data); err != nil {
logrus.WithField("error", err).Error("couldn't write message to queue")
}
logrus.WithField("payload", payload).Info("cron hourly")
<-t.C
}
}