c1d4ba70b7f6a172513b3644d716eb6386373cbe
[htsworkflow.git] / gaworkflow / runner.py
1 #!/usr/bin/env python
2 import logging
3 import os
4 import re
5 import sys
6 import time
7 import threading
8
9 from benderjab import rpc
10
11 from gaworkflow.pipeline.configure_run import *
12 from gaworkflow.pipeline.monitors import startCmdLineStatusMonitor
13
14 #s_fc = re.compile('FC[0-9]+')
15 s_fc = re.compile('_[0-9a-zA-Z]*$')
16
17
18 def _get_flowcell_from_rundir(run_dir):
19     """
20     Returns flowcell string based on run_dir.
21     Returns None and logs error if flowcell can't be found.
22     """
23     junk, dirname = os.path.split(run_dir)
24     mo = s_fc.search(dirname)
25     if not mo:
26         logging.error('RunDir 2 FlowCell error: %s' % (run_dir))
27         return None
28
29     return dirname[mo.start()+1:]
30     
31
32
33 class Runner(rpc.XmlRpcBot):
34     """
35     Manage running pipeline jobs.
36     """    
37     def __init__(self, section=None, configfile=None):
38         #if configfile is None:
39         #    self.configfile = "~/.gaworkflow"
40         super(Runner, self).__init__(section, configfile)
41         
42         self.cfg['notify_users'] = None
43         self.cfg['genome_dir'] = None
44         self.cfg['base_analysis_dir'] = None
45
46         self.cfg['notify_users'] = None
47         self.cfg['notify_postanalysis'] = None
48
49         self.conf_info_dict = {}
50         
51         self.register_function(self.sequencingFinished)
52         #self.eventTasks.append(self.update)
53
54     
55     def read_config(self, section=None, configfile=None):
56         super(Runner, self).read_config(section, configfile)
57
58         self.genome_dir = self._check_required_option('genome_dir')
59         self.base_analysis_dir = self._check_required_option('base_analysis_dir')
60
61         self.notify_users = self._parse_user_list(self.cfg['notify_users'])
62         #FIXME: process notify_postpipeline cfg
63         
64     
65     def _parser(self, msg, who):
66         """
67         Parse xmpp chat messages
68         """
69         help = u"I can send [start] a run, or report [status]"
70         if re.match(u"help", msg):
71             reply = help
72         elif re.match("status", msg):            
73             reply = u"not implemented"
74         elif re.match(u"start", msg):
75             words = msg.split()
76             if len(words) == 2:
77                 self.sequencingFinished(words[1])
78                 reply = u"starting run for %s" % (words[1])
79             else:
80                 reply = u"need runfolder name"
81         else:
82             reply = u"I didn't understand '%s'" %(msg)
83
84         logging.debug("reply: " + str(reply))
85         return reply
86
87         
88     def start(self, daemonize):
89         """
90         Start application
91         """
92         super(Runner, self).start(daemonize)
93
94         
95     def stop(self):
96         """
97         shutdown application
98         """
99         super(Runner, self).stop()
100
101             
102     def sequencingFinished(self, run_dir):
103         """
104         Sequenceing (and copying) is finished, time to start pipeline
105         """
106         logging.debug("received sequencing finished message")
107
108         # Setup config info object
109         ci = ConfigInfo()
110         ci.base_analysis_dir = self.base_analysis_dir
111         ci.analysis_dir = os.path.join(self.base_analysis_dir, run_dir)        
112
113         # get flowcell from run_dir name
114         flowcell = _get_flowcell_from_rundir(run_dir)
115
116         # Store ci object in dictionary
117         self.conf_info_dict[flowcell] = ci
118
119
120         # Launch the job in it's own thread and turn.
121         self.launchJob(run_dir, flowcell, ci)
122         
123         
124     def pipelineFinished(self, run_dir):
125         # need to strip off self.watch_dir from rundir I suspect.
126         logging.info("pipeline finished in" + str(run_dir))
127         #pattern = self.watch_dir
128         #if pattern[-1] != os.path.sep:
129         #    pattern += os.path.sep
130         #stripped_run_dir = re.sub(pattern, "", run_dir)
131         #logging.debug("stripped to " + stripped_run_dir)
132
133         # Notify each user that the run has finished.
134         if self.notify_users is not None:
135             for u in self.notify_users:
136                 self.send(u, 'Pipeline run %s finished' % (run_dir))
137                 
138         #if self.notify_runner is not None:
139         #    for r in self.notify_runner:
140         #        self.rpc_send(r, (stripped_run_dir,), 'sequencingFinished')
141
142     def reportMsg(self, msg):
143
144         if self.notify_users is not None:
145             for u in self.notify_users:
146                 self.send(u, msg)
147
148
149     def _runner(self, run_dir, flowcell, conf_info):
150
151         # retrieve config step
152         cfg_filepath = os.path.join(conf_info.analysis_dir,
153                                     'config32auto.txt')
154         status_retrieve_cfg = retrieve_config(conf_info,
155                                           flowcell,
156                                           cfg_filepath,
157                                           self.genome_dir)
158         if status_retrieve_cfg:
159             logging.info("Runner: Retrieve config: success")
160             self.reportMsg("Retrieve config (%s): success" % (run_dir))
161         else:
162             logging.error("Runner: Retrieve config: failed")
163             self.reportMsg("Retrieve config (%s): FAILED" % (run_dir))
164
165         
166         # configure step
167         if status_retrieve_cfg:
168             status = configure(conf_info)
169             if status:
170                 logging.info("Runner: Configure: success")
171                 self.reportMsg("Configure (%s): success" % (run_dir))
172             else:
173                 logging.error("Runner: Configure: failed")
174                 self.reportMsg("Configure (%s): FAILED" % (run_dir))
175
176             #if successful, continue
177             if status:
178                 # Setup status cmdline status monitor
179                 #startCmdLineStatusMonitor(ci)
180                 
181                 # running step
182                 print 'Running pipeline now!'
183                 run_status = run_pipeline(conf_info)
184                 if run_status is True:
185                     logging.info('Runner: Pipeline: success')
186                     self.piplineFinished(run_dir)
187                 else:
188                     logging.info('Runner: Pipeline: failed')
189                     self.reportMsg("Pipeline run (%s): FAILED" % (run_dir))
190
191
192     def launchJob(self, run_dir, flowcell, conf_info):
193         """
194         Starts up a thread for running the pipeline
195         """
196         t = threading.Thread(target=self._runner,
197                         args=[run_dir, flowcell, conf_info])
198         t.setDaemon(True)
199         t.start()
200         
201
202         
203 def main(args=None):
204     bot = Runner('demobot')
205     bot.cfg['loglevel'] = 'DEBUG'
206     return bot.main(args)
207     
208 if __name__ == "__main__":
209     sys.exit(main(sys.argv[1:]))
210