Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1000)

Unified Diff: worker/resumer/resumer.go

Issue 10266043: resumer: periodically resume transaction (Closed)
Patch Set: resumer: periodically resume transaction Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « worker/resumer/export_test.go ('k') | worker/resumer/resumer_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: worker/resumer/resumer.go
=== added file 'worker/resumer/resumer.go'
--- worker/resumer/resumer.go 1970-01-01 00:00:00 +0000
+++ worker/resumer/resumer.go 2013-06-19 11:49:55 +0000
@@ -0,0 +1,71 @@
+// Copyright 2012, 2013 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package resumer
+
+import (
+ "fmt"
+ "launchpad.net/juju-core/log"
+ "launchpad.net/tomb"
+ "time"
+)
+
+// defaultInterval is the standard value for the interval setting.
+const defaultInterval = time.Minute
+
+// interval sets how often the resuming is called.
+var interval = defaultInterval
+
+// TransactionResumer defines the interface for types capable to
+// resume transactions.
+type TransactionResumer interface {
+ // ResumeTransactions resumes all pending transactions.
+ ResumeTransactions() error
+}
+
+// Resumer is responsible for a periodical resuming of pending transactions.
+type Resumer struct {
+ tomb tomb.Tomb
+ tr TransactionResumer
+}
+
+// NewResumer periodically resumes pending transactions.
+func NewResumer(tr TransactionResumer) *Resumer {
+ rr := &Resumer{tr: tr}
+ go func() {
+ defer rr.tomb.Done()
+ rr.tomb.Kill(rr.loop())
+ }()
+ return rr
+}
+
+func (rr *Resumer) String() string {
+ return fmt.Sprintf("resumer")
+}
+
+func (rr *Resumer) Kill() {
+ rr.tomb.Kill(nil)
+}
+
+func (rr *Resumer) Stop() error {
+ rr.tomb.Kill(nil)
+ return rr.tomb.Wait()
+}
+
+func (rr *Resumer) Wait() error {
+ return rr.tomb.Wait()
+}
+
+func (rr *Resumer) loop() error {
+ for {
+ select {
+ case <-rr.tomb.Dying():
+ return tomb.ErrDying
+ case <-time.After(interval):
+ if err := rr.tr.ResumeTransactions(); err != nil {
+ log.Errorf("worker/resumer: cannot resume transactions: %v", err)
+ }
+ }
+ }
+ panic("unreachable")
+}
« no previous file with comments | « worker/resumer/export_test.go ('k') | worker/resumer/resumer_test.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b