diff --git a/internal/app/microfabd/config.go b/internal/app/microfabd/config.go index f74505a..7ce2f41 100644 --- a/internal/app/microfabd/config.go +++ b/internal/app/microfabd/config.go @@ -8,6 +8,7 @@ import ( "encoding/json" "os" "path" + "time" ) // Organization represents an organization in the configuration. @@ -32,6 +33,8 @@ type Config struct { CapabilityLevel string `json:"capability_level"` CouchDB bool `json:"couchdb"` CertificateAuthorities bool `json:"certificate_authorities"` + TimeoutString string `json:"timeout"` + Timeout time.Duration `json:"-"` } // DefaultConfig returns the default configuration. @@ -67,6 +70,7 @@ func DefaultConfig() (*Config, error) { CapabilityLevel: "V2_0", CouchDB: true, CertificateAuthorities: true, + TimeoutString: "30s", } if env, ok := os.LookupEnv("MICROFAB_CONFIG"); ok { err := json.Unmarshal([]byte(env), config) @@ -77,5 +81,10 @@ func DefaultConfig() (*Config, error) { if config.Port >= startPort && config.Port < endPort { logger.Fatalf("Cannot specify port %d, must be outside port range %d-%d", config.Port, 2000, 3000) } + timeout, err := time.ParseDuration(config.TimeoutString) + if err != nil { + return nil, err + } + config.Timeout = timeout return config, nil } diff --git a/internal/app/microfabd/microfabd.go b/internal/app/microfabd/microfabd.go index ee1cf8e..1c13c56 100644 --- a/internal/app/microfabd/microfabd.go +++ b/internal/app/microfabd/microfabd.go @@ -344,7 +344,7 @@ func (m *Microfab) createAndStartOrderer(organization *organization.Organization m.Lock() m.orderer = orderer m.Unlock() - err = orderer.Start(m.endorsingOrganizations) + err = orderer.Start(m.endorsingOrganizations, m.config.Timeout) if err != nil { return err } @@ -361,7 +361,7 @@ func (m *Microfab) waitForCouchDB() error { m.Lock() m.couchDB = couchDB m.Unlock() - err = couchDB.WaitFor() + err = couchDB.WaitFor(m.config.Timeout) if err != nil { return err } @@ -410,7 +410,7 @@ func (m *Microfab) createAndStartPeer(organization *organization.Organization, a m.Lock() m.peers = append(m.peers, peer) m.Unlock() - err = peer.Start() + err = peer.Start(m.config.Timeout) if err != nil { return err } @@ -437,7 +437,7 @@ func (m *Microfab) createAndStartCA(organization *organization.Organization, api m.Lock() m.cas = append(m.cas, theCA) m.Unlock() - err = theCA.Start() + err = theCA.Start(m.config.Timeout) if err != nil { return err } diff --git a/internal/pkg/ca/runtime.go b/internal/pkg/ca/runtime.go index 1e308d8..6c881ec 100644 --- a/internal/pkg/ca/runtime.go +++ b/internal/pkg/ca/runtime.go @@ -22,7 +22,7 @@ import ( ) // Start starts the peer. -func (c *CA) Start() error { +func (c *CA) Start(timeout time.Duration) error { logsDirectory := filepath.Join(c.directory, "logs") if err := os.MkdirAll(logsDirectory, 0755); err != nil { return err @@ -87,11 +87,11 @@ func (c *CA) Start() error { errchan <- err } }() - timeout := time.After(10 * time.Second) + timeoutCh := time.After(timeout) tick := time.Tick(250 * time.Millisecond) for { select { - case <-timeout: + case <-timeoutCh: c.Stop() return errors.New("timeout whilst waiting for CA to start") case err := <-errchan: diff --git a/internal/pkg/couchdb/couchdb.go b/internal/pkg/couchdb/couchdb.go index 04bdcde..576ddf4 100644 --- a/internal/pkg/couchdb/couchdb.go +++ b/internal/pkg/couchdb/couchdb.go @@ -28,12 +28,12 @@ func New(url string) (*CouchDB, error) { } // WaitFor waits for the CouchDB instance to start. -func (c *CouchDB) WaitFor() error { - timeout := time.After(10 * time.Second) +func (c *CouchDB) WaitFor(timeout time.Duration) error { + timeoutCh := time.After(timeout) tick := time.Tick(250 * time.Millisecond) for { select { - case <-timeout: + case <-timeoutCh: return errors.New("timeout whilst waiting for CouchDB to start") case <-tick: if c.hasStarted() { diff --git a/internal/pkg/orderer/runtime.go b/internal/pkg/orderer/runtime.go index 8da38fd..5d7cbb0 100644 --- a/internal/pkg/orderer/runtime.go +++ b/internal/pkg/orderer/runtime.go @@ -25,7 +25,7 @@ import ( ) // Start starts the orderer. -func (o *Orderer) Start(consortium []*organization.Organization) error { +func (o *Orderer) Start(consortium []*organization.Organization, timeout time.Duration) error { err := o.createDirectories() if err != nil { return err @@ -93,11 +93,11 @@ func (o *Orderer) Start(consortium []*organization.Organization) error { errchan <- err } }() - timeout := time.After(10 * time.Second) + timeoutCh := time.After(timeout) tick := time.Tick(250 * time.Millisecond) for { select { - case <-timeout: + case <-timeoutCh: o.Stop() return errors.New("timeout whilst waiting for orderer to start") case err := <-errchan: diff --git a/internal/pkg/peer/runtime.go b/internal/pkg/peer/runtime.go index b81949e..6fa59b6 100644 --- a/internal/pkg/peer/runtime.go +++ b/internal/pkg/peer/runtime.go @@ -22,7 +22,7 @@ import ( ) // Start starts the peer. -func (p *Peer) Start() error { +func (p *Peer) Start(timeout time.Duration) error { err := p.createDirectories() if err != nil { return err @@ -81,11 +81,11 @@ func (p *Peer) Start() error { errchan <- err } }() - timeout := time.After(10 * time.Second) + timeoutCh := time.After(timeout) tick := time.Tick(250 * time.Millisecond) for { select { - case <-timeout: + case <-timeoutCh: p.Stop() return errors.New("timeout whilst waiting for peer to start") case err := <-errchan: