-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
153 lines (116 loc) · 3.46 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
package main
import (
"log"
"os"
"os/signal"
"regexp"
"syscall"
"github.com/rycus86/release-watcher/config"
"github.com/rycus86/release-watcher/env"
"github.com/rycus86/release-watcher/model"
"github.com/rycus86/release-watcher/notifications"
"github.com/rycus86/release-watcher/providers"
"github.com/rycus86/release-watcher/store"
"github.com/rycus86/release-watcher/watcher"
"github.com/rycus86/release-watcher/webhooks"
)
var (
releaseChannel = make(chan []model.Release)
signalChannel = make(chan os.Signal, 1)
shutdownChannel = make(chan struct{})
)
func StartWatchers(configuration *model.Configuration) {
for providerName, projects := range configuration.Releases {
provider := providers.GetProvider(providerName)
if provider == nil {
log.Panicln("Provider not found:", providerName)
}
for _, project := range projects {
go WatchReleases(provider, project)
}
}
}
func WatchReleases(provider model.Provider, project model.GenericProject) {
rw, ok := provider.(watcher.ReleaseWatcher)
if !ok {
log.Println("The", provider.GetName(), "provider cannot watch releases")
return
}
watcher.WatchReleases(rw, project, releaseChannel, shutdownChannel)
}
func WaitForChanges(
db model.Store,
notifier notifications.NotificationManager,
webhookSender webhooks.WebhookSender,
reloadHandler func()) {
for {
select {
case releases := <-releaseChannel:
hasNewRelease := false
for _, release := range releases {
filter := release.Project.GetFilter()
matched, err := regexp.MatchString(filter, release.Name)
if !matched || err != nil {
continue
}
if db.Exists(release) {
break
}
log.Println(
"[", release.Provider.GetName(), "]",
"New release :", release.Project, ":", release.Name)
if err := db.Mark(release); err != nil {
log.Println("Failed to save the new version:", err)
}
webhookSender.Send(&release)
if !hasNewRelease {
hasNewRelease = true
if err := notifier.SendNotification(&release); err != nil {
log.Println("Failed to send notifications:", err)
}
}
}
case s := <-signalChannel:
if s == syscall.SIGHUP {
log.Println("Reload signal received")
reloadHandler()
} else {
log.Println("Shutdown signal received")
close(shutdownChannel)
return
}
}
}
}
func main() {
signal.Notify(signalChannel, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
dbPath := env.Lookup("DATABASE_PATH", "/var/secrets/release-watcher", "file::memory:?cache=shared")
db, err := store.Initialize(dbPath)
if err != nil {
log.Panicln("Failed to initialize the database:", err)
}
defer db.Close()
providers.InitializeProviders()
configPath := env.Lookup("CONFIGURATION_FILE", "/var/secrets/release-watcher", "release-watcher.yml")
configuration, err := config.ParseConfigurationFile(configPath)
if err != nil {
log.Panicln("Failed load the configuration file:", err)
}
notifier := notifications.NewNotificationManager()
defer notifier.Close()
webhookSender := webhooks.NewWebhookSender()
reloadHandler := func() {
close(shutdownChannel)
shutdownChannel = make(chan struct{})
config.Reload(configuration)
StartWatchers(configuration)
}
StartWatchers(configuration)
log.Println(
"Started watching releases using",
len(providers.GetProviders()), "providers",
)
WaitForChanges(db, notifier, webhookSender, reloadHandler)
log.Println("Application exiting")
}