From 49fd580ddeafcb74b9841df50bffef426cdc3b85 Mon Sep 17 00:00:00 2001 From: Kiran Jonnalagadda Date: Mon, 10 Feb 2014 11:49:13 +0530 Subject: [PATCH] Background tweeting with Flask-RQ --- hasjob/__init__.py | 2 ++ hasjob/twitter.py | 5 +++++ hasjob/views/listing.py | 2 +- requirements.txt | 1 + rq.sh | 3 +++ rqdev.py | 16 ++++++++++++++++ rqinit.py | 15 +++++++++++++++ 7 files changed, 43 insertions(+), 1 deletion(-) create mode 100755 rq.sh create mode 100644 rqdev.py create mode 100644 rqinit.py diff --git a/hasjob/__init__.py b/hasjob/__init__.py index 263a734b6..0714cd784 100644 --- a/hasjob/__init__.py +++ b/hasjob/__init__.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- from flask import Flask +from flask.ext.rq import RQ from flask.ext.mail import Mail from flask.ext.lastuser import Lastuser from baseframe import baseframe, assets, Version @@ -31,6 +32,7 @@ # Configure the app def init_for(env): coaster.app.init_app(app, env) + RQ(app) if app.config.get('SERVER_NAME'): subdomain = 'static' else: diff --git a/hasjob/twitter.py b/hasjob/twitter.py index 93ae5bc5a..288d18953 100644 --- a/hasjob/twitter.py +++ b/hasjob/twitter.py @@ -1,3 +1,6 @@ +# -*- coding: utf-8 -*- + +from flask.ext.rq import job from tweepy import OAuthHandler, API import bitlyapi import urllib2 @@ -5,6 +8,8 @@ import re from hasjob import app + +@job('hasjob') def tweet(title, url, location=None): auth = OAuthHandler(app.config['TWITTER_CONSUMER_KEY'], app.config['TWITTER_CONSUMER_SECRET']) auth.set_access_token(app.config['TWITTER_ACCESS_KEY'], app.config['TWITTER_ACCESS_SECRET']) diff --git a/hasjob/views/listing.py b/hasjob/views/listing.py index 5a826011e..31e5a53db 100644 --- a/hasjob/views/listing.py +++ b/hasjob/views/listing.py @@ -440,7 +440,7 @@ def confirm_email(hashid, key): db.session.commit() if app.config['TWITTER_ENABLED']: try: - tweet(post.headline, url_for('jobdetail', hashid=post.hashid, + tweet.delay(post.headline, url_for('jobdetail', hashid=post.hashid, _external=True), post.location) flash("Congratulations! Your job listing has been published and tweeted", "interactive") diff --git a/requirements.txt b/requirements.txt index e90e271ae..525d752ef 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ Flask-WTF Flask-Mail Flask-Assets Flask-Uploads +Flask-RQ Pillow pytz markdown diff --git a/rq.sh b/rq.sh new file mode 100755 index 000000000..8b2c62bb0 --- /dev/null +++ b/rq.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +rqworker -c rqdev hasjob diff --git a/rqdev.py b/rqdev.py new file mode 100644 index 000000000..5d9e969db --- /dev/null +++ b/rqdev.py @@ -0,0 +1,16 @@ +from urlparse import urlparse + +from hasjob import init_for, app + +init_for('dev') + +REDIS_URL = app.config.get('REDIS_URL', 'redis://localhost:6379/0') + +# REDIS_URL is not taken by setup_default_arguments function of rq/scripts/__init__.py +# so, parse it into pieces and give it + +r = urlparse(REDIS_URL) +REDIS_HOST = r.hostname +REDIS_PORT = r.port +REDIS_PASSWORD = r.password +REDIS_DB = 0 diff --git a/rqinit.py b/rqinit.py new file mode 100644 index 000000000..edc15498a --- /dev/null +++ b/rqinit.py @@ -0,0 +1,15 @@ +from urlparse import urlparse + +from hasjob import init_for, app + +init_for('production') +REDIS_URL = app.config.get('REDIS_URL', 'redis://localhost:6379/0') + +# REDIS_URL is not taken by setup_default_arguments function of rq/scripts/__init__.py +# so, parse that into pieces and give it + +r = urlparse(REDIS_URL) +REDIS_HOST = r.hostname +REDIS_PORT = r.port +REDIS_PASSWORD = r.password +REDIS_DB = 0