Commit b03b6abe authored by Cage, Gregory's avatar Cage, Gregory
Browse files

Add new endpoint for getting stdout and stderr

parent 15852de1
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -228,7 +228,7 @@ class JobManager:
        )
        return self.job_lock()

    def get_accessible_job(self, trans, decoded_job_id, stdout_position=-1, stdout_length=0, stderr_position=-1, stderr_length=0):
    def get_accessible_job(self, trans, decoded_job_id):
        job = trans.sa_session.query(trans.app.model.Job).filter(trans.app.model.Job.id == decoded_job_id).first()
        if job is None:
            raise ObjectNotFound()
@@ -245,8 +245,15 @@ class JobManager:
                if not self.dataset_manager.is_accessible(data_assoc.dataset.dataset, trans.user):
                    raise ItemAccessibilityException("You are not allowed to rerun this job.")
        trans.sa_session.refresh(job)
        return job

    def get_job_console_output(self, trans, job, stdout_position=-1, stdout_length=0, stderr_position=-1, stderr_length=0):
        if job is None:
            raise ObjectNotFound()

        # If stdout_length and stdout_position are good values, then load standard out and add it to status
        console_output = dict()
        console_output["state"] = job.state
        if job.state == job.states.RUNNING:
            working_directory = trans.app.object_store.get_filename(
                job, base_dir="job_work", dir_only=True, obj_dir=True
@@ -256,8 +263,7 @@ class JobManager:
                    stdout_path = Path(working_directory) / STDOUT_LOCATION
                    stdout_file = open(stdout_path, "r")
                    stdout_file.seek(stdout_position)
                    job.job_stdout = stdout_file.read(stdout_length)
                    job.tool_stdout = job.job_stdout
                    console_output["stdout"] = stdout_file.read(stdout_length)
                except Exception as e:
                    log.error("Could not read STDOUT: %s", e)
            if stderr_length > 0 and stderr_position > -1:
@@ -265,11 +271,13 @@ class JobManager:
                    stderr_path = Path(working_directory) / STDERR_LOCATION
                    stderr_file = open(stderr_path, "r")
                    stderr_file.seek(stderr_position)
                    job.job_stderr = stderr_file.read(stderr_length)
                    job.tool_stderr = job.job_stderr
                    console_output["stderr"] = stderr_file.read(stderr_length)
                except Exception as e:
                    log.error("Could not read STDERR: %s", e)
        return job
        else:
            console_output["stdout"] = job.tool_stdout
            console_output["stderr"] = job.tool_stderr
        return console_output

    def stop(self, job, message=None):
        if not job.finished:
+43 −10
Original line number Diff line number Diff line
@@ -178,10 +178,6 @@ class FastAPIJobs:
        id: DecodedDatabaseIdField,
        trans: ProvidesUserContext = DependsOnTrans,
        full: Optional[bool] = False,
        stdout_position: Optional[int] = None,
        stdout_length: Optional[int] = None,
        stderr_position: Optional[int] = None,
        stderr_length: Optional[int] = None,
    ) -> Dict[str, Any]:
        """
        Return dictionary containing description of job data
@@ -189,17 +185,11 @@ class FastAPIJobs:
        Parameters
        - id: ID of job to return
        - full: Return extra information ?
        - stdout_position: The index of the character to begine reading stdout from
        - stdout_length: How many characters of stdout to read
        """
        return self.service.show(
            trans,
            id,
            bool(full),
            int(stdout_position) if stdout_position else 0,
            int(stdout_length) if stdout_length else 0,
            int(stderr_position) if stderr_position else 0,
            int(stderr_length) if stderr_length else 0,
        )

    @router.get("/api/jobs")
@@ -364,6 +354,49 @@ class JobController(BaseGalaxyAPIController, UsesVisualizationMixin):
            exceptions.RequestParameterInvalidException(f"Job with id '{job.tool_id}' is not running.")
        return self.__dictify_associations(trans, job.output_datasets, job.output_library_datasets)

    @expose_api
    def console_output(
            self,
            trans: ProvidesUserContext,
            id,
            stdout_position,
            stdout_length,
            stderr_position,
            stderr_length,
            **kwd
    ):
        """
         * GET /api/jobs/{id}/console_output
                Get the stdout and/or stderr of a job.

        :type   id: string
        :param  id: Encoded job id

        :type   stdout_position: int
        :param  stdout_position: The index of the character to begin reading stdout from

        :type   stdout_length: int
        :param  stdout_length: How many characters of stdout to read

        :type   stderr_position: int
        :param  stderr_position: The index of the character to begin reading stderr from

        :type   stderr_length: int
        :param  stderr_length: How many characters of stderr to read

        :rtype:     dict
        :returns:   dict containing stdout and stderr fields
        """
        job = self.__get_job(trans, id)
        return self.job_manager.get_job_console_output(
            trans,
            job,
            int(stdout_position),
            int(stdout_length),
            int(stderr_position),
            int(stderr_length)
        )

    @expose_api_anonymous
    def metrics(self, trans: ProvidesUserContext, **kwd):
        """
+7 −0
Original line number Diff line number Diff line
@@ -1007,6 +1007,13 @@ def populate_api_routes(webapp, app):
    webapp.mapper.connect(
        "finish", "/api/jobs/{id}/finish", controller="jobs", action="finish", conditions=dict(method=["PUT"])
    )
    webapp.mapper.connect(
        "console_output",
        "/api/jobs/{id}/console_output",
        controller="jobs",
        action="console_output",
        conditions=dict(method=["GET"])
    )
    webapp.mapper.connect(
        "job_error", "/api/jobs/{id}/error", controller="jobs", action="error", conditions=dict(method=["POST"])
    )
+1 −5
Original line number Diff line number Diff line
@@ -48,12 +48,8 @@ class JobsService:
        trans: ProvidesUserContext,
        id: DecodedDatabaseIdField,
        full: bool = False,
        stdout_position: int = 0,
        stdout_length: int = 0,
        stderr_position: int = 0,
        stderr_length: int = 0,
    ) -> Dict[str, Any]:
        job = self.job_manager.get_accessible_job(trans, id, stdout_position, stdout_length, stderr_position, stderr_length)
        job = self.job_manager.get_accessible_job(trans, id,)
        return view_show_job(trans, job, bool(full))

    def index(