summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/FileMonitor/Fam.py
blob: 253bb2801964981ced4639708aa852aa1b92484e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
""" File monitor backend with support for the `File Alteration Monitor
<http://oss.sgi.com/projects/fam/>`_.  The FAM backend is deprecated. """

import os
import _fam
import stat
import logging
from time import time
from Bcfg2.Server.FileMonitor import FileMonitor

LOGGER = logging.getLogger(__name__)


class Fam(FileMonitor):
    """ **Deprecated** file monitor backend with support for the `File
    Alteration Monitor <http://oss.sgi.com/projects/fam/>`_ (also
    abbreviated "FAM")."""

    #: FAM is the worst actual monitor backend, so give it a low
    #: priority.
    __priority__ = 10

    def __init__(self, ignore=None, debug=False):
        FileMonitor.__init__(self, ignore=ignore, debug=debug)
        self.filemonitor = _fam.open()
        self.users = {}
        LOGGER.warning("The Fam file monitor backend is deprecated. Please "
                       "switch to a supported file monitor.")
    __init__.__doc__ = FileMonitor.__init__.__doc__

    def fileno(self):
        return self.filemonitor.fileno()
    fileno.__doc__ = FileMonitor.fileno.__doc__

    def handle_event_set(self, _=None):
        self.Service()
    handle_event_set.__doc__ = FileMonitor.handle_event_set.__doc__

    def handle_events_in_interval(self, interval):
        now = time()
        while (time() - now) < interval:
            if self.Service():
                now = time()
    handle_events_in_interval.__doc__ = \
        FileMonitor.handle_events_in_interval.__doc__

    def AddMonitor(self, path, obj, _=None):
        mode = os.stat(path)[stat.ST_MODE]
        if stat.S_ISDIR(mode):
            handle = self.filemonitor.monitorDirectory(path, None)
        else:
            handle = self.filemonitor.monitorFile(path, None)
        self.handles[handle.requestID()] = handle
        if obj != None:
            self.users[handle.requestID()] = obj
        return handle.requestID()
    AddMonitor.__doc__ = FileMonitor.AddMonitor.__doc__

    def Service(self, interval=0.50):
        """ Handle events for the specified period of time (in
        seconds).  This call will block for ``interval`` seconds.

        :param interval: The interval, in seconds, during which events
                         should be handled.  Any events that are
                         already pending when :func:`Service` is
                         called will also be handled.
        :type interval: int
        :returns: None
        """
        count = 0
        collapsed = 0
        rawevents = []
        start = time()
        now = time()
        while (time() - now) < interval:
            if self.filemonitor.pending():
                while self.filemonitor.pending():
                    count += 1
                    rawevents.append(self.filemonitor.nextEvent())
                now = time()
        unique = []
        bookkeeping = []
        for event in rawevents:
            if self.should_ignore(event):
                continue
            if event.code2str() != 'changed':
                # process all non-change events
                unique.append(event)
            else:
                if (event.filename, event.requestID) not in bookkeeping:
                    bookkeeping.append((event.filename, event.requestID))
                    unique.append(event)
                else:
                    collapsed += 1
        for event in unique:
            if event.requestID in self.users:
                try:
                    self.users[event.requestID].HandleEvent(event)
                except:  # pylint: disable=W0702
                    LOGGER.error("Handling event for file %s" % event.filename,
                                 exc_info=1)
        end = time()
        LOGGER.info("Processed %s fam events in %03.03f seconds. "
                    "%s coalesced" % (count, (end - start), collapsed))
        return count