~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/poppy/twistedconfigreset.py

Merge db-devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""A Twisted job to touch the GPGHandler config files."""
 
5
 
 
6
__metaclass__ = type
 
7
__all__ = [
 
8
    'GPGHandlerConfigResetJob',
 
9
    ]
 
10
 
 
11
from twisted.application.service import Service
 
12
from twisted.internet import task
 
13
from twisted.internet.error import AlreadyCancelled
 
14
 
 
15
from zope.component import getUtility
 
16
from zope.component.interfaces import ComponentLookupError
 
17
 
 
18
from canonical.launchpad.interfaces.gpghandler import IGPGHandler
 
19
 
 
20
 
 
21
class GPGHandlerConfigResetJob(Service):
 
22
    """Manages twisted job to touch the files in the gpgconfig directory."""
 
23
 
 
24
    def startService(self):
 
25
        self._gpghandler_job = None
 
26
        # start the GPGHandler job
 
27
        self._scheduleGPGHandlerJob()
 
28
        Service.startService(self)
 
29
 
 
30
    def stopService(self):
 
31
        self._stopGPGHandlerJob()
 
32
        Service.stopService(self)
 
33
 
 
34
    def _scheduleGPGHandlerJob(self, touch_interval=12 * 3600):
 
35
        # Create a job to touch the GPGHandler home directory every so often
 
36
        # so that it does not get cleaned up by any reaper scripts which look
 
37
        # at time last modified.
 
38
 
 
39
        self._stopGPGHandlerJob()
 
40
        self._gpghandler_job = task.LoopingCall(
 
41
            getUtility(IGPGHandler).touchConfigurationDirectory)
 
42
        return self._gpghandler_job.start(touch_interval)
 
43
 
 
44
    def _stopGPGHandlerJob(self):
 
45
        try:
 
46
            if self._gpghandler_job and self._gpghandler_job.running:
 
47
                self._gpghandler_job.stop()
 
48
        except AlreadyCancelled:
 
49
            # So we're already cancelled, meh.
 
50
            pass