import cherrypy
import json 
import urllib
from celery.result import AsyncResult
from celery.execute import send_task


'''
Run without arguments:
www.cybercommons.org/api/q/run/teco

Run with positional arguments
www.cybercommons.org/api/q/run/teco/SomeString/Anotherstring/

Run with keyword arguments:
www.cybercommons.org/api/q/run/teco?arg1=SomeString&arg2=Anotherstring

www.cybercommons.org/api/q/status/UUID


Method name lookups should be driven by catalog.
[
('teco', 'cybercomq.model.teco.task.runTeco'),
('setTecoInput', 'cybercomq.model.teco.task.setTecoinput')
]


'''

def mimetype(type):
    def decorate(func):
        def wrapper(*args, **kwargs):
            cherrypy.response.headers['Content-Type'] = type
            return func(*args, **kwargs)
        return wrapper
    return decorate

class Root(object):
    @cherrypy.expose
    def index(self):
        return None
    @cherrypy.expose
    @mimetype('application/json')
    def run(self,*args,**kwargs):
        funcname = args[0]
        queue = args[1]
        funcargs = args[2:]
        taskobj = send_task( funcname, args=funcargs, kwargs=kwargs, queue=queue, track_started=True )
        return json.dumps({'task_id':taskobj.task_id}, indent=2)
    @cherrypy.expose
    @mimetype('application/json')
    def task(self,task_id=None,type=None,**kwargs):
        if task_id == None:
            return json.dumps({'available_urls':['/<task_id>/','/<task_id>/status/','/<task_id>/tombstone/']},indent=2)
        if type == None:
            res = {}
            result = urllib.urlopen("http://fire.rccc.ou.edu/mongo/db_find/cybercom_queue/cybercom_queue_meta/{'spec':{'_id':'" + task_id + "'}}")
            res["tombstone"]=json.loads(result.read())
            res['status']=AsyncResult(task_id).status
            res['task_id']=task_id
            return json.dumps(res,indent=2)
        if type.lower() == 'status':
            return json.dumps({"status": AsyncResult(task_id).status},indent=2)
        if type.lower() == 'tombstone':
            res={}
            result = urllib.urlopen("http://fire.rccc.ou.edu/mongo/db_find/cybercom_queue/cybercom_queue_meta/{'spec':{'_id':'" + task_id + "'}}")
            res['tombstone']=json.loads(result.read())
            res['task_id']=task_id            
            return json.dumps(res,indent=2) #result.read()
        return json.dumps({'available_urls':['/<task_id>/','/<task_id>/status/','/<task_id>/tombstone/']},indent=2)
cherrypy.tree.mount(Root())
application = cherrypy.tree

if __name__ == '__main__':
    cherrypy.engine.start()
    cherrypy.engine.block()


@@ -111,17 +111,56 @@
 nd_task%0A
+from celery.task.control import inspect
 %0A
-
 %0A'''%0ARun
@@ -616,16 +616,127 @@
 %0A%0A%0A'''%0A%0A
+i = inspect()%0AREGISTERED_TASKS = set()%0Afor item in i.registered().values():%0A    REGISTERED_TASKS.update(item)%0A%0A
 def mime
@@ -1118,32 +1118,32 @@
 args,**kwargs):%0A
-
         funcname
@@ -1153,16 +1153,180 @@
 args%5B0%5D%0A
+        if funcname not in REGISTERED_TASKS:%0A            return json.dumps(%7B'error': %22Unknown task%22, 'available_tasks': list(REGISTERED_TASKS)%7D, indent=2)%0A        %0A
         

Test for known tasks