From d1473fd3beed30c4a4e2fef0861fb172ee243d99 Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 10 Nov 2017 16:55:39 +0100 Subject: [PATCH] Issue #219: Fix waiting for save on shutdown Improve the logic License: MIT Signed-off-by: Hector Sanjuan --- config/config.go | 13 +++++++------ ipfs-cluster-service/main.go | 5 +++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index ba3ef5a1..a9a2a797 100644 --- a/config/config.go +++ b/config/config.go @@ -118,10 +118,11 @@ func (cfg *Manager) watchSave(save <-chan struct{}) { defer ticker.Stop() thingsToSave := false - exit := false for { select { + case <-save: + thingsToSave = true case <-ticker.C: if thingsToSave { err := cfg.SaveJSON("") @@ -130,13 +131,13 @@ func (cfg *Manager) watchSave(save <-chan struct{}) { } thingsToSave = false } - if exit { + + // Exit if we have to + select { + case <-cfg.ctx.Done(): return + default: } - case <-save: - thingsToSave = true - case <-cfg.ctx.Done(): - exit = true } } } diff --git a/ipfs-cluster-service/main.go b/ipfs-cluster-service/main.go index 80561f69..d63ea9af 100644 --- a/ipfs-cluster-service/main.go +++ b/ipfs-cluster-service/main.go @@ -264,6 +264,9 @@ func run(c *cli.Context) error { func daemon(c *cli.Context) error { // Load all the configurations cfg, clusterCfg, apiCfg, ipfshttpCfg, consensusCfg, monCfg, diskInfCfg, numpinInfCfg := makeConfigs() + // always wait for configuration to be saved + defer cfg.Shutdown() + err := cfg.LoadJSONFromFile(configPath) checkErr("loading configuration", err) @@ -323,8 +326,6 @@ func daemon(c *cli.Context) error { //case <-cluster.Ready(): } } - // wait for configuration to be saved - cfg.Shutdown() return nil }