From b6e538ba402787785cbd7d66c26ab9cc562bb37c Mon Sep 17 00:00:00 2001 From: Lukas Martinelli Date: Sat, 24 Oct 2015 17:07:19 +0200 Subject: [PATCH] Read jobs from sqs --- export/worker/export.py | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/export/worker/export.py b/export/worker/export.py index 992c10b..35fe8a9 100644 --- a/export/worker/export.py +++ b/export/worker/export.py @@ -17,8 +17,11 @@ Options: --tm2source= Directory of tm2source """ import subprocess +import os import os.path +import json +import boto.sqs from docopt import docopt @@ -58,6 +61,47 @@ def export_local(tilelive_command): cmd=tilelive_command) +def connect_job_queue(queue_name): + conn = boto.sqs.connect_to_region( + region_name=os.getenv('AWS_REGION', 'eu-central-1'), + aws_access_key_id=os.environ['AWS_ACCESS_KEY'], + aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'] + ) + queue = conn.get_queue(queue_name) + return queue + + +def export_remote(tm2source, sqs_queue, render_scheme): + timeout = int(os.getenv('JOB_TIMEOUT', 15 * 60)) + queue = connect_job_queue(sqs_queue) + + while True: + message = queue.read(visibility_timeout=timeout) + print(message) + if message: + body = json.loads(message.get_body()) + print(body) + + bounds = body['bounds'] + bbox = '{} {} {} {}'.format( + bounds[0][0], bounds[0][1], + bounds[1][0], bounds[1][1] + ) + mbtiles_file = '{}_{}.mbtiles'.format(body['x'], body['y']) + + tilelive_command = create_tilelive_command( + tm2source, + mbtiles_file, + bbox, + body['min_zoom'], + body['max_zoom'], + render_scheme + ) + export_local(tilelive_command) + print("Executed job and exportet to " + mbtiles_file) + queue.delete_message(message) + + if __name__ == '__main__': arguments = docopt(__doc__, version='0.1') print(arguments) @@ -71,3 +115,10 @@ if __name__ == '__main__': arguments['--render_scheme'] ) export_local(tilelive_command) + + if arguments.get('remote'): + export_remote( + arguments['--tm2source'], + arguments[''], + arguments['--render_scheme'] + )