Skip to content

Commit e6e343d

Browse files
authored
Implement webhook call to reload proxy (#51)
1 parent b5f64d6 commit e6e343d

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

handlers/getStatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var initTime = time.Now()
15-
var version = "1.13.1"
15+
var version = "1.14.0"
1616

1717
func getStatus(w http.ResponseWriter, r *http.Request) {
1818
json.NewEncoder(w).Encode(map[string]interface{}{

main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ func main() {
1818
flagAdminToken := flag.String("admintoken", "", "Admin token to allow certain endpoints")
1919
flagCacheTTL := flag.Uint("cachettl", 180, "Cache TTL in minutes")
2020
flagMaintenanceTTL := flag.Uint("maintenancettl", 5, "Allows to limit how frequently scraper can check for maintenance end in minutes")
21+
flagMaxTasksPerClient := flag.Uint("maxtasksperclient", 5, "Maximum number of scraping tasks per client")
2122
flagMongo := flag.String("mongo", "", "MongoDB connection string for loggig")
2223
flagPort := flag.Uint("port", 8001, "Port to catch requests on")
2324
flagProxy := flag.String("proxy", "", "Open proxy address to make requests to BDO servers")
25+
flagProxyReloadWebhook := flag.String("proxyreloadwebhook", "", "Webhook address to request proxy reload")
2426
flagRateLimit := flag.Uint64("ratelimit", 512, "Maximum number of requests per minute per IP")
25-
flagVerbose := flag.Bool("verbose", false, "Print out additional logs into stdout")
2627
flagTaskRetries := flag.Uint("taskretries", 3, "Number of retries for a scraping task")
27-
flagMaxTasksPerClient := flag.Uint("maxtasksperclient", 5, "Maximum number of scraping tasks per client")
28+
flagVerbose := flag.Bool("verbose", false, "Print out additional logs into stdout")
2829
flag.Parse()
2930

3031
// Read port from flags and env
@@ -52,6 +53,7 @@ func main() {
5253
viper.Set("maintenancettl", time.Duration(*flagMaintenanceTTL)*time.Minute)
5354
viper.Set("maxtasksperclient", int(*flagMaxTasksPerClient))
5455
viper.Set("mongo", *flagMongo)
56+
viper.Set("proxyreloadwebhook", *flagProxyReloadWebhook)
5557
viper.Set("ratelimit", int64(*flagRateLimit))
5658
viper.Set("taskretries", int(*flagTaskRetries))
5759
viper.Set("verbose", *flagVerbose)

scraper/scraper.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ func InitScraper() {
8282
if imperva {
8383
taskRetries, _ := strconv.Atoi(body.Request.Ctx.Get("taskRetries"))
8484
logger.Error(fmt.Sprintf("Hit Imperva while loading %v, retries: %v", body.Request.URL.String(), taskRetries))
85-
taskQueue.Pause(time.Duration(60-time.Now().Second()) * time.Second)
85+
if proxyReloadWebhook := viper.GetString("proxyreloadwebhook"); proxyReloadWebhook != "" {
86+
utils.SendDummyRequest(proxyReloadWebhook)
87+
taskQueue.Pause(time.Second * 5)
88+
} else {
89+
taskQueue.Pause(time.Duration(60-time.Now().Second()) * time.Second)
90+
}
8691
taskQueue.ConfirmTaskCompletion(taskClient, taskHash)
8792

8893
if taskRetries < viper.GetInt("taskretries") {

utils/SendRequest.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package utils
2+
3+
import "net/http"
4+
5+
func SendDummyRequest(url string) error {
6+
req, err := http.NewRequest("POST", url, nil)
7+
if err != nil {
8+
return err
9+
}
10+
11+
client := &http.Client{}
12+
resp, err := client.Do(req)
13+
if err != nil {
14+
return err
15+
}
16+
resp.Body.Close()
17+
return nil
18+
}

0 commit comments

Comments
 (0)