Skip to content
Snippets Groups Projects
Verified Commit 63aab32a authored by meskio's avatar meskio :tent:
Browse files

[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
parent 5687154a
No related branches found
Tags 0.18.9
No related merge requests found
Pipeline #
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment