-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_worker.go
66 lines (58 loc) · 1.49 KB
/
fetch_worker.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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gorm.io/gorm"
)
func runFetchWorker(db *gorm.DB, api *GitHubAPI, notifier Notifier) error {
log.Println("Running fetch_worker")
start := time.Now()
// Expose simple http liveness check
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Late Night Commits fetch worker running since: %v", start)
})
log.Fatalln(http.ListenAndServe(":"+viper.GetString("port"), nil))
}()
// Run fetch job on a schedule
scheduler := NewScheduler()
jobs := []*JobDefinition{
{
Name: "daily_notifier",
Schedule: viper.GetString("daily_notifier.schedule"),
Run: func() error {
return runDailyNotification(db, notifier)
},
},
{
Name: "weekly_notifier",
Schedule: viper.GetString("weekly_notifier.schedule"),
Run: func() error {
return runDailyNotification(db, notifier)
},
},
{
Name: "fetch_commits",
Schedule: viper.GetString("fetch_worker.schedule"),
Run: func() error {
return runFetchJob(db, api)
},
},
}
for _, job := range jobs {
if err := scheduler.AddJob(job); err != nil {
if errors.Is(err, NoScheduleError) {
log.Printf("No schedule provided for %s worker, skipping", job.Name)
continue
}
return errors.Wrapf(err, "failed to schedule %s worker", job.Name)
}
log.Printf(`Scheduled %s worker to run on "%s"`, job.Name, job.Schedule)
}
scheduler.Run()
return nil
}