@@ -11,7 +11,6 @@ import (
1111 "time"
1212
1313 "github.com/MakeNowJust/heredoc"
14- "github.com/jackc/pgx/v5"
1514 "github.com/joho/godotenv"
1615 "github.com/spf13/cobra"
1716 "go.uber.org/automaxprocs/maxprocs"
@@ -30,6 +29,7 @@ import (
3029 "github.com/PythonHacker24/linux-acl-management-backend/internal/session"
3130 "github.com/PythonHacker24/linux-acl-management-backend/internal/transprocessor"
3231 "github.com/PythonHacker24/linux-acl-management-backend/internal/utils"
32+ "github.com/jackc/pgx/v5"
3333)
3434
3535func main () {
@@ -126,38 +126,9 @@ func run(ctx context.Context) error {
126126 wg sync.WaitGroup
127127 )
128128
129-
130-
131-
132- /* RULE: complete backend system must initiate before http server starts */
133-
134- /* DATABASE CONNECTIONS MUST BE MADE BEFORE SCHEDULER STARTS */
135- logRedisClient , err := redis .NewRedisClient (
136- config .BackendConfig .Database .TransactionLogRedis .Address ,
137- config .BackendConfig .Database .TransactionLogRedis .Password ,
138- config .BackendConfig .Database .TransactionLogRedis .DB ,
139- )
140- if err != nil {
141- zap .L ().Fatal ("Failed to connect to Redis" , zap .Error (err ))
142- }
143-
144- pqDB := fmt .Sprintf ("postgres://%s:%s@%s:%d/%s?sslmode=%s" ,
145- config .BackendConfig .Database .ArchivalPQ .User ,
146- config .BackendConfig .Database .ArchivalPQ .Password ,
147- config .BackendConfig .Database .ArchivalPQ .Host ,
148- config .BackendConfig .Database .ArchivalPQ .Port ,
149- config .BackendConfig .Database .ArchivalPQ .DBName ,
150- config .BackendConfig .Database .ArchivalPQ .SSLMode ,
151- )
152-
153- /* connect to PostgreSQL database */
154- connPQ , err := pgx .Connect (context .Background (), pqDB )
155- if err != nil {
156- fmt .Fprintf (os .Stderr , "Unable to connect to database: %v\n " , err )
157- os .Exit (1 )
158- }
159-
160- archivalPQ := postgresql .New (connPQ )
129+ /* create a context and waitgroup for the logging goroutine */
130+ logCtx , logCancel := context .WithCancel (context .Background ())
131+ var logWg sync.WaitGroup
161132
162133 /* create a error channel */
163134 errChLog := make (chan error , 1 )
@@ -168,17 +139,17 @@ func run(ctx context.Context) error {
168139 /* attempting to keep connections alive all the time even with no activity */
169140 var kacp = keepalive.ClientParameters {
170141 /* send pings every 10 seconds if there is no activity */
171- Time : 10 * time .Second ,
142+ Time : 10 * time .Second ,
172143
173144 /* wait 2 second for ping ack before considering the connection dead */
174- Timeout : 2 * time .Second ,
145+ Timeout : 2 * time .Second ,
175146
176147 /* send pings even without active streams */
177- PermitWithoutStream : true ,
148+ PermitWithoutStream : true ,
178149 }
179150
180151 pool := grpcpool .NewClientPool (
181- grpc .WithTransportCredentials (insecure .NewCredentials ()),
152+ grpc .WithTransportCredentials (insecure .NewCredentials ()),
182153 grpc .WithKeepaliveParams (kacp ),
183154 )
184155
@@ -187,14 +158,14 @@ func run(ctx context.Context) error {
187158 /* check if system is remote */
188159 if system .Remote != nil {
189160 address := fmt .Sprintf ("%s:%d" , system .Remote .Host , system .Remote .Port )
190- go func (addr string , errCh chan <- error ) {
161+ go func (addr string , errCh chan <- error ) {
191162 _ , err := pool .GetConn (addr , errCh )
192- if err != nil {
193- zap .L ().Error ("Failed to get connect with a daemon" ,
194- zap .String ("Address" , addr ),
163+ if err != nil {
164+ zap .L ().Error ("Failed to get connect with a daemon" ,
165+ zap .String ("Address" , addr ),
195166 zap .Error (err ),
196167 )
197- }
168+ }
198169
199170 /* now test for connections */
200171 zap .L ().Info ("Connected to" ,
@@ -203,7 +174,7 @@ func run(ctx context.Context) error {
203174
204175 }(address , errChLog )
205176 }
206- }
177+ }
207178
208179 /*
209180 initializing scheduler
@@ -212,16 +183,42 @@ func run(ctx context.Context) error {
212183 */
213184 errChShed := make (chan error , 1 )
214185
186+ logRedisClient , err := redis .NewRedisClient (
187+ config .BackendConfig .Database .TransactionLogRedis .Address ,
188+ config .BackendConfig .Database .TransactionLogRedis .Password ,
189+ config .BackendConfig .Database .TransactionLogRedis .DB ,
190+ )
191+ if err != nil {
192+ zap .L ().Fatal ("Failed to connect to Redis" , zap .Error (err ))
193+ }
194+
195+ pqDB := fmt .Sprintf ("postgres://%s:%s@%s:%d/%s?sslmode=%s" ,
196+ config .BackendConfig .Database .ArchivalPQ .User ,
197+ config .BackendConfig .Database .ArchivalPQ .Password ,
198+ config .BackendConfig .Database .ArchivalPQ .Host ,
199+ config .BackendConfig .Database .ArchivalPQ .Port ,
200+ config .BackendConfig .Database .ArchivalPQ .DBName ,
201+ config .BackendConfig .Database .ArchivalPQ .SSLMode ,
202+ )
203+
204+ connPQ , err := pgx .Connect (context .Background (), pqDB )
205+ if err != nil {
206+ fmt .Fprintf (os .Stderr , "Unable to connect to database: %v\n " , err )
207+ os .Exit (1 )
208+ }
209+
210+ archivalPQ := postgresql .New (connPQ )
211+
215212 /* create a session manager */
216213 sessionManager := session .NewManager (logRedisClient , archivalPQ , errChLog )
217214
218215 /* create a permissions processor */
219216 permProcessor := transprocessor .NewPermProcessor (pool , errChLog )
220217
221- /* handle session and processor errors */
222- wg .Add (1 )
218+ /* start logging goroutine - should be last to exit */
219+ logWg .Add (1 )
223220 go func (ctx context.Context ) {
224- defer wg .Done ()
221+ defer logWg .Done ()
225222 zap .L ().Info ("log error handler started" )
226223 for {
227224 select {
@@ -241,8 +238,7 @@ func run(ctx context.Context) error {
241238 return
242239 }
243240 }
244- /* update this to use different ctx called essentialctx which ends after everything is shutdown */
245- }(ctx )
241+ }(logCtx )
246242
247243 /* currently FCFS scheduler */
248244 transSched := fcfs .NewFCFSScheduler (sessionManager , permProcessor )
@@ -345,9 +341,12 @@ func run(ctx context.Context) error {
345341 /* close archival database connection */
346342 connPQ .Close (context .Background ())
347343
348- /* essentialctx must be closed here */
349-
350344 zap .L ().Info ("All background processes closed gracefully" )
351345
346+ /* close the logging error channel and cancel logging context */
347+ close (errChLog )
348+ logCancel ()
349+ logWg .Wait ()
350+
352351 return err
353352}
0 commit comments