Giter Site home page Giter Site logo

Decorator all the things! about jobtastic HOT 4 OPEN

policystat avatar policystat commented on August 16, 2024
Decorator all the things!

from jobtastic.

Comments (4)

winhamwr avatar winhamwr commented on August 16, 2024

So some possible places to look for implementing this:

@jobtastic_task via shared_task

celery.app.shared_task shows a way to register a task that will be available to all celery app instances. This gives us a way to avoid doing something nasty like monkeypatching the Celery object to add a celery.jobtastic_task or some such.

Steal relevant stuff from _task_from_fun

celery.app.base:Celery._task_from_fun has all of the magic needed to create your task object from a function.

from jobtastic.

winhamwr avatar winhamwr commented on August 16, 2024

Back of the envelope possible solution:

def _jobtastic_task_from_fun(app, fun, **options):
    if 'base' in options:
        raise Exception("'base' can't be used on a JobtasticTask")

    T = type(fun.__name__, (JobtasticTask, ), dict({
        'app': app,
        'accept_magic_kwargs': False,
        'calculate_result': staticmethod(fun),
        '__doc__': fun.__doc__,
        '__module__': fun.__module__}, **options))()
    task = app._tasks[T.name]  # return global instance.
    task.bind(self)
    return task

def jobtastic_task(*args, **kwargs):

    def create_jobtastic_task(**options):

        def __inner(fun):
            name = options.get('name')
            _shared_task(lambda app: _jobtastic_task_from_fun(app, fun, **options))

            for app in _get_active_apps():
                if app.finalized:
                    with app._finalize_mutex:
                        _jobtastic_task_from_fun(app, fun, **options)

            def task_by_cons():
                app = current_app()
                return app.tasks[
                    name or gen_task_name(app, fun.__name__, fun.__module__)
                ]
            return Proxy(task_by_cons)
        return __inner

    if len(args) == 1 and isinstance(args[0], Callable):
        return create_jobtastic_task(**kwargs)(args[0])
    return create_jobtastic_task(*args, **kwargs)

from jobtastic.

brk0v avatar brk0v commented on August 16, 2024

Hi, I'm investigating how to do the same thing. Could you please give a full example of using this kind of pattern.
Thanks!

from jobtastic.

winhamwr avatar winhamwr commented on August 16, 2024

Hi Viacheslav,

Unfortunately, that back of the envelope example is the closest I've come. If I had a full example, it would already be merged in :P If you give this a try and you run in to specific problems, I'd be happy to help with some debugging, but I haven't yet been able to prioritize building this out completely. Decorators would be awesome, though.

-Wes

from jobtastic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.