Commit c6516243 authored by Jerome Mariette's avatar Jerome Mariette

add a first version of the jflow_server

parent 77c5fa08
#
# Copyright (C) 2012 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import cherrypy
import json
import sys
from functools import wraps
import time
try:
import _preamble
except ImportError:
sys.exc_clear()
from jflow.workflows_manager import WorkflowsManager
import jflow.utils as utils
from cctools.util import time_format
def json_workflow_status(workflow):
if workflow.start_time: start_time = time.asctime(time.localtime(workflow.start_time))
else: start_time = "-"
if workflow.start_time and workflow.end_time: elapsed_time = str(workflow.end_time-workflow.start_time)
elif workflow.start_time: elapsed_time = str(time.time()-workflow.start_time)
else: elapsed_time = "-"
if workflow.end_time: end_time = time.asctime(time.localtime(workflow.end_time))
else: end_time = "-"
components = []
for i, component in enumerate(workflow.get_components_nameid()):
status_info = workflow.get_component_status(component)
try: perc_waiting = (status_info["waiting"]*100.0)/status_info["tasks"]
except: perc_waiting = 0
try: perc_running = (status_info["running"]*100.0)/status_info["tasks"]
except: perc_running = 0
try: perc_failed = (status_info["failed"]*100.0)/status_info["tasks"]
except: perc_failed = 0
try: perc_aborted = (status_info["aborted"]*100.0)/status_info["tasks"]
except: perc_aborted = 0
try: perc_completed = (status_info["completed"]*100.0)/status_info["tasks"]
except: perc_completed = 0
components.append({"name": component,
"elapsed_time": time_format(status_info["time"]),
"total": status_info["tasks"],
"waiting": status_info["waiting"],
"failed": status_info["failed"],
"running": status_info["running"],
"aborted": status_info["aborted"],
"completed": status_info["completed"]})
status = {"id":utils.get_nb_string(workflow.id),
"name": workflow.name,
"status": workflow.get_status(),
"elapsed_time": str(elapsed_time),
"start_time": start_time,
"end_time": end_time,
"components": components}
return status
class JFlowServer (object):
def __init__(self):
# Create a workflow manager to get access to our workflows
self.wfmanager = WorkflowsManager()
def jsonify(func):
'''JSON decorator for CherryPy'''
@wraps(func)
def wrapper(*args, **kw):
value = func(*args, **kw)
cherrypy.response.headers["Content-Type"] = "application/json"
return json.dumps(value)
return wrapper
@cherrypy.expose
@jsonify
def get_available_workflows(self, **kwargs):
workflows = []
wf_instances = self.wfmanager.get_available_workflows()
for instance in wf_instances:
parameters = []
for param in instance.parameters:
parameters.append({"help": param.help,
"required": param.required,
"default": param.default,
"choices": param.choices,
"name": param.name})
workflows.append({"name": instance.name,
"help": instance.description,
"params": parameters})
return workflows
@cherrypy.expose
@jsonify
def rerun_workflow(self, **kwargs):
self.wfmanager.rerun(kwargs["workflow_id"])
workflow = self.wfmanager.get_workflow(kwargs["workflow_id"])
return json_workflow_status(workflow)
@cherrypy.expose
@jsonify
def get_workflows_status(self, **kwargs):
if kwargs.has_key("workflow_id"):
workflow = self.wfmanager.get_workflow(kwargs["workflow_id"])
return json_workflow_status(workflow)
else:
status = []
workflows = self.wfmanager.get_workflows()
for workflow in workflows:
status.append(json_workflow_status(workflow))
return status
cherrypy.quickstart(JFlowServer())
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment