Browse Source

Make polling time configurable

master
Chris Smith 5 years ago
parent
commit
931d7c852d
2 changed files with 30 additions and 13 deletions
  1. 5
    1
      README.md
  2. 25
    12
      main.go

+ 5
- 1
README.md View File

20
 
20
 
21
 ```
21
 ```
22
 Usage of github-release-redirector:
22
 Usage of github-release-redirector:
23
+  -poll int
24
+    	the amount of time to wait between polling for releases; 0 to disable polling (default 3600)
23
   -port int
25
   -port int
24
     	the port to listen on for HTTP requests (default 8080)
26
     	the port to listen on for HTTP requests (default 8080)
25
   -redirect string
27
   -redirect string
34
 will be redirected to that URL, and no further processing will be done for
36
 will be redirected to that URL, and no further processing will be done for
35
 that request.
37
 that request.
36
 
38
 
37
-Releases are refreshed at startup and then once an hour.
39
+Releases are refreshed at startup and then, by default, once an hour. You can
40
+customise this duration with the `-poll` option; setting the poll time to `0`
41
+will disable polling entirely other than at startup.
38
 
42
 
39
 Assets are matched on their name, so an asset attached to the latest release
43
 Assets are matched on their name, so an asset attached to the latest release
40
 named "myproject-installer-1.2.3.exe" will be available at the URL
44
 named "myproject-installer-1.2.3.exe" will be available at the URL

+ 25
- 12
main.go View File

21
 	ctx context.Context
21
 	ctx context.Context
22
 	client *github.Client
22
 	client *github.Client
23
 	release *github.RepositoryRelease
23
 	release *github.RepositoryRelease
24
+	ticker *time.Ticker
24
 )
25
 )
25
 
26
 
26
 func fetchLatest() {
27
 func fetchLatest() {
29
 		log.Println("Error retrieving latest release", err)
30
 		log.Println("Error retrieving latest release", err)
30
 		return
31
 		return
31
 	}
32
 	}
33
+	log.Printf("Found latest release: %s\n", *latest.Name)
32
 	release = latest
34
 	release = latest
33
 }
35
 }
34
 
36
 
73
 	return nil
75
 	return nil
74
 }
76
 }
75
 
77
 
78
+func initTicker(seconds int) {
79
+	if seconds > 0 {
80
+		log.Printf("Starting ticker for polling once every %d seconds.\n", seconds)
81
+		ticker = time.NewTicker(time.Duration(seconds) * time.Second)
82
+		go func() {
83
+			for range ticker.C {
84
+				fetchLatest()
85
+			}
86
+		}()
87
+	} else {
88
+		log.Println("Not starting ticker; performing one off fetch and relying on webhooks.")
89
+	}
90
+	fetchLatest()
91
+}
92
+
76
 func main() {
93
 func main() {
77
 	redirect = flag.String("redirect", "", "if specified, requests for / will be redirected to this url")
94
 	redirect = flag.String("redirect", "", "if specified, requests for / will be redirected to this url")
78
 	var fullRepo = flag.String("repo", "", "the repository to redirect releases for, in user/repo format [required]")
95
 	var fullRepo = flag.String("repo", "", "the repository to redirect releases for, in user/repo format [required]")
79
 	var port = flag.Int("port", 8080, "the port to listen on for HTTP requests")
96
 	var port = flag.Int("port", 8080, "the port to listen on for HTTP requests")
97
+	var poll = flag.Int("poll", 3600, "the amount of time to wait between polling for releases; 0 to disable polling")
80
 
98
 
81
 	flag.Parse()
99
 	flag.Parse()
82
 
100
 
88
 
106
 
89
 	client = github.NewClient(nil)
107
 	client = github.NewClient(nil)
90
 
108
 
91
-	ticker := time.NewTicker(time.Hour)
92
-	go func() {
93
-		fetchLatest()
94
-		for range ticker.C {
95
-			fetchLatest()
96
-		}
97
-	}()
98
-
99
-
100
 	var cancel context.CancelFunc
109
 	var cancel context.CancelFunc
101
 	ctx, cancel = context.WithCancel(context.Background())
110
 	ctx, cancel = context.WithCancel(context.Background())
102
 
111
 
107
 	go func() {
116
 	go func() {
108
 		for sig := range c {
117
 		for sig := range c {
109
 			cancel()
118
 			cancel()
110
-			ticker.Stop()
111
-			log.Printf("Received %s, exiting.", sig.String())
119
+			if ticker != nil {
120
+				ticker.Stop()
121
+			}
122
+			log.Printf("Received %s, exiting.\n", sig.String())
112
 			os.Exit(0)
123
 			os.Exit(0)
113
 		}
124
 		}
114
 	}()
125
 	}()
115
 
126
 
116
-	log.Printf("Listing on :%d", *port)
127
+	initTicker(*poll)
128
+
129
+	log.Printf("Listing on :%d\n", *port)
117
 
130
 
118
 	server := &http.Server{
131
 	server := &http.Server{
119
 		Addr:    fmt.Sprintf(":%d", *port),
132
 		Addr:    fmt.Sprintf(":%d", *port),

Loading…
Cancel
Save