예제 #1
0
    async def enqueue(self, name, **args):
        """
        Enqueue job with name from argument.

        :return: enqueued :class:`core4.queue.job.CoreJob`` instance
        """
        try:
            job = self.queue.job_factory(name, **args)
        except Exception:
            exc_info = sys.exc_info()
            raise HTTPError(404, "cannot instantiate job [%s]: %s:\n%s", name,
                            repr(exc_info[1]),
                            traceback.format_exception(*exc_info))
        if not await self.user.has_job_exec_access(job.qual_name()):
            raise HTTPError(403)
        job.__dict__["attempts_left"] = job.__dict__["attempts"]
        job.__dict__["state"] = core4.queue.job.STATE_PENDING
        job.__dict__["enqueued"] = self.who()
        doc = job.serialise()
        try:
            ret = await self.collection("queue").insert_one(doc)
        except pymongo.errors.DuplicateKeyError:
            raise HTTPError(400, "job [%s] exists with args %s",
                            job.qual_name(), job.args)
        job.__dict__["_id"] = ret.inserted_id
        job.__dict__["identifier"] = ret.inserted_id
        self.logger.info('successfully enqueued [%s] with [%s]',
                         job.qual_name(), job._id)
        await self.make_stat("enqueue_job", str(job._id))
        return job
예제 #2
0
    async def post(self, _id=None):
        """
        **DEPRECATED!** Use :class:`core4.api.v1.request.job.JobRequest`.

        Only jobs with execute access permissions granted to the current user
        can be posted.

        Methods:
            POST /core4/api/v1/enqueue - enqueue job

        Parameters:
            - args (dict): arguments to be passed to the job
            - attempts (int): maximum number of execution attempts after job
                              failure before the job enters the final ``error``
                              state
            - chain (list of str): list of jobs to be started after successful
                                   job completion
            - defer_time (int): seconds to wait before restart after defer
            - defer_max (int): maximum number of seconds to defer the job before
                               the job turns inactive
            - dependency (list of str): jobs which need to be completed before
                                        execution start
            - error_time (int): seconds to wait before job restart after failure
            - force (bool): if ``True`` then ignore worker resource limits and
                            launch the job
            - max_parallel (int): maximum number jobs to run in parallel on the
                                  same node
            - priority (int): to execute the job with >0 higher and <0 lower
                              priority (defaults to 0)
            - python (str): Python executable to be used for dedicated Python
                            virtual environment
            - wall_time (int): number of seconds before a running job turns into
                               a non-stopping job
            - worker (list of str): eligable to execute the job
            - zombie_time (int): number of seconds before a job turns into a
                                 zombie non-stopping job

        Returns:
            data element with

            - **_id**: of the enqueued job
            - **name**: of the enqueued job

        Raises:
            400: job exists with args
            401: Unauthorized
            403: Forbidden
            404: cannot instantiate job

        Examples:
            >>> from requests import post, get
            >>> url = "http://localhost:5001/core4/api/v1"
            >>> signin = get(url + "/login?username=admin&password=hans")
            >>> token = signin.json()["data"]["token"]
            >>> h = {"Authorization": "Bearer " + token}
            >>> name = "core4.queue.helper.job.example.DummyJob"
            >>> rv = post(url + "/jobs/enqueue?name=" + name, headers=h)
            >>> rv.json()
            {
                '_id': '5bdb554fde8b6925830b8b39',
                'code': 200,
                'message': 'OK',
                'timestamp': '2018-11-01T19:34:39.542516',
                'data': {
                    '_id': '5bdb554fde8b6925830b8b3e',
                    'name': 'core4.queue.helper.DummyJob'
                }
            }
        """
        job = await self.enqueue_by_args()
        self.reply({"name": job.qual_name(), "_id": job._id})