Beispiel #1
0
    def test_register_decorator_registers_without_args(self):
        @register_task
        def add(x, y):
            return x + y

        @register_task()
        def subtract(x, y):
            return x - y

        add_funcstr = stringify_func(add)
        subtract_funcstr = stringify_func(subtract)

        self.assertIsInstance(self.registered_jobs[add_funcstr], RegisteredJob)
        self.assertIsInstance(self.registered_jobs[subtract_funcstr],
                              RegisteredJob)
Beispiel #2
0
    def test_can_enqueue_single_job(self, defaultbackend, simplejob):
        job_id = defaultbackend.enqueue_job(simplejob, QUEUE)

        new_job = defaultbackend.get_job(job_id)

        # Does the returned job record the function we set to run?
        assert str(new_job.func) == stringify_func(id)

        # Does the job have the right state (QUEUED)?
        assert new_job.state == State.QUEUED
Beispiel #3
0
def register_task(
    func=None,
    job_id=None,
    validator=None,
    priority=Priority.REGULAR,
    group=None,
    cancellable=False,
    track_progress=False,
    permission_classes=[],
):
    """
    Registers the decorated function as task.
    """
    if func is None:
        return partial(
            register_task,
            job_id=job_id,
            validator=validator,
            priority=priority,
            group=group,
            cancellable=cancellable,
            track_progress=track_progress,
            permission_classes=permission_classes,
        )

    registered_job = RegisteredJob(
        func,
        job_id=job_id,
        validator=validator,
        priority=priority,
        group=group,
        cancellable=cancellable,
        track_progress=track_progress,
        permission_classes=permission_classes,
    )

    func.enqueue = registered_job.enqueue
    func.enqueue_in = registered_job.enqueue_in
    func.enqueue_at = registered_job.enqueue_at

    funcstring = stringify_func(func)
    JobRegistry.REGISTERED_JOBS[funcstring] = registered_job

    logger.debug("Successfully registered '%s' as job.", funcstring)

    return func
Beispiel #4
0
    def __init__(self, func, *args, **kwargs):
        """
        Create a new Job that will run func given the arguments passed to Job(). If the track_progress keyword parameter
        is given, the worker will pass an update_progress function to update interested parties about the function's
        progress. See Client.__doc__ for update_progress's function parameters.

        :param func: func can be a callable object, in which case it is turned into an importable string,
        or it can be an importable string already.
        """
        if isinstance(func, Job):
            args = copy.copy(func.args)
            kwargs = copy.copy(func.kwargs)
            kwargs["track_progress"] = func.track_progress
            kwargs["cancellable"] = func.cancellable
            kwargs["extra_metadata"] = func.extra_metadata.copy()
            func = func.func
        self.job_id = uuid.uuid4().hex
        self.state = kwargs.pop("state", State.QUEUED)
        self.traceback = ""
        self.exception = None
        self.track_progress = kwargs.pop("track_progress", False)
        self.cancellable = kwargs.pop("cancellable", False)
        self.extra_metadata = kwargs.pop("extra_metadata", {})
        self.progress = 0
        self.total_progress = 0
        self.args = args
        self.kwargs = kwargs

        self.save_meta_method = None
        self.update_progress_method = None
        self.check_for_cancel_method = None

        if callable(func):
            funcstring = stringify_func(func)
        elif isinstance(func, str):
            funcstring = func
        else:
            raise Exception(
                "Error in creating job. We do not know how to "
                "handle a function of type {}".format(type(func))
            )

        self.func = funcstring
Beispiel #5
0
    def test_register_decorator_assigns_api_methods(self):
        @register_task(
            job_id="test",
            validator=id,
            permission_classes=[int],
            priority="high",
            cancellable=True,
            track_progress=True,
            group="math",
        )
        def add(x, y):
            return x + y

        add_registered_job = self.registered_jobs[stringify_func(add)]

        self.assertIsInstance(add_registered_job, RegisteredJob)

        self.assertEqual(add.enqueue, add_registered_job.enqueue)
        self.assertEqual(add.enqueue_in, add_registered_job.enqueue_in)
        self.assertEqual(add.enqueue_at, add_registered_job.enqueue_at)
Beispiel #6
0
    def __init__(self, func, *args, **kwargs):
        """
        Create a new Job that will run func given the arguments passed to Job(). If the track_progress keyword parameter
        is given, the worker will pass an update_progress function to update interested parties about the function's
        progress. See Client.__doc__ for update_progress's function parameters.

        :param func: func can be a callable object, in which case it is turned into an importable string,
        or it can be an importable string already.
        """
        if isinstance(func, Job):
            args = copy.copy(func.args)
            kwargs = copy.copy(func.kwargs)
            kwargs["track_progress"] = func.track_progress
            kwargs["cancellable"] = func.cancellable
            kwargs["extra_metadata"] = func.extra_metadata.copy()
            kwargs["group"] = func.group
            func = func.func
        elif not callable(func) and not isinstance(func, str):
            raise TypeError("Cannot create Job for object of type {}".format(
                type(func)))

        job_id = kwargs.pop("job_id", None)
        if job_id is None:
            job_id = uuid.uuid4().hex

        self.job_id = job_id
        self.state = kwargs.pop("state", State.PENDING)
        self.group = kwargs.pop("group", None)
        self.traceback = ""
        self.exception = None
        self.track_progress = kwargs.pop("track_progress", False)
        self.cancellable = kwargs.pop("cancellable", False)
        self.extra_metadata = kwargs.pop("extra_metadata", {})
        self.progress = 0
        self.total_progress = 0
        self.args = args
        self.kwargs = kwargs
        self.result = None
        self.storage = None
        self.func = stringify_func(func)
Beispiel #7
0
    def test_stringify_func_is_importable(self):
        funcstring = stringify_func(set_flag)
        func = import_stringified_func(funcstring)

        assert set_flag == func