Patch for signal handling to start a sync by Jim Pryor

Here's the way I'd like to use offlineimap on my laptop:
    1. Have a regular cron job running infrequently. The cron job
    checks to see
if I'm online, plugged in, and that no other copy of offlineimap is
running. If
all of these conditions are satisfied, it runs offlineimap just once:
"offlineimap -o -u Noninteractive.Quiet"

    2. When I start up mutt, I do it by calling a wrapper script that
    delays
until cron-started copies of offlineimap have finished, then starts
    offlineimap
on its regular, stay-alive and keep checking schedule. When I quit
    mutt, the
wrapper script tells offlineimap to stop.

This way I get frequent regular checks while I have mutt running, but
I don't
waste my battery/cpu checking frequently for mail when I'm not
interested in
it.

To make this work, though, it'd be nicer if it were easier to tell
offlineimap,
from the outside, things like "terminate cleanly now" and "when you've
finished
synching, then terminate instead of sleeping and synching again."

OK, to put my money where my mouth is, I attach two patches against
offlineimap
6.0.3.

The first, "cleanup.patch", cleans up a few spots that tend to throw
exceptions
for me as offlineimap is exiting from a KeyboardInterrupt.

The second adds signaling capabilities to offlineimap.

* sending a SIGTERM tells offlineimap to terminate immediately but
  cleanly,
  just as if "q" had been pressed in the GUI interface

* sending a SIGUSR1 tells every account to do a full sync asap: if
  it's
  sleeping, then wake up and do the sync now. If it's mid-sync, then
  re-synch
  any folders whose syncing has already been started or completed, and
  continue
  to synch the other, queued but not-yet-synched folders.

