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,6 +20,8 @@ $ docker run -p 8080:8080 csmith/github-release-redirector -repo user/repo
20 20
 
21 21
 ```
22 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 25
   -port int
24 26
     	the port to listen on for HTTP requests (default 8080)
25 27
   -redirect string
@@ -34,7 +36,9 @@ If the `-redirect` option is specified, then requests to the root (i.e. `/`)
34 36
 will be redirected to that URL, and no further processing will be done for
35 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 43
 Assets are matched on their name, so an asset attached to the latest release
40 44
 named "myproject-installer-1.2.3.exe" will be available at the URL

+ 25
- 12
main.go View File

@@ -21,6 +21,7 @@ var (
21 21
 	ctx context.Context
22 22
 	client *github.Client
23 23
 	release *github.RepositoryRelease
24
+	ticker *time.Ticker
24 25
 )
25 26
 
26 27
 func fetchLatest() {
@@ -29,6 +30,7 @@ func fetchLatest() {
29 30
 		log.Println("Error retrieving latest release", err)
30 31
 		return
31 32
 	}
33
+	log.Printf("Found latest release: %s\n", *latest.Name)
32 34
 	release = latest
33 35
 }
34 36
 
@@ -73,10 +75,26 @@ func parseRepo(fullRepo *string) error {
73 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 93
 func main() {
77 94
 	redirect = flag.String("redirect", "", "if specified, requests for / will be redirected to this url")
78 95
 	var fullRepo = flag.String("repo", "", "the repository to redirect releases for, in user/repo format [required]")
79 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 99
 	flag.Parse()
82 100
 
@@ -88,15 +106,6 @@ func main() {
88 106
 
89 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 109
 	var cancel context.CancelFunc
101 110
 	ctx, cancel = context.WithCancel(context.Background())
102 111
 
@@ -107,13 +116,17 @@ func main() {
107 116
 	go func() {
108 117
 		for sig := range c {
109 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 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 131
 	server := &http.Server{
119 132
 		Addr:    fmt.Sprintf(":%d", *port),

Loading…
Cancel
Save