Skip to content

Commit

Permalink
remade worker clean daily questions
Browse files Browse the repository at this point in the history
  • Loading branch information
kish1n committed Aug 23, 2024
1 parent f3f344d commit 890371d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
20 changes: 10 additions & 10 deletions internal/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ func Run(args []string) bool {
gracefulStop := make(chan os.Signal, 1)
signal.Notify(gracefulStop, syscall.SIGTERM, syscall.SIGINT)

wgch := make(chan struct{})
go func() {
wg.Wait()
close(wgch)
}()

select {
case <-ctx.Done():
cfg.Log().WithError(ctx.Err()).Info("Interrupt signal received")
case <-func() chan struct{} {
ch := make(chan struct{})
go func() {
wg.Wait()
close(ch)
}()
return ch
}():
cfg.Log().Warn("All services stopped")
stop()
<-wgch
case <-wgch:
cfg.Log().Warn("all services stopped")
}

stop()
return true
}
2 changes: 1 addition & 1 deletion internal/cli/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func runServices(ctx context.Context, cfg config.Config, wg *sync.WaitGroup) {
run(func() { leveljustice.Run(cfg, levelJustice) })

//service for cleaning daily question deadlines after day
run(func() { cleanquestiondeadlines.Run(cfg, cleanDQuestionDeadlines) })
run(func() { cleanquestiondeadlines.Run(ctx, cfg, cleanDQuestionDeadlines) })

// service depends on all the workers for good UX
<-expiryWatchSig
Expand Down
8 changes: 7 additions & 1 deletion internal/service/workers/cleanquestiondeadlines/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cleanquestiondeadlines

import (
"context"
"time"

"github.com/rarimo/geo-points-svc/internal/config"
)

func Run(cfg config.Config, sig chan struct{}) {
func Run(ctx context.Context, cfg config.Config, sig chan struct{}) {
offset := cfg.DailyQuestions().Timezone

for {
Expand All @@ -30,6 +31,11 @@ func Run(cfg config.Config, sig chan struct{}) {
cfg.Log().Info("Daily Question cleaning stop")
timer.Stop()
return

case <-ctx.Done():
cfg.Log().Info("Daily Question cleaning stop")
timer.Stop()
return
}
}
}

0 comments on commit 890371d

Please sign in to comment.