diff options
author | Ruben Pollan <meskio@sindominio.net> | 2018-06-15 18:02:04 +0200 |
---|---|---|
committer | Ruben Pollan <meskio@sindominio.net> | 2018-06-19 12:39:39 +0200 |
commit | 63aab32a7525677dda8ea553c1b71b75d15b4eeb (patch) | |
tree | 6949742c49ce3db71673abf2be8d3f146ebdddc6 | |
parent | 5687154a9581325d5a9382a592e793218dba1a74 (diff) |
[bug] wait for bitmaskd to be launched
In the snap, launching both bitmaskd and the systray at once bitmaskd
might not be ready to answer http posts when the systray starts.
- Resolves: #46
-rw-r--r-- | bitmask/main.go | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/bitmask/main.go b/bitmask/main.go index e303adb..8acab2f 100644 --- a/bitmask/main.go +++ b/bitmask/main.go @@ -45,6 +45,12 @@ func Init() (*Bitmask, error) { client := &http.Client{ Timeout: timeout, } + + err := waitForBitmaskd() + if err != nil { + return nil, err + } + apiToken, err := getToken() if err != nil { return nil, err @@ -68,6 +74,20 @@ func (b *Bitmask) Close() { } } +func waitForBitmaskd() error { + var err error + for i := 0; i < 30; i++ { + resp, err := http.Post(url, "", nil) + if err == nil { + resp.Body.Close() + return nil + } + log.Printf("Bitmask is not ready (iteration %i): %v", i, err) + time.Sleep(1 * time.Second) + } + return err +} + func (b *Bitmask) send(parts ...interface{}) (map[string]interface{}, error) { resJSON, err := send(b.apiToken, b.client, parts...) if err != nil { @@ -118,7 +138,15 @@ func parseResponse(resJSON []byte) (interface{}, error) { } func getToken() (string, error) { + var err error path := path.Join(ConfigPath, "authtoken") - b, err := ioutil.ReadFile(path) - return string(b), err + for i := 0; i < 30; i++ { + b, err := ioutil.ReadFile(path) + if err == nil { + return string(b), nil + } + log.Printf("Auth token is not ready (iteration %i): %v", i, err) + time.Sleep(1 * time.Second) + } + return "", err } |