1
0
Fork 0

Startup daemons using system tasks

This commit is contained in:
Gregory Eremin 2016-01-25 04:15:17 +03:00
parent 43a3bd98a1
commit 701cac997c
1 changed files with 22 additions and 2 deletions

View File

@ -94,7 +94,6 @@ func (s *Shezmu) AddDaemon(d Daemon) {
base.logger = s.Logger
base.shutdown = s.shutdownSystem
go d.Startup()
s.daemons = append(s.daemons, d)
}
@ -110,6 +109,11 @@ func (s *Shezmu) StartDaemons() {
for i := 0; i < s.NumWorkers; i++ {
go s.runWorker()
}
s.Logger.Println("Setting up daemons")
for _, d := range s.daemons {
s.setupDaemon(d)
}
}
// StopDaemons stops all running daemons.
@ -127,6 +131,22 @@ func (s *Shezmu) StopDaemons() {
fmt.Println(s.runtimeStats.Fetch(stats.Latency))
}
func (s *Shezmu) setupDaemon(d Daemon) {
defer func() {
if err := recover(); err != nil {
s.Logger.Printf("Failed to setup daemon %s due to process termination", d)
}
}()
s.queue <- &task{
daemon: d,
actor: d.Startup,
createdAt: time.Now(),
system: true,
name: "startup",
}
}
func (s *Shezmu) runWorker() {
s.wgWorkers.Add(1)
defer s.wgWorkers.Done()
@ -180,7 +200,7 @@ func (s *Shezmu) processSystemTask(t *task) {
t.createdAt = time.Now()
s.queue <- t // Restarting task
} else {
s.Logger.Printf("System task %s has stopped\n", t)
s.Logger.Printf("System task %s finished\n", t)
}
}()