Last active
June 9, 2016 09:50
-
-
Save hatdropper1977/c8f7eb09683c18488e01b71c85c66762 to your computer and use it in GitHub Desktop.
A Celery worker node for HOWTO 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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