def cancel_job(self, job_id: str): """ Cancel a job with the given job_id so it won't be run. :param job_id: the job_id to cancel :raise JobNotFoundError: if the job could not be found in the database :raise JobCancelationFailureError: if the job could not be canceled """ _LOGGER.info("Canceling job {}".format(job_id)) try: _LOGGER.debug(f"Getting job {job_id} from DB") job = Job.get_or_none(Job.job_id == job_id) if job is None: _LOGGER.error(f"Could not find job {job_id} to cancel") raise JobNotFoundError() if ( job.status == JobStatus.error or job.status == JobStatus.completed or job.status == JobStatus.canceled ): _LOGGER.error(f"Could not cancel job {job_id} with status {job.status}") raise JobCancelationFailureError( "Job with status {} cannot be canceled".format(job.status) ) self._update_job_canceled(job) except (JobNotFoundError, JobCancelationFailureError) as passthrough_err: raise passthrough_err except Exception as err: _LOGGER.warning(f"Error while canceling job {job_id} in db: {err}")
def get_job(job_id: str): """ Route for getting a job matching the given job_id. Raises an HTTPNotFoundError if the job is not found in the database. :param job_id: the id of the job to get :return: a tuple containing (json response, http status code) """ _LOGGER.info("getting job {}".format(job_id)) job = Job.get_or_none(Job.job_id == job_id) if job is None: raise HTTPNotFoundError("could not find job with job_id {}".format(job_id)) resp_schema = ResponseJobSchema() resp_job = resp_schema.dump({"job": job}) resp_schema.validate(resp_job) _LOGGER.info("retrieved job {}".format(resp_job)) return jsonify(resp_job), HTTPStatus.OK.value
def cancel_job(self, job_id: str): """ Cancel a job with the given job_id so it won't be run. Blocks until the job can be canceled. :param job_id: the job_id to cancel :raise JobNotFoundError: if the job could not be found in the database :raise JobCancelationFailureError: if the job could not be canceled """ _LOGGER.info("Canceling job with id {}".format(job_id)) with self._lock: if self._current is not None and self._current.job_id == job_id: self._current.cancel() return with database.connection_context(): job = Job.get_or_none(Job.job_id == job_id) if job is None: _LOGGER.error( "Could not find job with id {}".format(job_id)) raise JobNotFoundError( "Could not find job with id {}".format(job_id)) if (job.status == JobStatus.error or job.status == JobStatus.completed or job.status == JobStatus.canceled): _LOGGER.error("Could not cancel job with status {}".format( job.status)) raise JobCancelationFailureError( "Job with status {} cannot be canceled".format( job.status)) job.status = JobStatus.canceled job.save()