pyinotify rm_watch takes a list, not a dictionary.
[htsworkflow.git] / gaworkflow / automation / spoolwatcher.py
1 #!/usr/bin/env python
2 import logging
3 import os
4 import re
5 import sys
6 import time
7
8 # this uses pyinotify
9 import pyinotify
10 from pyinotify import EventsCodes
11
12 from benderjab import rpc
13
14 class WatcherEvents(object):
15     # two events need to be tracked
16     # one to send startCopy
17     # one to send OMG its broken
18     # OMG its broken needs to stop when we've seen enough
19     #  cycles
20     # this should be per runfolder. 
21     # read the xml files 
22     def __init__(self):
23         
24
25 class Handler(pyinotify.ProcessEvent):
26     def __init__(self, watchmanager, bot):
27         self.last_event_time = None
28         self.watchmanager = watchmanager
29         self.bot = bot
30
31     def process_IN_CREATE(self, event):
32         self.last_event_time = time.time()
33         msg = "Create: %s" %  os.path.join(event.path, event.name)
34         if event.name.lower() == "run.completed":
35             try:
36                 self.bot.sequencingFinished(event.path)
37             except IOError, e:
38                 logging.error("Couldn't send sequencingFinished")
39         logging.debug(msg)
40
41     def process_IN_DELETE(self, event):
42         logging.debug("Remove: %s" %  os.path.join(event.path, event.name))
43
44     def process_IN_UNMOUNT(self, event):
45         self.bot.unmount_watch()
46
47 class SpoolWatcher(rpc.XmlRpcBot):
48     """
49     Watch a directory and send a message when another process is done writing.
50     
51     This monitors a directory tree using inotify (linux specific) and
52     after some files having been written will send a message after <timeout>
53     seconds of no file writing.
54     
55     (Basically when the solexa machine finishes dumping a round of data
56     this'll hopefully send out a message saying hey look theres data available
57     
58     """
59     # these params need to be in the config file
60     # I wonder where I should put the documentation
61     #:Parameters:
62     #    `watchdir` - which directory tree to monitor for modifications
63     #    `profile` - specify which .gaworkflow profile to use
64     #    `write_timeout` - how many seconds to wait for writes to finish to
65     #                      the spool
66     #    `notify_timeout` - how often to timeout from notify
67     
68     def __init__(self, section=None, configfile=None):
69         #if configfile is None:
70         #    self.configfile = "~/.gaworkflow"
71         super(SpoolWatcher, self).__init__(section, configfile)
72         
73         self.cfg['watchdir'] = None
74         self.cfg['write_timeout'] = 10
75         self.cfg['notify_users'] = None
76         self.cfg['notify_runner'] = None
77         
78         self.notify_timeout = 0.001
79         self.wm = pyinotify.WatchManager()
80         self.handler = Handler(self.wm, self)
81         self.notifier = pyinotify.Notifier(self.wm, self.handler)
82         self.wdd = None
83         
84         self.notify_users = None
85         self.notify_runner = None
86         
87         self.eventTasks.append(self.process_notify)
88
89     def read_config(self, section=None, configfile=None):
90         super(SpoolWatcher, self).read_config(section, configfile)
91         
92         self.watch_dir = self._check_required_option('watchdir')
93         self.write_timeout = int(self.cfg['write_timeout'])
94         
95         self.notify_users = self._parse_user_list(self.cfg['notify_users'])
96         try:
97           self.notify_runner = \
98              self._parse_user_list(self.cfg['notify_runner'],
99                                    require_resource=True)
100         except bot.JIDMissingResource:
101             msg = 'need a full jabber ID + resource for xml-rpc destinations'
102             logging.FATAL(msg)
103             raise bot.JIDMissingResource(msg)
104             
105     def add_watch(self, watchdir=None):
106         """
107         start watching watchdir or self.watch_dir
108         we're currently limited to watching one directory tree.
109         """
110         # the one tree limit is mostly because self.wdd is a single item
111         # but managing it as a list might be a bit more annoying
112         if watchdir is None:
113             watchdir = self.watch_dir
114         logging.info("Watching:"+str(watchdir))
115         mask = EventsCodes.IN_CREATE | EventsCodes.IN_UNMOUNT
116         # rec traverses the tree and adds all the directories that are there
117         # at the start.
118         # auto_add will add in new directories as they are created
119         self.wdd = self.wm.add_watch(watchdir, mask, rec=True, auto_add=True)
120
121     def unmount_watch(self):
122         if self.wdd is not None:
123             logging.debug("disabling watch")
124             logging.debug(str(self.wdd))
125             self.wm.rm_watch(self.wdd.values())
126             self.wdd = None
127             
128     def process_notify(self, *args):
129         # process the queue of events as explained above
130         self.notifier.process_events()
131         #check events waits timeout
132         if self.notifier.check_events(self.notify_timeout):
133             # read notified events and enqeue them
134             self.notifier.read_events()
135             # should we do something?
136         last_event_time = self.handler.last_event_time
137         if last_event_time is not None:
138             time_delta = time.time() - last_event_time
139             if time_delta > self.write_timeout:
140                 self.startCopy()
141                 self.handler.last_event_time = None
142     
143     def _parser(self, msg, who):
144         """
145         Parse xmpp chat messages
146         """
147         help = u"I can send [copy] message, or squencer [finished]"
148         if re.match(u"help", msg):
149             reply = help
150         elif re.match("copy", msg):            
151             self.startCopy()
152             reply = u"sent copy message"
153         elif re.match(u"finished", msg):
154             words = msg.split()
155             if len(words) == 2:
156                 self.sequencingFinished(words[1])
157                 reply = u"sending sequencing finished for %s" % (words[1])
158             else:
159                 reply = u"need runfolder name"
160         else:
161             reply = u"I didn't understand '%s'" %(msg)            
162         return reply
163         
164     def start(self, daemonize):
165         """
166         Start application
167         """
168         self.add_watch()
169         super(SpoolWatcher, self).start(daemonize)
170         
171     def stop(self):
172         """
173         shutdown application
174         """
175         # destroy the inotify's instance on this interrupt (stop monitoring)
176         self.notifier.stop()
177         super(SpoolWatcher, self).stop()
178     
179     def startCopy(self):
180         logging.debug("writes seem to have stopped")
181         if self.notify_runner is not None:
182             for r in self.notify_runner:
183                 self.rpc_send(r, tuple(), 'startCopy')
184         
185     def sequencingFinished(self, run_dir):
186         # need to strip off self.watch_dir from rundir I suspect.
187         logging.info("run.completed in " + str(run_dir))
188         pattern = self.watch_dir
189         if pattern[-1] != os.path.sep:
190             pattern += os.path.sep
191         stripped_run_dir = re.sub(pattern, "", run_dir)
192         logging.debug("stripped to " + stripped_run_dir)
193         if self.notify_users is not None:
194             for u in self.notify_users:
195                 self.send(u, 'Sequencing run %s finished' % (stripped_run_dir))
196         if self.notify_runner is not None:
197             for r in self.notify_runner:
198                 self.rpc_send(r, (stripped_run_dir,), 'sequencingFinished')
199         
200 def main(args=None):
201     bot = SpoolWatcher()
202     return bot.main(args)
203     
204 if __name__ == "__main__":
205     sys.exit(main(sys.argv[1:]))
206