From 701cac997c1cadfd497a54968deb40e7ecfa4e55 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Mon, 25 Jan 2016 04:15:17 +0300 Subject: [PATCH] Startup daemons using system tasks --- shezmu.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/shezmu.go b/shezmu.go index 854e0c7..36faa2a 100644 --- a/shezmu.go +++ b/shezmu.go @@ -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) } }()