/lib/galaxy/web/controllers/async.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 138 lines · 90 code · 25 blank · 23 comment · 22 complexity · 8f31dd26033f301dc9a03d7aad7c601c MD5 · raw file

  1. """
  2. Upload class
  3. """
  4. from galaxy.web.base.controller import *
  5. from galaxy import jobs, util, datatypes, web
  6. import logging, urllib, sys
  7. from galaxy.util.hash_util import *
  8. log = logging.getLogger( __name__ )
  9. class ASync( BaseUIController ):
  10. @web.expose
  11. def default(self, trans, tool_id=None, data_id=None, data_secret=None, **kwd):
  12. """Catches the tool id and redirects as needed"""
  13. return self.index( trans, tool_id=tool_id, data_id=data_id, data_secret=data_secret, **kwd)
  14. @web.expose
  15. def index(self, trans, tool_id=None, data_secret=None, **kwd):
  16. """Manages ascynchronous connections"""
  17. if tool_id is None:
  18. return "tool_id argument is required"
  19. tool_id=str(tool_id)
  20. #log.debug('async params -> %s' % kwd)
  21. # redirect to main when getting no parameters
  22. if not kwd:
  23. return trans.response.send_redirect( "/index" )
  24. history = trans.get_history( create=True )
  25. params = util.Params(kwd, sanitize=False)
  26. STATUS = params.STATUS
  27. URL = params.URL
  28. data_id = params.data_id
  29. log.debug('async dataid -> %s' % data_id)
  30. trans.log_event( 'Async dataid -> %s' % str(data_id) )
  31. # initialize the tool
  32. toolbox = self.get_toolbox()
  33. tool = toolbox.tools_by_id.get(tool_id, '')
  34. if not tool:
  35. return "Tool with id %s not found" % tool_id
  36. #
  37. # we have an incoming data_id
  38. #
  39. if data_id:
  40. if not URL:
  41. return "No URL parameter was submitted for data %s" % data_id
  42. data = trans.sa_session.query( trans.model.HistoryDatasetAssociation ).get( data_id )
  43. if not data:
  44. return "Data %s does not exist or has already been deleted" % data_id
  45. if STATUS == 'OK':
  46. key = hmac_new( trans.app.config.tool_secret, "%d:%d" % ( data.id, data.history_id ) )
  47. if key != data_secret:
  48. return "You do not have permission to alter data %s." % data_id
  49. # push the job into the queue
  50. data.state = data.blurb = data.states.RUNNING
  51. log.debug('executing tool %s' % tool.id)
  52. trans.log_event( 'Async executing tool %s' % tool.id, tool_id=tool.id )
  53. galaxy_url = trans.request.base + '/async/%s/%s/%s' % ( tool_id, data.id, key )
  54. galaxy_url = params.get("GALAXY_URL",galaxy_url)
  55. params = dict( url=URL, GALAXY_URL=galaxy_url )
  56. # Assume there is exactly one output file possible
  57. params[tool.outputs.keys()[0]] = data.id
  58. tool.execute( trans, incoming=params )
  59. else:
  60. log.debug('async error -> %s' % STATUS)
  61. trans.log_event( 'Async error -> %s' % STATUS )
  62. data.state = data.blurb = jobs.JOB_ERROR
  63. data.info = "Error -> %s" % STATUS
  64. trans.sa_session.flush()
  65. return "Data %s with status %s received. OK" % (data_id, STATUS)
  66. #
  67. # no data_id must be parameter submission
  68. #
  69. if not data_id and len(params)>3:
  70. if params.galaxyFileFormat == 'wig':
  71. GALAXY_TYPE = 'wig'
  72. else:
  73. GALAXY_TYPE = params.GALAXY_TYPE or 'interval'
  74. GALAXY_NAME = params.GALAXY_NAME or '%s query' % tool.name
  75. GALAXY_INFO = params.GALAXY_INFO or params.galaxyDescription or ''
  76. GALAXY_BUILD = params.GALAXY_BUILD or params.galaxyFreeze or 'hg17'
  77. #data = datatypes.factory(ext=GALAXY_TYPE)()
  78. #data.ext = GALAXY_TYPE
  79. #data.name = GALAXY_NAME
  80. #data.info = GALAXY_INFO
  81. #data.dbkey = GALAXY_BUILD
  82. #data.state = jobs.JOB_OK
  83. #history.datasets.add_dataset( data )
  84. data = trans.app.model.HistoryDatasetAssociation( create_dataset=True, sa_session=trans.sa_session, extension=GALAXY_TYPE )
  85. trans.app.security_agent.set_all_dataset_permissions( data.dataset, trans.app.security_agent.history_get_default_permissions( trans.history ) )
  86. data.name = GALAXY_NAME
  87. data.dbkey = GALAXY_BUILD
  88. data.info = GALAXY_INFO
  89. trans.sa_session.add( data ) #Need to add data to session before setting state (setting state requires that the data object is in the session, but this may change)
  90. data.state = data.states.NEW
  91. open( data.file_name, 'wb' ).close() #create the file
  92. trans.history.add_dataset( data, genome_build=GALAXY_BUILD )
  93. trans.sa_session.add( trans.history )
  94. trans.sa_session.flush()
  95. trans.log_event( "Added dataset %d to history %d" %(data.id, trans.history.id ), tool_id=tool_id )
  96. try:
  97. key = hmac_new( trans.app.config.tool_secret, "%d:%d" % ( data.id, data.history_id ) )
  98. galaxy_url = trans.request.base + '/async/%s/%s/%s' % ( tool_id, data.id, key )
  99. params.update( { 'GALAXY_URL' :galaxy_url } )
  100. params.update( { 'data_id' :data.id } )
  101. url = tool.action + '?' + urllib.urlencode( params.flatten() )
  102. log.debug("connecting to -> %s" % url)
  103. trans.log_event( "Async connecting to -> %s" % url )
  104. text = urllib.urlopen(url).read(-1)
  105. text = text.strip()
  106. if not text.endswith('OK'):
  107. raise Exception, text
  108. data.state = data.blurb = data.states.RUNNING
  109. except Exception, e:
  110. data.info = str(e)
  111. data.state = data.blurb = data.states.ERROR
  112. trans.sa_session.flush()
  113. return trans.fill_template( 'tool_executed.mako', history=history, toolbox=toolbox, tool=tool, util=util, out_data={} )