Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running scheduled jobs #74

Closed
erikash opened this issue Feb 26, 2016 · 12 comments

Comments

Projects
None yet
6 participants
@erikash
Copy link

commented Feb 26, 2016

Hi,
Kudos for the great job!
This will be definitely take django to the next level!

Are you planning to include support for scheduled jobs? Like celery beat?

Any suggested workarounds?

Best,
Erik.

@andrewgodwin

This comment has been minimized.

Copy link
Member

commented Feb 26, 2016

Yes, the intention is to have some sort of command that can push messages onto channels periodically. In the meantime, I suggest writing a custom management command that does this and calling it using cron.

@erikash

This comment has been minimized.

Copy link
Author

commented Feb 26, 2016

Thanks, sounds great

@avayo

This comment has been minimized.

Copy link

commented Apr 21, 2016

Hello Andrew. Thank you for the great project!
Can you (or someone else) suggest the best way to send signals to channels via cron?

@andrewgodwin

This comment has been minimized.

Copy link
Member

commented Apr 21, 2016

For now, I recommend writing a management command that sends the signals onto the channels, and calling that from cron.

@mrasband

This comment has been minimized.

Copy link

commented May 12, 2016

For anyone else coming here and not being sure where to start (like me), a quick example:

# settings.py
CHANNEL_LAYERS = {
    # This is the channel layer id, you can have a bunch if you want
    'default': {
        'BACKEND': 'asgi_redis.RedisChannelLayer',
        'CONFIG': {
            'hosts': [os.environ.get('REDIS_URL', 'redis://localhost:6379')],
        },
        # this defines the router for the layer, e.g. the definitions of channels and their handlers
        'ROUTING': 'app.routing.channel_routing',  
    }
}

# app/routing.py
channel_routing = {
    # You can name your channel anything you want, you probably see 'websocket.*' in most tutorials.
    'your_channel_name': lambda x: print('Channel Triggered Event!'),
}

# ./manage.py shell
from channels import Channel, channel_layers

# default here matches your layer name (from settings.py) that can delegate to your_channel_name
c = Channel('your_channel_name', channel_layer=channel_layers['default'])
c.send({})

Adding a command is pretty simple, see here and then you can just call ./manage.py commandname from cron

@andrewgodwin

This comment has been minimized.

Copy link
Member

commented May 12, 2016

Yup, that's a great example - thanks @mrasband! I'll see if I can work stuff for calling from cron into the docs - it also looks like we're going to have some work start on a scheduled task interface server soon (similar to celerybeat), which might be better if you want things very frequently or can't set cron tasks.

@mrasband

This comment has been minimized.

Copy link

commented May 12, 2016

Thank you @andrewgodwin for the awesome work, this has helped swing me back to using Django more :)

@andrewgodwin

This comment has been minimized.

Copy link
Member

commented Jun 28, 2016

I believe @jacobian was looking into it, but I don't know what sort of progress he's made, or if he's found the time to start yet.

@ssteinerx

This comment has been minimized.

Copy link
Contributor

commented Sep 11, 2016

You can do it in Twisted in three lines:

from twisted.internet.task import LoopingCall
looper = LoopingCall(send_some_message_function)
looper.start(1)

looper.start(1) will call the function send_some_message_function once every second.

@ssteinerx

This comment has been minimized.

Copy link
Contributor

commented Sep 12, 2016

looper.stop()

docs

@JohnDoee

This comment has been minimized.

Copy link

commented Oct 13, 2016

I went ahead and created a thin wrapper around apscheduler add_job command that'll send a message back on a specified channel when triggered in my own asgi implementation.

It would be simple to squeeze it into Daphne too and solves most, if not all, delayed-call / scheduling issues.

@andrewgodwin

This comment has been minimized.

Copy link
Member

commented Feb 1, 2018

Closing this as there's delay for channels 1 and in channels 2 you can just use asyncio stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.