score-sync-zqds: It works! + some optimizations
This commit is contained in:
parent
22e25a384a
commit
debfd2a894
2 changed files with 38 additions and 18 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"path"
|
||||
"syscall"
|
||||
|
||||
"golang.org/x/oauth2/clientcredentials"
|
||||
"gopkg.in/fsnotify.v1"
|
||||
)
|
||||
|
||||
|
|
@ -27,9 +28,11 @@ func main() {
|
|||
roundId = v
|
||||
}
|
||||
|
||||
var clientId string
|
||||
if v, exists := os.LookupEnv("ZQDS_CLIENTID"); exists {
|
||||
clientId = v
|
||||
}
|
||||
var clientSecret string
|
||||
if v, exists := os.LookupEnv("ZQDS_CLIENTSECRET"); exists {
|
||||
clientSecret = v
|
||||
}
|
||||
|
|
@ -46,6 +49,13 @@ func main() {
|
|||
|
||||
TeamsDir = path.Clean(TeamsDir)
|
||||
|
||||
configOauth = clientcredentials.Config{
|
||||
ClientID: clientId,
|
||||
ClientSecret: clientSecret,
|
||||
Scopes: []string{"score:update"},
|
||||
TokenURL: TokenURL,
|
||||
}
|
||||
|
||||
log.Println("Registering directory events...")
|
||||
watcher, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
|
|
@ -57,8 +67,10 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
|
||||
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||
if !skipInitialSync {
|
||||
if _, err := os.Stat(path.Join(TeamsDir, "teams.json")); err == nil {
|
||||
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||
}
|
||||
}
|
||||
|
||||
// Register SIGUSR1, SIGUSR2
|
||||
|
|
@ -74,7 +86,7 @@ func main() {
|
|||
treatAll(path.Join(TeamsDir, "teams.json"))
|
||||
log.Println("SIGHUP treated.")
|
||||
case ev := <-watcher.Events:
|
||||
if ev.Name == "teams.json" {
|
||||
if path.Base(ev.Name) == "teams.json" {
|
||||
if ev.Op&watchedNotify == watchedNotify {
|
||||
if *debugINotify {
|
||||
log.Println("Treating event:", ev, "for", ev.Name)
|
||||
|
|
@ -83,6 +95,7 @@ func main() {
|
|||
} else if ev.Op&fsnotify.Write == fsnotify.Write {
|
||||
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
||||
watchedNotify = fsnotify.Write
|
||||
go treatDiff(ev.Name)
|
||||
} else if *debugINotify {
|
||||
log.Println("Skipped teams.json event:", ev)
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue