~launchpad-pqm/launchpad/devel

12919.6.13 by Ian Booth
Move job instantiation to separate class
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
12919.6.14 by Ian Booth
Set up service properly using twisted
11
from twisted.application.service import Service
12919.6.13 by Ian Booth
Move job instantiation to separate class
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
12919.6.14 by Ian Booth
Set up service properly using twisted
21
class GPGHandlerConfigResetJob(Service):
12919.6.13 by Ian Booth
Move job instantiation to separate class
22
    """Manages twisted job to touch the files in the gpgconfig directory."""
12919.6.14 by Ian Booth
Set up service properly using twisted
23
24
    def startService(self):
12919.6.13 by Ian Booth
Move job instantiation to separate class
25
        self._gpghandler_job = None
26
        # start the GPGHandler job
27
        self._scheduleGPGHandlerJob()
12919.6.14 by Ian Booth
Set up service properly using twisted
28
        Service.startService(self)
29
30
    def stopService(self):
31
        self._stopGPGHandlerJob()
32
        Service.stopService(self)
12919.6.13 by Ian Booth
Move job instantiation to separate class
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()
12919.6.15 by Ian Booth
Code review fixes
40
        self._gpghandler_job = task.LoopingCall(
41
            getUtility(IGPGHandler).touchConfigurationDirectory)
42
        return self._gpghandler_job.start(touch_interval)
12919.6.13 by Ian Booth
Move job instantiation to separate class
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