10 from pyinotify import EventsCodes
12 from benderjab import rpc
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
20 # this should be per runfolder.
25 class Handler(pyinotify.ProcessEvent):
26 def __init__(self, watchmanager, bot):
27 self.last_event_time = None
28 self.watchmanager = watchmanager
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":
36 self.bot.sequencingFinished(event.path)
38 logging.error("Couldn't send sequencingFinished")
41 def process_IN_DELETE(self, event):
42 logging.debug("Remove: %s" % os.path.join(event.path, event.name))
44 def process_IN_UNMOUNT(self, event):
45 self.bot.unmount_watch()
47 class SpoolWatcher(rpc.XmlRpcBot):
49 Watch a directory and send a message when another process is done writing.
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.
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
59 # these params need to be in the config file
60 # I wonder where I should put the documentation
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
66 # `notify_timeout` - how often to timeout from notify
68 def __init__(self, section=None, configfile=None):
69 #if configfile is None:
70 # self.configfile = "~/.gaworkflow"
71 super(SpoolWatcher, self).__init__(section, configfile)
73 self.cfg['watchdir'] = None
74 self.cfg['write_timeout'] = 10
75 self.cfg['notify_users'] = None
76 self.cfg['notify_runner'] = None
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)
84 self.notify_users = None
85 self.notify_runner = None
87 self.eventTasks.append(self.process_notify)
89 def read_config(self, section=None, configfile=None):
90 super(SpoolWatcher, self).read_config(section, configfile)
92 self.watch_dir = self._check_required_option('watchdir')
93 self.write_timeout = int(self.cfg['write_timeout'])
95 self.notify_users = self._parse_user_list(self.cfg['notify_users'])
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'
103 raise bot.JIDMissingResource(msg)
105 def add_watch(self, watchdir=None):
107 start watching watchdir or self.watch_dir
108 we're currently limited to watching one directory tree.
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
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
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)
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())
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:
141 self.handler.last_event_time = None
143 def _parser(self, msg, who):
145 Parse xmpp chat messages
147 help = u"I can send [copy] message, or squencer [finished]"
148 if re.match(u"help", msg):
150 elif re.match("copy", msg):
152 reply = u"sent copy message"
153 elif re.match(u"finished", msg):
156 self.sequencingFinished(words[1])
157 reply = u"sending sequencing finished for %s" % (words[1])
159 reply = u"need runfolder name"
161 reply = u"I didn't understand '%s'" %(msg)
164 def start(self, daemonize):
169 super(SpoolWatcher, self).start(daemonize)
175 # destroy the inotify's instance on this interrupt (stop monitoring)
177 super(SpoolWatcher, self).stop()
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')
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')
202 return bot.main(args)
204 if __name__ == "__main__":
205 sys.exit(main(sys.argv[1:]))