ReadPreps can also use the netcopy complete flags which causes problems
[htsworkflow.git] / htsworkflow / automation / spoolwatcher.py
1 #!/usr/bin/env python
2 import logging
3 import os
4 import re
5 import shlex
6 import sys
7 import time
8
9 from htsworkflow.util import mount
10
11 # this uses pyinotify
12 import pyinotify
13 from pyinotify import EventsCodes
14
15 from benderjab import rpc
16
17 def is_runfolder(name):
18     """
19     Is it a runfolder?
20
21     >>> print is_runfolder('090630_HWUSI-EAS999_0006_30LNFAAXX')
22     True
23     >>> print is_runfolder('hello')
24     False
25     """
26     if re.match("[0-9]{6}_.*", name):
27         return True
28     else:
29         return False
30
31 def get_top_dir(root, path):
32     """
33     Return the directory in path that is a subdirectory of root.
34     e.g.
35
36     >>> print get_top_dir('/a/b/c', '/a/b/c/d/e/f')
37     d
38     >>> print get_top_dir('/a/b/c/', '/a/b/c/d/e/f')
39     d
40     >>> print get_top_dir('/a/b/c', '/g/e/f')
41     None
42     >>> print get_top_dir('/a/b/c', '/a/b/c')
43     <BLANKLINE>
44     """
45     if path.startswith(root):
46         subpath = path[len(root):]
47         if subpath.startswith('/'):
48             subpath = subpath[1:]
49         return subpath.split(os.path.sep)[0]
50     else:
51         return None
52
53 class WatcherEvent(object):
54     """
55     Track information about a file event
56
57     Currently its time, and if it was an indication we've completed the run.
58     """
59     def __init__(self, event_root=None):
60         self.time = time.time()
61         self.event_root = event_root
62         self.complete = False
63         
64     def __unicode__(self):
65         if self.complete:
66            complete = "(completed)"
67         else:
68            complete = ""
69         return u"<WatchEvent: %s %s %s>" % (time.ctime(self.time), self.event_root, complete)
70
71 class Handler(pyinotify.ProcessEvent):
72     def __init__(self, watchmanager, bot, completion_file=None):
73         """
74         Completion file contains current "completion" filename
75         """
76         self.last_event = {}
77         self.watchmanager = watchmanager
78         self.bot = bot
79         if completion_file is not None:
80             completion_file = completion_file.lower()
81         self.completion_file = completion_file
82
83     def process_IN_CREATE(self, event):
84         for wdd in self.bot.wdds:
85             for watch_path in self.bot.watchdirs:
86                 run_already_complete = False
87                 # I only care about things created inside the watch directory, so
88                 # the event path needs to be longer than the watch path in addition to
89                 # starting with the watch_path
90                 if len(event.path) > len(watch_path) and event.path.startswith(watch_path):
91                     # compute name of the top level directory that had an event
92                     # in the current watch path
93                     target = get_top_dir(watch_path, event.path)
94                     runfolder = os.path.join(watch_path, target)
95
96                     if not is_runfolder(target):
97                         logging.debug("Skipping %s, not a runfolder" % (target,))
98                         continue
99                     
100                     # grab the previous events for this watch path
101                     watch_path_events = self.last_event.setdefault(watch_path, {})
102
103                     # if we've already seen an event in this directory (AKA runfolder)
104                     # keep track if its already hit the "completed" flag
105                     if watch_path_events.has_key(target):
106                        run_already_complete = watch_path_events[target].complete
107
108                     watch_path_events[target] = WatcherEvent(target)
109                     #self.last_event.setdefault(watch_path, {})[target] = WatcherEvent(target)
110
111                     msg = "Create: %s %s %s %s" % (watch_path, target, event.path, event.name)
112
113                     # the ReadPrep step uses some of the same file completion flags as the
114                     # main analysis, which means this completion code might get tripped because of it
115                     # so we need to make sure we're getting the completion file in the root of the
116                     # runfolder
117                     if (self.completion_file == event.name.lower() and event.path == runfolder) \
118                       or run_already_complete:
119                         self.last_event[watch_path][target].complete = True
120                         msg += "(completed)"
121
122                     logging.debug(msg)
123
124     def process_IN_DELETE(self, event):
125         logging.debug("Remove: %s" %  os.path.join(event.path, event.name))
126         pass
127
128     def process_IN_UNMOUNT(self, event):
129         pathname = os.path.join(event.path, event.name)
130         logging.debug("IN_UNMOUNT: %s" % (pathname,))
131         self.bot.unmount_watch(event.path)
132
133 class SpoolWatcher(rpc.XmlRpcBot):
134     """
135     Watch a directory and send a message when another process is done writing.
136     
137     This monitors a directory tree using inotify (linux specific) and
138     after some files having been written will send a message after <timeout>
139     seconds of no file writing.
140     
141     (Basically when the solexa machine finishes dumping a round of data
142     this'll hopefully send out a message saying hey look theres data available
143     
144     """
145     # these params need to be in the config file
146     # I wonder where I should put the documentation
147     #:Parameters:
148     #    `watchdirs` - list of directories to monitor for modifications
149     #    `profile` - specify which .htsworkflow profile to use
150     #    `write_timeout` - how many seconds to wait for writes to finish to
151     #                      the spool
152     #    `notify_timeout` - how often to timeout from notify
153     #    `completion_file` - what file indicates we've finished sequencing
154     #                        defaults to: netcopy_complete.txt
155     
156     def __init__(self, section=None, configfile=None):
157         #if configfile is None:
158         #    self.configfile = "~/.htsworkflow"
159         super(SpoolWatcher, self).__init__(section, configfile)
160         
161         self.cfg['watchdirs'] = None
162         self.cfg['write_timeout'] = 10
163         self.cfg['notify_users'] = None
164         self.cfg['notify_runner'] = None
165         self.cfg['completion_file'] = 'netcopy_complete.txt'
166        
167         self.watchdirs = []
168         self.watchdir_url_map = {}
169         self.notify_timeout = 0.001
170
171         self.wm = None 
172         self.notify_users = None
173         self.notify_runner = None
174         self.wdds = []
175
176         # keep track if the specified mount point is currently mounted
177         self.mounted_points = {}
178         # keep track of which mount points tie to which watch directories
179         # so maybe we can remount them.
180         self.mounts_to_watches = {}
181         
182         self.eventTasks.append(self.process_notify)
183
184     def read_config(self, section=None, configfile=None):
185         # Don't give in to the temptation to use logging functions here, 
186         # need to wait until after we detach in start
187         super(SpoolWatcher, self).read_config(section, configfile)
188         
189         self.watchdirs = shlex.split(self._check_required_option('watchdirs'))
190         # see if there's an alternate url that should be used for the watchdir
191         for watchdir in self.watchdirs:
192             self.watchdir_url_map[watchdir] = self.cfg.get(watchdir, watchdir)
193
194         self.write_timeout = int(self.cfg['write_timeout'])
195         self.completion_file = self.cfg['completion_file']
196         
197         self.notify_users = self._parse_user_list(self.cfg['notify_users'])
198         try:
199           self.notify_runner = \
200              self._parse_user_list(self.cfg['notify_runner'],
201                                    require_resource=True)
202         except bot.JIDMissingResource:
203             msg = 'need a full jabber ID + resource for xml-rpc destinations'
204             raise bot.JIDMissingResource(msg)
205
206         self.handler = None
207         self.notifier = None
208
209     def add_watch(self, watchdirs=None):
210         """
211         start watching watchdir or self.watchdir
212         we're currently limited to watching one directory tree.
213         """
214         # create the watch managers if we need them
215         if self.wm is None:
216             self.wm = pyinotify.WatchManager()
217             self.handler = Handler(self.wm, self, self.completion_file)
218             self.notifier = pyinotify.Notifier(self.wm, self.handler)
219
220         # the one tree limit is mostly because self.wdd is a single item
221         # but managing it as a list might be a bit more annoying
222         if watchdirs is None:
223             watchdirs = self.watchdirs
224
225         mask = EventsCodes.IN_CREATE | EventsCodes.IN_UNMOUNT
226         # rec traverses the tree and adds all the directories that are there
227         # at the start.
228         # auto_add will add in new directories as they are created
229         for w in watchdirs:
230             mount_location = mount.find_mount_point_for(w)
231             self.mounted_points[mount_location] = True
232             mounts = self.mounts_to_watches.get(mount_location, [])
233             if w not in mounts:
234                 mounts.append(w)
235                 self.mounts_to_watches[mount_location] = mounts
236
237             logging.info(u"Watching:"+unicode(w))
238             self.wdds.append(self.wm.add_watch(w, mask, rec=True, auto_add=True))
239
240     def unmount_watch(self, event_path):
241         # remove backwards so we don't get weirdness from 
242         # the list getting shorter
243         for i in range(len(self.wdds),0, -1):
244             wdd = self.wdds[i]
245             logging.info(u'unmounting: '+unicode(wdd.items()))
246             self.wm.rm_watch(wdd.values())
247             del self.wdds[i]
248         self.mounted = False
249
250     def make_copy_url(self, watchdir, list_event_dir):
251         root_copy_url = self.watchdir_url_map[watchdir]
252         if root_copy_url[-1] != '/':
253             root_copy_url += '/'
254         copy_url = root_copy_url + list_event_dir
255         logging.debug('Copy url: %s' % (copy_url,))
256         return copy_url
257                   
258     def process_notify(self, *args):
259         if self.notifier is None:
260             # nothing to do yet
261             return
262         # process the queue of events as explained above
263         self.notifier.process_events()
264         #check events waits timeout
265         if self.notifier.check_events(self.notify_timeout):
266             # read notified events and enqeue them
267             self.notifier.read_events()
268             # should we do something?
269         # has something happened?
270         for watchdir, last_events in self.handler.last_event.items():
271             for last_event_dir, last_event_detail in last_events.items():
272                 time_delta = time.time() - last_event_detail.time
273                 if time_delta > self.write_timeout:
274                     print "timeout", unicode(last_event_detail)
275                     copy_url = self.make_copy_url(watchdir, last_event_dir)
276                     self.startCopy(copy_url)
277                     if last_event_detail.complete:
278                         self.sequencingFinished(last_event_detail.event_root)
279
280                     self.handler.last_event[watchdir] = {}
281         # handle unmounted filesystems
282         for mount_point, was_mounted in self.mounted_points.items():
283             if not was_mounted and mount.is_mounted(mount_point):
284                 # we've been remounted. Huzzah!
285                 # restart the watch
286                 for watch in self.mounts_to_watches[mount_point]:
287                     self.add_watch(watch)
288                     logging.info(
289                         "%s was remounted, restarting watch" % \
290                             (mount_point)
291                     )
292                 self.mounted_points[mount_point] = True
293
294     def _parser(self, msg, who):
295         """
296         Parse xmpp chat messages
297         """
298         help = u"I can send [copy] message, or squencer [finished]"
299         if re.match(u"help", msg):
300             reply = help
301         elif re.match("copy", msg):            
302             self.startCopy(msg)
303             reply = u"sent copy message"
304         elif re.match(u"finished", msg):
305             words = msg.split()
306             if len(words) == 2:
307                 self.sequencingFinished(words[1])
308                 reply = u"sending sequencing finished for %s" % (words[1])
309             else:
310                 reply = u"need runfolder name"
311         else:
312             reply = u"I didn't understand '%s'" %(msg)            
313         return reply
314         
315     def run(self):
316         """
317         Start application
318         """
319         # we have to configure pyinotify after BenderJab.start is called
320         # as weird things happen to pyinotify if the stdio is closed
321         # after it's initialized.
322         self.add_watch()
323         super(SpoolWatcher, self).run()
324         
325     def stop(self):
326         """
327         shutdown application
328         """
329         # destroy the inotify's instance on this interrupt (stop monitoring)
330         if self.notifier is not None:
331             self.notifier.stop()
332         super(SpoolWatcher, self).stop()
333     
334     def startCopy(self, copy_url=None):
335         logging.debug("writes seem to have stopped")
336         if self.notify_runner is not None:
337             for r in self.notify_runner:
338                 self.rpc_send(r, tuple([copy_url]), 'startCopy')
339         if self.notify_users is not None:
340             for u in self.notify_users:
341                 self.send(u, 'startCopy %s.' % (copy_url,))
342         
343     def sequencingFinished(self, run_dir):
344         # need to strip off self.watchdirs from rundir I suspect.
345         logging.info("run.completed in " + str(run_dir))
346         for watch in self.watchdirs:
347             if not run_dir.startswith(watch):
348                 print "%s didn't start with %s" % (run_dir, watch)
349                 continue
350             if watch[-1] != os.path.sep:
351                 watch += os.path.sep
352             stripped_run_dir = re.sub(watch, "", run_dir)
353         else:
354             stripped_run_dir = run_dir
355
356         logging.debug("stripped to " + stripped_run_dir)
357         if self.notify_users is not None:
358             for u in self.notify_users:
359                 self.send(u, 'Sequencing run %s finished' % \
360                           (stripped_run_dir))
361         if self.notify_runner is not None:
362             for r in self.notify_runner:
363                 self.rpc_send(r, (stripped_run_dir,), 'sequencingFinished')
364
365 def main(args=None):
366     bot = SpoolWatcher()
367     return bot.main(args)
368     
369 if __name__ == "__main__":
370     ret = main(sys.argv[1:])
371     #sys.exit(ret)
372
373 # TODO:
374 # send messages to copier specifying which mount to copy