* sending a SIGHUP tells every account to die as soon as it can (but
  not
  immediately: only after finishing any synch it's now engaged in)

* sending a SIGUSR2 tells every account to do a full sync asap (as
  with
  SIGUSR1), then die

It's tricky to mix signals with threads, but I think I've done this
correctly.
I've been using it now for a few weeks without any obvious
problems. But I'm passing it
on so that others can review the code and test it out on their
systems. I developed the
patch when I was running Python 2.5.2, but to my knowledge I don't use
any Python 2.5-specific
code. Now I'm using the patch with Python 2.6.

Although I said "without any obvious problems," let me confess that
I'm
seeing offlineimap regularly choke when I do things like this: start
up
my offlineimap-wrapped copy of mutt, wait a while, put the machine to
sleep (not sure if offlineimap is active in the background or idling),
move to a different spot, wake the machine up again and it acquires a
new network, sometimes a wired network instead of wifi. Offlineimap
doesn't like that so much. I don't yet have any reason to think the
problems here come from my patches. But I'm just acknowledging them,
so
that if others are able to use offlineimap without any difficulty in
situations like I described, then maybe the fault is with my patches.
This commit is contained in:
John Goerzen 2008-12-01 16:13:16 -06:00
parent 5db24303b7
commit e1fb9492f8
7 changed files with 85 additions and 34 deletions

View File

@ -62,7 +62,7 @@ class Account(CustomConfig.ConfigHelperMixin):
def getsection(self):
return 'Account ' + self.getname()
def sleeper(self):
def sleeper(self, siglistener):
"""Sleep handler. Returns same value as UIBase.sleep:
0 if timeout expired, 1 if there was a request to cancel the timer,
and 2 if there is a request to abort the program.
@ -83,14 +83,25 @@ class Account(CustomConfig.ConfigHelperMixin):
item.startkeepalive()
refreshperiod = int(self.refreshperiod * 60)
sleepresult = self.ui.sleep(refreshperiod)
# try:
# sleepresult = siglistener.get_nowait()
# # retrieved signal before sleep started
# if sleepresult == 1:
# # catching signal 1 here means folders were cleared before signal was posted
# pass
# except Empty:
# sleepresult = self.ui.sleep(refreshperiod, siglistener)
sleepresult = self.ui.sleep(refreshperiod, siglistener)
if sleepresult == 1:
self.quicknum = 0
# Cancel keepalive
for item in kaobjs:
item.stopkeepalive()
return sleepresult
class AccountSynchronizationMixin:
def syncrunner(self):
def syncrunner(self, siglistener):
self.ui.registerthread(self.name)
self.ui.acct(self.name)
accountmetadata = self.getaccountmeta()
@ -106,19 +117,19 @@ class AccountSynchronizationMixin:
self.statusrepos = offlineimap.repository.LocalStatus.LocalStatusRepository(self.getconf('localrepository'), self)
if not self.refreshperiod:
self.sync()
self.sync(siglistener)
self.ui.acctdone(self.name)
return
looping = 1
while looping:
self.sync()
looping = self.sleeper() != 2
self.sync(siglistener)
looping = self.sleeper(siglistener) != 2
self.ui.acctdone(self.name)
def getaccountmeta(self):
return os.path.join(self.metadatadir, 'Account-' + self.name)
def sync(self):
def sync(self, siglistener):
# We don't need an account lock because syncitall() goes through
# each account once, then waits for all to finish.
@ -145,19 +156,24 @@ class AccountSynchronizationMixin:
self.ui.syncfolders(remoterepos, localrepos)
remoterepos.syncfoldersto(localrepos, [statusrepos])
folderthreads = []
for remotefolder in remoterepos.getfolders():
thread = InstanceLimitedThread(\
instancename = 'FOLDER_' + self.remoterepos.getname(),
target = syncfolder,
name = "Folder sync %s[%s]" % \
(self.name, remotefolder.getvisiblename()),
args = (self.name, remoterepos, remotefolder, localrepos,
statusrepos, quick))
thread.setDaemon(1)
thread.start()
folderthreads.append(thread)
threadutil.threadsreset(folderthreads)
siglistener.addfolders(remoterepos.getfolders(), bool(self.refreshperiod), quick)
while True:
folderthreads = []
for remotefolder, quick in siglistener.queuedfolders():
thread = InstanceLimitedThread(\
instancename = 'FOLDER_' + self.remoterepos.getname(),
target = syncfolder,
name = "Folder sync %s[%s]" % \
(self.name, remotefolder.getvisiblename()),
args = (self.name, remoterepos, remotefolder, localrepos,
statusrepos, quick))
thread.setDaemon(1)
thread.start()
folderthreads.append(thread)
threadutil.threadsreset(folderthreads)
if siglistener.clearfolders():
break
mbnames.write()
localrepos.forgetfolders()
remoterepos.forgetfolders()

View File

@ -26,6 +26,7 @@ from offlineimap.CustomConfig import CustomConfigParser
from threading import *
import threading, socket
from getopt import getopt
import signal
try:
import fcntl
@ -131,6 +132,11 @@ def startup(versionno):
lock(config, ui)
def sigterm_handler(signum, frame):
# die immediately
ui.terminate(errormsg="terminating...")
signal.signal(signal.SIGTERM,sigterm_handler)
try:
pidfd = open(config.getmetadatadir() + "/pid", "w")
pidfd.write(str(os.getpid()) + "\n")
@ -183,12 +189,31 @@ def startup(versionno):
else:
threadutil.initInstanceLimit(instancename,
config.getdefaultint('Repository ' + reposname, "maxconnections", 1))
siglisteners = []
def sig_handler(signum, frame):
if signum == signal.SIGUSR1:
# tell each account to do a full sync asap
signum = (1,)
elif signum == signal.SIGHUP:
# tell each account to die asap
signum = (2,)
elif signum == signal.SIGUSR2:
# tell each account to do a full sync asap, then die
signum = (1, 2)
# one listener per account thread (up to maxsyncaccounts)
for listener in siglisteners:
for sig in signum:
listener.put_nowait(sig)
signal.signal(signal.SIGHUP,sig_handler)
signal.signal(signal.SIGUSR1,sig_handler)
signal.signal(signal.SIGUSR2,sig_handler)
threadutil.initexitnotify()
t = ExitNotifyThread(target=syncmaster.syncitall,
name='Sync Runner',
kwargs = {'accounts': syncaccounts,
'config': config})
'config': config,
'siglisteners': siglisteners})
t.setDaemon(1)
t.start()
except:

View File

@ -20,27 +20,31 @@ import imaplib
from offlineimap import imapserver, repository, folder, mbnames, threadutil, version
from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
import offlineimap.accounts
from offlineimap.accounts import SyncableAccount
from offlineimap.accounts import SyncableAccount, SigListener
from offlineimap.ui import UIBase
import re, os, os.path, offlineimap, sys
from ConfigParser import ConfigParser
from threading import *
def syncaccount(threads, config, accountname):
def syncaccount(threads, config, accountname, siglisteners):
account = SyncableAccount(config, accountname)
siglistener = SigListener()
thread = InstanceLimitedThread(instancename = 'ACCOUNTLIMIT',
target = account.syncrunner,
name = "Account sync %s" % accountname)
name = "Account sync %s" % accountname,
kwargs = {'siglistener': siglistener} )
# the Sync Runner thread is the only one that will mutate siglisteners
siglisteners.append(siglistener)
thread.setDaemon(1)
thread.start()
threads.add(thread)
def syncitall(accounts, config):
def syncitall(accounts, config, siglisteners):
currentThread().setExitMessage('SYNC_WITH_TIMER_TERMINATE')
ui = UIBase.getglobalui()
threads = threadutil.threadlist()
mbnames.init(config, accounts)
for accountname in accounts:
syncaccount(threads, config, accountname)
syncaccount(threads, config, accountname, siglisteners)
# Wait for the threads to finish.
threads.reset()

View File

@ -132,10 +132,10 @@ class BlinkenBase:
s.gettf().setcolor('white')
s.__class__.__bases__[-1].callhook(s, msg)
def sleep(s, sleepsecs):
def sleep(s, sleepsecs, siglistener):
s.gettf().setcolor('red')
s.getaccountframe().startsleep(sleepsecs)
UIBase.sleep(s, sleepsecs)
return UIBase.sleep(s, sleepsecs, siglistener)
def sleeping(s, sleepsecs, remainingsecs):
if remainingsecs and s.gettf().getcolor() == 'black':

View File

@ -541,10 +541,10 @@ class Blinkenlights(BlinkenBase, UIBase):
s.c.stop()
UIBase.mainException(s)
def sleep(s, sleepsecs):
def sleep(s, sleepsecs, siglistener):
s.gettf().setcolor('red')
s._msg("Next sync in %d:%02d" % (sleepsecs / 60, sleepsecs % 60))
BlinkenBase.sleep(s, sleepsecs)
return BlinkenBase.sleep(s, sleepsecs, siglistener)
if __name__ == '__main__':
x = Blinkenlights(None)

View File

@ -33,10 +33,10 @@ class Basic(UIBase):
warntxt = 'warning'
sys.stderr.write(warntxt + ": " + str(msg) + "\n")
def sleep(s, sleepsecs):
def sleep(s, sleepsecs, siglistener):
if s.verbose >= 0:
s._msg("Sleeping for %d:%02d" % (sleepsecs / 60, sleepsecs % 60))
UIBase.sleep(s, sleepsecs)
return UIBase.sleep(s, sleepsecs, siglistener)
def sleeping(s, sleepsecs, remainingsecs):
if sleepsecs > 0:

View File

@ -19,6 +19,7 @@
import offlineimap.version
import re, time, sys, traceback, threading, thread
from StringIO import StringIO
from Queue import Empty
debugtypes = {'imap': 'IMAP protocol debugging',
'maildir': 'Maildir repository debugging',
@ -330,7 +331,7 @@ class UIBase:
################################################## Other
def sleep(s, sleepsecs):
def sleep(s, sleepsecs, siglistener):
"""This function does not actually output anything, but handles
the overall sleep, dealing with updates as necessary. It will,
however, call sleeping() which DOES output something.
@ -340,7 +341,12 @@ class UIBase:
abortsleep = 0
while sleepsecs > 0 and not abortsleep:
abortsleep = s.sleeping(1, sleepsecs)
try:
abortsleep = siglistener.get_nowait()
# retrieved signal while sleeping: 1 means immediately resynch, 2 means immediately die
except Empty:
# no signal
abortsleep = s.sleeping(1, sleepsecs)
sleepsecs -= 1
s.sleeping(0, 0) # Done sleeping.
return abortsleep