/
background.go
62 lines (54 loc) · 2 KB
/
background.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
package core
import (
"time"
"github.com/Nextdoor/conductor/services/code"
"github.com/Nextdoor/conductor/services/data"
"github.com/Nextdoor/conductor/services/messaging"
"github.com/Nextdoor/conductor/services/phase"
"github.com/Nextdoor/conductor/services/ticket"
"github.com/Nextdoor/conductor/shared/datadog"
"github.com/Nextdoor/conductor/shared/flags"
)
const SyncTicketsInterval = time.Second * 10
const CheckJobsInterval = time.Second * 5
const CheckTrainLockInterval = time.Second * 5
// How long to wait until starting background tasks after boot, in seconds.
// This is useful when upgrading Conductor, to avoid race conditions when two instances are polling at once.
var backgroundTaskStartDelay = flags.EnvInt("BACKGROUND_TASK_START_DELAY", 0)
func backgroundTaskLoop() {
datadog.Info("Waiting %d seconds to start background jobs.", backgroundTaskStartDelay)
time.Sleep(time.Second * time.Duration(backgroundTaskStartDelay))
datadog.Info("Starting background jobs.")
// This loop handles restarting the background task loop if it ever panics.
killed := make(chan bool)
for {
go func() {
dataClient := data.NewClient()
codeService := code.GetService()
messagingService := messaging.GetService()
phaseService := phase.GetService()
ticketService := ticket.GetService()
syncTicketsTicker := time.NewTicker(SyncTicketsInterval)
checkJobsTicker := time.NewTicker(CheckJobsInterval)
checkTrainLockTicker := time.NewTicker(CheckTrainLockInterval)
defer func() {
err, stack := parsePanic(recover())
if err != nil {
datadog.Error("Panic in background task: %v. Stack trace: %v", err, stack)
}
killed <- true
}()
for {
select {
case <-syncTicketsTicker.C:
syncTickets(dataClient, codeService, messagingService, phaseService, ticketService)
case <-checkJobsTicker.C:
checkJobs(dataClient)
case <-checkTrainLockTicker.C:
checkTrainLock(dataClient, codeService, messagingService, phaseService, ticketService)
}
}
}()
<-killed
}
}