func (s *DiskvSpooler) Start(sz Serializer) error { s.status.Update("data-spooler", "Starting") // Create the data dir if necessary. Normally the manager does this, // but it's necessary to create it here for testing. if err := pct.MakeDir(s.dataDir); err != nil { return err } // Create basedir/trash/data/ for Reject(). s.trashDataDir = path.Join(s.trashDir, "data") if err := pct.MakeDir(s.trashDataDir); err != nil { return err } // T{} -> []byte s.sz = sz // diskv reads all files in BasePath on startup. s.cache = diskv.New(diskv.Options{ BasePath: s.dataDir, Transform: func(s string) []string { return []string{} }, CacheSizeMax: DEFAULT_DATA_MAX_SIZE, Index: &diskv.LLRBIndex{}, IndexLess: func(a, b string) bool { return a < b }, }) s.mux.Lock() s.updateStats() s.mux.Unlock() go s.run() s.logger.Info("Started") return nil }
func (m *Manager) Start() error { m.mux.Lock() defer m.mux.Unlock() if m.config != nil { return pct.ServiceIsRunningError{Service: "data"} } m.status.Update("data", "Starting") // Load config from disk (optional, but should exist). config := &Config{} if err := pct.Basedir.ReadConfig("data", config); err != nil { if !os.IsNotExist(err) { return err } } if err := m.validateConfig(config); err != nil { return err } // Make data and trash dirs used/shared by all services (mm, qan, etc.). if err := pct.MakeDir(m.dataDir); err != nil { return err } if err := pct.MakeDir(m.trashDir); err != nil { return err } // Make data serializer/encoder, e.g. T{} -> gzip -> []byte. sz, err := makeSerializer(config.Encoding) if err != nil { return err } // Make persistent (disk-back) key-value cache and start data spooler. m.status.Update("data", "Starting spooler") spooler := NewDiskvSpooler( pct.NewLogger(m.logger.LogChan(), "data-spooler"), m.dataDir, m.trashDir, m.hostname, config.Limits, ) if err := spooler.Start(sz); err != nil { return err } m.spooler = spooler // Start data sender. m.status.Update("data", "Starting sender") sender := NewSender( pct.NewLogger(m.logger.LogChan(), "data-sender"), m.client, ) if err := sender.Start(m.spooler, time.Tick(time.Duration(config.SendInterval)*time.Second), config.SendInterval, config.Blackhole); err != nil { return err } m.sender = sender m.config = config m.running = true m.logger.Info("Started") m.status.Update("data", "Running") return nil }