Skip to content

Commit

Permalink
Handle rendering requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
asciipip committed Jun 7, 2014
1 parent dad9123 commit ba1dfe3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
16 changes: 15 additions & 1 deletion MESSAGE_PROTOCOL
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Command:

{ "command": <command-name>, <parameters> ... }


Normal rendering sequence:

queuemaster -> all renderers
Expand Down Expand Up @@ -51,6 +52,19 @@ renderer -> queuemaster
All renderer -> queuemaster communication should have the renderer's queue
as the "reply_to" property.


Normal render request sequence:

requester -> queuemaster

{ "command": "render",
"tile": "z/x/y" }

queuemaster -> requester

{ "command": "rendered" }


The queuemaster also understands:

{ "command": "stats" }
{ "command": "stats" }
51 changes: 40 additions & 11 deletions queuemaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ def idle(self):
@property
def status(self):
if self.idle:
base = 'idle'
else:
base = 'rendering: %s' % self.working_on
return 'idle'
base = 'rendering: %s' % self.working_on
if time.time() - self.last_activity > RENDERER_STALE_TIME:
stale = ' (STALE %ds)' % (time.time() - self.last_activity)
else:
Expand Down Expand Up @@ -102,7 +101,13 @@ def __init__(self, maxz):
def queue_metatile(self, mt, queue, seen, source):
added = False
with self.lock:
if seen:
if seen == None:
# This is a regular, by-zoom queue.
if not mt in self.queued_metatiles:
queue.append(mt)
self.queued_metatiles[mt] = 1
added = True
else:
# This is one of the specialty queues.
if not mt in seen:
queue.append(mt)
Expand All @@ -112,12 +117,6 @@ def queue_metatile(self, mt, queue, seen, source):
else:
self.queued_metatiles[mt] = 1
added = True
else:
# This is a regular, by-zoom queue.
if not mt in self.queued_metatiles:
self.queued_metatiles[mt] = 1
queue.append(mt)
added = True
if added:
if source:
log_message('queue from %s: %s' % (source, mt))
Expand All @@ -129,7 +128,7 @@ def queue_tile(self, z, x, y, queue='zoom', source=None):
if queue == 'missing':
self.queue_metatile(mt, self.missing_queue, self.missing_seen, source)
elif queue == 'important':
self.queue_metatile(mt, self.important_queue, self.important_seen, source)
self.queue_metatile(mt, self.important_stack, self.important_seen, source)
elif queue == 'zoom':
self.queue_metatile(mt, self.zoom_queues[z], None, source)

Expand Down Expand Up @@ -284,6 +283,7 @@ def __init__(self, maxz):
self.expirer = TileExpirer(self.maxz, self.queue)
self.expirer.start()
self.renderers = {}
self.requests = {}

### Startup sequence.

Expand Down Expand Up @@ -350,6 +350,7 @@ def on_command(self, chan, method, props, body):
if props.reply_to in self.renderers:
self.remove_renderer(props.reply_to)
elif command == 'rendered':
self.send_render_replies(message['metatile'])
if props.reply_to in self.renderers:
self.renderers[props.reply_to].finished(message['metatile'])
self.send_render_requests()
Expand All @@ -361,6 +362,8 @@ def on_command(self, chan, method, props, body):
correlation_id=props.correlation_id,
content_type='application/json'),
body=self.get_stats())
elif command == 'render':
self.handle_render_request(message['tile'], props)
else:
log_message('unknown message: %s' % body)
except ValueError:
Expand All @@ -382,6 +385,32 @@ def send_render_requests(self):
for renderer in self.renderers.values():
renderer.send_request()

def send_render_replies(self, mt):
if mt in self.requests:
for props in self.requests[mt]:
self.channel.basic_publish(
exchange='',
routing_key=props.reply_to,
properties=pika.BasicProperties(
correlation_id=props.correlation_id,
content_type='application/json'),
body=json.dumps({'command': 'rendered'}))
del self.requests[mt]

def handle_render_request(self, tile, props):
z, x, y = [ int(i) for i in tile.split('/') ]
mt = '%s/%s/%s' % (z, x / NTILES[z], y / NTILES[z])
if mt in self.requests:
self.requests[mt].add(props)
else:
self.requests[mt] = [props]
if tileExists(REFERENCE_TILESET, z, x, y):
importance = 'important'
else:
importance = 'missing'
self.queue.queue_tile(z, x, y, importance, 'request')



if __name__ == "__main__":
qm = Queuemaster(16)
Expand Down

0 comments on commit ba1dfe3

Please sign in to comment.