Selaa lähdekoodia

add error checking for backend processor initialization

flashmob 8 vuotta sitten
vanhempi
commit
0d1e35c004
2 muutettua tiedostoa jossa 28 lisäystä ja 5 poistoa
  1. 25 4
      backends/backend.go
  2. 3 1
      backends/gateway.go

+ 25 - 4
backends/backend.go

@@ -105,11 +105,27 @@ func (s Shutdown) Shutdown() error {
 	return s()
 }
 
+type Errors []error
+
+// implement the Error interface
+func (e Errors) Error() string {
+	if len(e) == 1 {
+		return e[0].Error()
+	}
+	// multiple errors
+	msg := ""
+	for _, err := range e {
+		msg += "\n" + err.Error()
+	}
+	return msg
+}
+
 type BackendService struct {
 	Initializers []ProcessorInitializer
 	Shutdowners  []ProcessorShutdowner
 	sync.Mutex
-	mainlog atomic.Value
+	mainlog    atomic.Value
+	initErrors Errors
 }
 
 // Get loads the log.logger in an atomic operation. Returns a stderr logger if not able to load
@@ -139,13 +155,18 @@ func (b *BackendService) AddShutdowner(i ProcessorShutdowner) {
 	b.Shutdowners = append(b.Shutdowners, i)
 }
 
-// Initialize initializes all the processors by
-func (b *BackendService) Initialize(backend BackendConfig) {
+// Initialize initializes all the processors one-by-one and returns any errors.
+func (b *BackendService) Initialize(backend BackendConfig) Errors {
 	b.Lock()
 	defer b.Unlock()
+	b.initErrors = nil
 	for i := range b.Initializers {
-		b.Initializers[i].Initialize(backend)
+		err := b.Initializers[i].Initialize(backend)
+		if err != nil {
+			b.initErrors = append(b.initErrors, err)
+		}
 	}
+	return b.initErrors
 }
 
 // Shutdown shuts down all the processor by calling their shutdowners

+ 3 - 1
backends/gateway.go

@@ -178,7 +178,9 @@ func (gw *BackendGateway) Initialize(cfg BackendConfig) error {
 			lines = append(lines, gw.newProcessorLine())
 		}
 		// initialize processors
-		Service.Initialize(cfg)
+		if err := Service.Initialize(cfg); err != nil {
+			return err
+		}
 		gw.saveMailChan = make(chan *savePayload, workersSize)
 		// start our savemail workers
 		gw.wg.Add(workersSize)