-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
203 lines (169 loc) · 5.56 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"airqo-integrator/config"
"airqo-integrator/controllers"
"airqo-integrator/models"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jmoiron/sqlx"
"github.com/robfig/cron/v3"
"github.com/samply/golang-fhir-models/fhir-models/fhir"
log "github.com/sirupsen/logrus"
"os"
"sync"
"time"
)
func init() {
formatter := new(log.TextFormatter)
formatter.TimestampFormat = time.RFC3339
formatter.FullTimestamp = true
log.SetFormatter(formatter)
log.SetOutput(os.Stdout)
}
type LocationEntry struct {
FullURL string `json:"fullUrl"`
Resource fhir.Location `json:"resource"`
}
var splash = `
┏━┓╻┏━┓┏━┓┏━┓ ╺┳╸┏━┓ ╺┳┓╻ ╻╻┏━┓┏━┓
┣━┫┃┣┳┛┃┓┃┃ ┃ ┃ ┃ ┃ ┃┃┣━┫┃┗━┓┏━┛
╹ ╹╹╹┗╸┗┻┛┗━┛ ╹ ┗━┛ ╺┻┛╹ ╹╹┗━┛┗━╸
`
func testing(msg string) {
log.Infof("Hi %s, You're testing schedules task at %v", msg, time.Now().Format("15:04:05"))
}
func main() {
fmt.Printf(splash)
dbConn, err := sqlx.Connect("postgres", config.AirQoIntegratorConf.Database.URI)
if err != nil {
log.Fatalln(err)
}
// log.WithField("DHIS2_SERVER_CONFIGS", config.MFLDHIS2ServersConfigMap).Info("SERVER: =======>")
LoadServersFromConfigFiles(config.AIRQODHIS2ServersConfigMap)
// log.WithFields(log.Fields{"Servers": models.ServerMapByName["localhost"]}).Info("SERVERS==>>")
// os.Exit(1)
go func() {
if !*config.SkipSync {
LoadOuLevels()
LoadOuGroups()
LoadAttributes()
LoadLocations() // Load organisation units - before facility in base DHIS2 instance
// SyncLocationsToDHIS2Instances()
}
_ = models.LoadSites()
_ = models.LoadGrids()
// SendAirQoClimateData()
if !*config.SkipFectchingByDate {
startDate, err := time.Parse("2006-01-02", *config.StartDate)
if err != nil {
fmt.Println("Error parsing start date:", err)
return
}
endDate, err := time.Parse("2006-01-02", *config.EndDate)
if err != nil {
fmt.Println("Error parsing end date:", err)
return
}
SendAirQoClimateData2(startDate, endDate)
}
}()
go func() {
// Create a new scheduler
c := cron.New()
if !*config.SkipSync {
_, err := c.AddFunc(config.AirQoIntegratorConf.API.AIRQOSyncCronExpression, func() {
SendAirQoClimateData2(time.Now().Add(-24*time.Hour), time.Now())
})
if err != nil {
log.WithError(err).Error("Error scheduling measurements sync task:")
return
}
}
if !*config.SkipRequestProcessing {
_, err := c.AddFunc(config.AirQoIntegratorConf.API.AIRQORetryCronExpression, func() {
RetryIncompleteRequests()
})
if err != nil {
log.WithError(err).Error("Error scheduling incomplete request retry task:")
}
}
c.Start()
}()
jobs := make(chan int)
var wg sync.WaitGroup
seenMap := make(map[models.RequestID]bool)
mutex := &sync.Mutex{}
rWMutex := &sync.RWMutex{}
if !*config.SkipRequestProcessing {
// don't produce anything if skip processing is enabled
// Start the producer goroutine
wg.Add(1)
go Produce(dbConn, jobs, &wg, mutex, seenMap)
// Start the consumer goroutine
wg.Add(1)
go StartConsumers(jobs, &wg, rWMutex, seenMap)
}
scheduledJobs := make(chan int64)
workingOn := make(map[int64]bool)
var workingOnMutex = &sync.Mutex{}
var rWworkingOnMutex = &sync.RWMutex{}
if !*config.SkipScheduleProcessing {
wg.Add(1)
go ProduceSchedules(dbConn, scheduledJobs, &wg, workingOnMutex, workingOn)
wg.Add(1)
go StartScheduleConsumers(scheduledJobs, &wg, rWworkingOnMutex, workingOn)
}
// Start the backend API gin server
if !*config.DisableHTTPServer {
wg.Add(1)
go startAPIServer(&wg)
}
wg.Wait()
close(scheduledJobs)
close(jobs)
}
func startAPIServer(wg *sync.WaitGroup) {
defer wg.Done()
router := gin.Default()
v2 := router.Group("/api", BasicAuth())
{
v2.GET("/test2", func(c *gin.Context) {
c.String(200, "Authorized")
})
tk := new(controllers.TokenController)
v2.GET("/getToken", tk.GetActiveToken)
v2.GET("/generateToken", tk.GenerateNewToken)
v2.DELETE("/deleteTokens", tk.DeleteInactiveTokens)
q := new(controllers.QueueController)
v2.POST("/queue", q.Queue)
v2.GET("/queue", q.Requests)
v2.GET("/queue/:id", q.GetRequest)
v2.DELETE("/queue/:id", q.DeleteRequest)
ou := new(controllers.OrgUnitController)
v2.POST("/organisationUnits", ou.OrgUnit)
v2.GET("/organisationUnits", ou.GetOrganisationUnits)
s := new(controllers.ServerController)
v2.POST("/servers", s.CreateServer)
v2.POST("/importServers", s.ImportServers)
ot := new(controllers.OrgUnitTreeController)
v2.GET("/outree/:server", ot.CreateOrgUnitTree)
at := new(controllers.AttributeController)
v2.GET("/syncAttributes/:server", at.SyncAttributes)
ad := new(controllers.AdminController)
v2.GET("/clearDistrictRequests/:district", ad.ClearRequestsByDistrict)
v2.GET("/clearBatchRequests/:batch", ad.ClearRequestsByBatch)
sc := new(controllers.ScheduleController)
v2.GET("/schedules", sc.ListSchedules)
v2.POST("/schedules", sc.NewSchedule)
v2.GET("/schedules/:id", sc.GetSchedule)
v2.POST("/schedules/:id", sc.UpdateSchedule)
v2.DELETE("/schedules/:id", sc.DeleteSchedule)
}
// Handle error response when a route is not defined
router.NoRoute(func(c *gin.Context) {
c.String(404, "Page Not Found!")
})
_ = router.Run(":" + fmt.Sprintf("%s", config.AirQoIntegratorConf.Server.Port))
}
//TIP See GoLand help at <a href="https://www.jetbrains.com/help/go/">jetbrains.com/help/go/</a>.
// Also, you can try interactive lessons for GoLand by selecting 'Help | Learn IDE Features' from the main menu.