Skip to content

Instantly share code, notes, and snippets.

@hatdropper1977
Last active June 9, 2016 09:50
Show Gist options
  • Select an option

  • Save hatdropper1977/c8f7eb09683c18488e01b71c85c66762 to your computer and use it in GitHub Desktop.

Select an option

Save hatdropper1977/c8f7eb09683c18488e01b71c85c66762 to your computer and use it in GitHub Desktop.
A Celery worker node for HOWTO 5
# tasks.py
from boto.connection import AWSAuthConnection
import requests
from celery import Celery
# Be sure to add your SQS URL below!
application = Celery('tasks',broker='sqs://sqs.us-east-1.amazonaws.com/<your arn>/flask-es')
class ESConnection(AWSAuthConnection):
def __init__(self, region, **kwargs):
super(ESConnection, self).__init__(**kwargs)
self._set_auth_region_name(region)
self._set_auth_service_name("es")
def _required_auth_capability(self):
return ['hmac-v4']
client = ESConnection(
region='us-east-1',
host=' search-test-domain-ircp547akjoolsbp4ehu2a56u4.us-east-1.es.amazonaws.com',
is_secure=False)
@application.task
def get_location(user,address):
# Get the location from the API
r = requests.get('http://freegeoip.net/json/' + address)
jstr = str(r.json()['latitude']) + ',' + str(r.json()['longitude'])
# Update the ElasticSearch index
resp = client.make_request(method='POST',path='/big_survey/quiz/' + user + '/_update',data='{"doc":{"geo":"' + jstr + '"}}')
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment