Beispiel #1
0
    async def run(self, queue):
        """
        Run a Job and set state/result accordingly.
        This method is supposed to run in a greenlet.
        """

        if self.options["logs"]:
            logs_dir = os.path.join("/tmp/middlewared/jobs")
            os.makedirs(logs_dir, exist_ok=True)
            self.logs_path = os.path.join(logs_dir, f"{self.id}.log")
            self.logs_fd = open(self.logs_path, "wb", buffering=0)

        try:
            if self.aborted:
                raise asyncio.CancelledError()
            else:
                self.set_state('RUNNING')

            self.future = asyncio.ensure_future(self.__run_body())
            try:
                await self.future
            except Exception as e:
                handled = adapt_exception(e)
                if handled is not None:
                    raise handled
                else:
                    raise
        except asyncio.CancelledError:
            self.set_state('ABORTED')
        except Exception:
            self.set_state('FAILED')
            self.set_exception(sys.exc_info())
            if self.options['transient']:
                logger.error("Transient job failed", exc_info=True)
        finally:
            await self.__close_logs()
            await self.__close_pipes()

            queue.release_lock(self)
            self._finished.set()
            if self.options['transient']:
                queue.remove(self.id)
            else:
                self.middleware.send_event('core.get_jobs',
                                           'CHANGED',
                                           id=self.id,
                                           fields=self.__encode__())
Beispiel #2
0
    async def run(self, queue):
        """
        Run a Job and set state/result accordingly.
        This method is supposed to run in a greenlet.
        """

        if self.options["logs"]:
            self.logs_path = os.path.join(LOGS_DIR, f"{self.id}.log")
            self.start_logging()

        try:
            if self.aborted:
                raise asyncio.CancelledError()
            else:
                self.set_state('RUNNING')
                self.send_event('CHANGED', self.__encode__())

            self.future = asyncio.ensure_future(self.__run_body())
            try:
                await self.future
            except Exception as e:
                handled = adapt_exception(e)
                if handled is not None:
                    raise handled
                else:
                    raise
        except asyncio.CancelledError:
            self.set_state('ABORTED')
        except Exception:
            self.set_state('FAILED')
            self.set_exception(sys.exc_info())
            logger.error("Job %r failed", self.method, exc_info=True)
        finally:
            await self.__close_logs()
            await self.__close_pipes()

            queue.release_lock(self)
            self._finished.set()
            self.send_event('CHANGED', self.__encode__())
            if self.options['transient']:
                queue.remove(self.id)