github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/resumer/resumer.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package resumer 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/juju/juju/worker" 11 "github.com/juju/loggo" 12 "launchpad.net/tomb" 13 ) 14 15 var logger = loggo.GetLogger("juju.worker.resumer") 16 17 // defaultInterval is the standard value for the interval setting. 18 const defaultInterval = time.Minute 19 20 // interval sets how often the resuming is called. 21 var interval = defaultInterval 22 23 // TransactionResumer defines the interface for types capable to 24 // resume transactions. 25 type TransactionResumer interface { 26 // ResumeTransactions resumes all pending transactions. 27 ResumeTransactions() error 28 } 29 30 // Resumer is responsible for a periodical resuming of pending transactions. 31 type Resumer struct { 32 tomb tomb.Tomb 33 tr TransactionResumer 34 } 35 36 // NewResumer periodically resumes pending transactions. 37 var NewResumer = func(tr TransactionResumer) worker.Worker { 38 rr := &Resumer{tr: tr} 39 go func() { 40 defer rr.tomb.Done() 41 rr.tomb.Kill(rr.loop()) 42 }() 43 return rr 44 } 45 46 func (rr *Resumer) String() string { 47 return fmt.Sprintf("resumer") 48 } 49 50 func (rr *Resumer) Kill() { 51 rr.tomb.Kill(nil) 52 } 53 54 func (rr *Resumer) Wait() error { 55 return rr.tomb.Wait() 56 } 57 58 func (rr *Resumer) loop() error { 59 for { 60 select { 61 case <-rr.tomb.Dying(): 62 return tomb.ErrDying 63 case <-time.After(interval): 64 // TODO(fwereade): 2016-03-17 lp:1558657 65 if err := rr.tr.ResumeTransactions(); err != nil { 66 logger.Errorf("cannot resume transactions: %v", err) 67 } 68 } 69 } 70 }