# Source Server Stats
# File: sourcestats/collector/__main__.py
# Desc: __main__ for the collector

from gevent import monkey
monkey.patch_all()

import sys
import logging

import gevent
from coloredlogs import ColoredStreamHandler

from .. import logger
from . import find, collect, index


if __name__ == '__main__':
    logging.getLogger().setLevel(logging.CRITICAL)
    log_level = logging.DEBUG if '--debug' in sys.argv else logging.INFO

    color_args = {
        'show_timestamps': False,
        'show_hostname': False
    }
    handler = ColoredStreamHandler(level=log_level, **color_args)
    logger.setLevel(log_level)
    logger.addHandler(handler)

    logger.info('Starting find, collect & index workers...')
    gevent.spawn(find)
    gevent.spawn(collect)
    gevent.spawn(index)

    try:
        gevent.wait()
    except KeyboardInterrupt:
        print 'Exiting upon user request...'
        raise SystemExit(0)

@@ -717,16 +717,39 @@
 rs...')%0A
+%0A    greenlets = %5B%0A    
     geve
@@ -762,17 +762,22 @@
 wn(find)
-%0A
+,%0A    
     geve
@@ -793,17 +793,22 @@
 collect)
+,
 %0A
+    
     geve
@@ -822,16 +822,22 @@
 n(index)
+%0A    %5D
 %0A%0A    tr
@@ -851,20 +851,437 @@
     
-gevent.wait(
+while True:%0A            # Get greenlets which have stopped (should be empty list)%0A            greenlet_states = %5Bgreenlet.ready() for greenlet in greenlets%5D%0A            greenlet_states = filter(lambda x: x, greenlet_states)%0A%0A            # If we have any, something broke!%0A            if len(greenlet_states) %3E 0:%0A                break%0A%0A            gevent.sleep(1)%0A%0A        logger.critical('One of the greenlets stopped, exiting!'
 )%0A  

Make the collector exit if one of it's greenlets fails.