From 43a3bd98a1cd058304dbd9c0c35251babfda3302 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Mon, 25 Jan 2016 04:03:13 +0300 Subject: [PATCH] Handle task scheduling in case of premature process termination --- daemon.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/daemon.go b/daemon.go index c731aaa..b2e4f2e 100644 --- a/daemon.go +++ b/daemon.go @@ -71,23 +71,24 @@ func (d *BaseDaemon) Process(a Actor) { if d.limit != nil { d.limit.Wait(1) } - d.queue <- &task{ + + d.tryEnqueue(&task{ daemon: d.self, actor: a, createdAt: time.Now(), - } + }) } // SystemProcess creates a system task that is restarted in case of failure // and then adds it to processing queue. func (d *BaseDaemon) SystemProcess(name string, a Actor) { - d.queue <- &task{ + d.tryEnqueue(&task{ daemon: d.self, actor: a, createdAt: time.Now(), system: true, name: name, - } + }) } // Subscribe subscriasdsdfsdgdfgdfsg sdgsdfg sdfgs dfgdfgdfg. @@ -191,6 +192,16 @@ func (d *BaseDaemon) base() *BaseDaemon { return d } +func (d *BaseDaemon) tryEnqueue(t *task) { + defer func() { + if err := recover(); err != nil { + d.Logf("Failed to enqueue task %q due to process termination", t) + } + }() + + d.queue <- t +} + func (d *BaseDaemon) handlePanic(err interface{}) { if d.panicHandler != nil { d.panicHandler(err)