docker-offlineimap/offlineimap/ui/Blinkenlights.py
John Goerzen e1fb9492f8 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.
2008-12-01 16:13:16 -06:00

148 lines
5.2 KiB
Python

# Blinkenlights base classes
# Copyright (C) 2003 John Goerzen
# <jgoerzen@complete.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from threading import *
from offlineimap.ui.UIBase import UIBase
import thread
from offlineimap.threadutil import MultiLock
class BlinkenBase:
"""This is a mix-in class that should be mixed in with either UIBase
or another appropriate base class. The Tk interface, for instance,
will probably mix it in with VerboseUI."""
def acct(s, accountname):
s.gettf().setcolor('purple')
s.__class__.__bases__[-1].acct(s, accountname)
def connecting(s, hostname, port):
s.gettf().setcolor('gray')
s.__class__.__bases__[-1].connecting(s, hostname, port)
def syncfolders(s, srcrepos, destrepos):
s.gettf().setcolor('blue')
s.__class__.__bases__[-1].syncfolders(s, srcrepos, destrepos)
def syncingfolder(s, srcrepos, srcfolder, destrepos, destfolder):
s.gettf().setcolor('cyan')
s.__class__.__bases__[-1].syncingfolder(s, srcrepos, srcfolder, destrepos, destfolder)
def skippingfolder(s, folder):
s.gettf().setcolor('cyan')
s.__class__.__bases__[-1].skippingfolder(s, folder)
def loadmessagelist(s, repos, folder):
s.gettf().setcolor('green')
s._msg("Scanning folder [%s/%s]" % (s.getnicename(repos),
folder.getvisiblename()))
def syncingmessages(s, sr, sf, dr, df):
s.gettf().setcolor('blue')
s.__class__.__bases__[-1].syncingmessages(s, sr, sf, dr, df)
def copyingmessage(s, uid, src, destlist):
s.gettf().setcolor('orange')
s.__class__.__bases__[-1].copyingmessage(s, uid, src, destlist)
def deletingmessages(s, uidlist, destlist):
s.gettf().setcolor('red')
s.__class__.__bases__[-1].deletingmessages(s, uidlist, destlist)
def deletingmessage(s, uid, destlist):
s.gettf().setcolor('red')
s.__class__.__bases__[-1].deletingmessage(s, uid, destlist)
def addingflags(s, uidlist, flags, destlist):
s.gettf().setcolor('yellow')
s.__class__.__bases__[-1].addingflags(s, uidlist, flags, destlist)
def deletingflags(s, uidlist, flags, destlist):
s.gettf().setcolor('pink')
s.__class__.__bases__[-1].deletingflags(s, uidlist, flags, destlist)
def warn(s, msg, minor = 0):
if minor:
s.gettf().setcolor('pink')
else:
s.gettf().setcolor('red')
s.__class__.__bases__[-1].warn(s, msg, minor)
def init_banner(s):
s.availablethreadframes = {}
s.threadframes = {}
s.tflock = MultiLock()
def threadExited(s, thread):
threadid = thread.threadid
accountname = s.getthreadaccount(thread)
s.tflock.acquire()
try:
if threadid in s.threadframes[accountname]:
tf = s.threadframes[accountname][threadid]
del s.threadframes[accountname][threadid]
s.availablethreadframes[accountname].append(tf)
tf.setthread(None)
finally:
s.tflock.release()
UIBase.threadExited(s, thread)
def gettf(s):
threadid = thread.get_ident()
accountname = s.getthreadaccount()
s.tflock.acquire()
try:
if not accountname in s.threadframes:
s.threadframes[accountname] = {}
if threadid in s.threadframes[accountname]:
return s.threadframes[accountname][threadid]
if not accountname in s.availablethreadframes:
s.availablethreadframes[accountname] = []
if len(s.availablethreadframes[accountname]):
tf = s.availablethreadframes[accountname].pop(0)
tf.setthread(currentThread())
else:
tf = s.getaccountframe().getnewthreadframe()
s.threadframes[accountname][threadid] = tf
return tf
finally:
s.tflock.release()
def callhook(s, msg):
s.gettf().setcolor('white')
s.__class__.__bases__[-1].callhook(s, msg)
def sleep(s, sleepsecs, siglistener):
s.gettf().setcolor('red')
s.getaccountframe().startsleep(sleepsecs)
return UIBase.sleep(s, sleepsecs, siglistener)
def sleeping(s, sleepsecs, remainingsecs):
if remainingsecs and s.gettf().getcolor() == 'black':
s.gettf().setcolor('red')
else:
s.gettf().setcolor('black')
return s.getaccountframe().sleeping(sleepsecs, remainingsecs)