9 #from gaworkflow.pipeline.recipe_parser import get_cycles
13 from pyinotify import EventsCodes
15 from benderjab import rpc
18 class WatcherEvents(object):
19 # two events need to be tracked
20 # one to send startCopy
21 # one to send OMG its broken
22 # OMG its broken needs to stop when we've seen enough
24 # this should be per runfolder.
30 class Handler(pyinotify.ProcessEvent):
31 def __init__(self, watchmanager, bot):
32 self.last_event_time = None
33 self.watchmanager = watchmanager
36 def process_IN_CREATE(self, event):
37 self.last_event_time = time.time()
38 msg = "Create: %s" % os.path.join(event.path, event.name)
39 if event.name.lower() == "run.completed":
41 self.bot.sequencingFinished(event.path)
43 logging.error("Couldn't send sequencingFinished")
46 def process_IN_DELETE(self, event):
47 logging.debug("Remove: %s" % os.path.join(event.path, event.name))
49 def process_IN_UNMOUNT(self, event):
50 self.bot.unmount_watch()
52 class SpoolWatcher(rpc.XmlRpcBot):
54 Watch a directory and send a message when another process is done writing.
56 This monitors a directory tree using inotify (linux specific) and
57 after some files having been written will send a message after <timeout>
58 seconds of no file writing.
60 (Basically when the solexa machine finishes dumping a round of data
61 this'll hopefully send out a message saying hey look theres data available
64 # these params need to be in the config file
65 # I wonder where I should put the documentation
67 # `watchdir` - which directory tree to monitor for modifications
68 # `profile` - specify which .gaworkflow profile to use
69 # `write_timeout` - how many seconds to wait for writes to finish to
71 # `notify_timeout` - how often to timeout from notify
73 def __init__(self, section=None, configfile=None):
74 #if configfile is None:
75 # self.configfile = "~/.gaworkflow"
76 super(SpoolWatcher, self).__init__(section, configfile)
78 self.cfg['watchdir'] = None
79 self.cfg['write_timeout'] = 10
80 self.cfg['notify_users'] = None
81 self.cfg['notify_runner'] = None
83 self.notify_timeout = 0.001
84 self.wm = pyinotify.WatchManager()
85 self.handler = Handler(self.wm, self)
86 self.notifier = pyinotify.Notifier(self.wm, self.handler)
89 self.notify_users = None
90 self.notify_runner = None
92 self.eventTasks.append(self.process_notify)
94 def read_config(self, section=None, configfile=None):
95 super(SpoolWatcher, self).read_config(section, configfile)
97 self.watch_dir = self._check_required_option('watchdir')
98 self.write_timeout = int(self.cfg['write_timeout'])
100 self.notify_users = self._parse_user_list(self.cfg['notify_users'])
102 self.notify_runner = \
103 self._parse_user_list(self.cfg['notify_runner'],
104 require_resource=True)
105 except bot.JIDMissingResource:
106 msg = 'need a full jabber ID + resource for xml-rpc destinations'
108 raise bot.JIDMissingResource(msg)
110 def add_watch(self, watchdir=None):
112 start watching watchdir or self.watch_dir
113 we're currently limited to watching one directory tree.
115 # the one tree limit is mostly because self.wdd is a single item
116 # but managing it as a list might be a bit more annoying
118 watchdir = self.watch_dir
119 logging.info("Watching:"+str(watchdir))
120 mask = EventsCodes.IN_CREATE | EventsCodes.IN_UNMOUNT
121 # rec traverses the tree and adds all the directories that are there
123 # auto_add will add in new directories as they are created
124 self.wdd = self.wm.add_watch(watchdir, mask, rec=True, auto_add=True)
126 def unmount_watch(self):
127 if self.wdd is not None:
128 logging.debug("disabling watch")
129 logging.debug(str(self.wdd))
130 self.wm.rm_watch(self.wdd.values())
133 def process_notify(self, *args):
134 # process the queue of events as explained above
135 self.notifier.process_events()
136 #check events waits timeout
137 if self.notifier.check_events(self.notify_timeout):
138 # read notified events and enqeue them
139 self.notifier.read_events()
140 # should we do something?
141 last_event_time = self.handler.last_event_time
142 if last_event_time is not None:
143 time_delta = time.time() - last_event_time
144 if time_delta > self.write_timeout:
146 self.handler.last_event_time = None
148 def _parser(self, msg, who):
150 Parse xmpp chat messages
152 help = u"I can send [copy] message, or squencer [finished]"
153 if re.match(u"help", msg):
155 elif re.match("copy", msg):
157 reply = u"sent copy message"
158 elif re.match(u"finished", msg):
161 self.sequencingFinished(words[1])
162 reply = u"sending sequencing finished for %s" % (words[1])
164 reply = u"need runfolder name"
166 reply = u"I didn't understand '%s'" %(msg)
169 def start(self, daemonize):
174 super(SpoolWatcher, self).start(daemonize)
180 # destroy the inotify's instance on this interrupt (stop monitoring)
182 super(SpoolWatcher, self).stop()
185 logging.debug("writes seem to have stopped")
186 if self.notify_runner is not None:
187 for r in self.notify_runner:
188 self.rpc_send(r, tuple(), 'startCopy')
190 def sequencingFinished(self, run_dir):
191 # need to strip off self.watch_dir from rundir I suspect.
192 logging.info("run.completed in " + str(run_dir))
193 pattern = self.watch_dir
194 if pattern[-1] != os.path.sep:
195 pattern += os.path.sep
196 stripped_run_dir = re.sub(pattern, "", run_dir)
197 logging.debug("stripped to " + stripped_run_dir)
198 if self.notify_users is not None:
199 for u in self.notify_users:
200 self.send(u, 'Sequencing run %s finished' % (stripped_run_dir))
201 if self.notify_runner is not None:
202 for r in self.notify_runner:
203 self.rpc_send(r, (stripped_run_dir,), 'sequencingFinished')
207 return bot.main(args)
209 if __name__ == "__main__":
210 sys.exit(main(sys.argv[1:]))