<s> import os <EOL> import time <EOL> import StringIO <EOL> from redis . exceptions import ConnectionError <EOL> import redis <EOL> from wcloud . tasks . celery_app import celery_app <EOL> from wcloud . tasks . starter_tasks import start_redis <EOL> DEBUG = False <EOL> class AlreadyDeployedException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . msg = "<STR_LIT>" <EOL> def deploy_redis_instance ( redis_env_folder , port ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( redis_env_folder ) : <EOL> os . mkdir ( redis_env_folder ) <EOL> if not os . path . isdir ( redis_env_folder ) : <EOL> raise Exception ( "<STR_LIT>" % redis_env_folder ) <EOL> if not os . path . isdir ( redis_env_folder ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" % redis_env_folder ) <EOL> conf = StringIO . StringIO ( ) <EOL> conf . write ( "<STR_LIT>" % port ) <EOL> conf . write ( "<STR_LIT>" ) <EOL> conf . write ( "<STR_LIT>" % ( os . path . join ( redis_env_folder , "<STR_LIT>" % port ) ) ) <EOL> if DEBUG : <EOL> conf . write ( "<STR_LIT>" ) <EOL> config_file_name = "<STR_LIT>" % port <EOL> if os . path . exists ( os . path . join ( redis_env_folder , config_file_name ) ) : <EOL> raise AlreadyDeployedException ( ) <EOL> f = file ( os . path . join ( redis_env_folder , config_file_name ) , "<STR_LIT:w>" ) <EOL> f . write ( conf . getvalue ( ) ) <EOL> f . close ( ) <EOL> return True <EOL> def check_redis_deployment ( redis_env_folder , port ) : <EOL> """<STR_LIT>""" <EOL> started_by_us = False <EOL> try : <EOL> r = redis . StrictRedis ( host = "<STR_LIT:127.0.0.1>" , port = port , db = <NUM_LIT:0> ) <EOL> r . ping ( ) <EOL> except ConnectionError : <EOL> result = start_redis . delay ( redis_env_folder , "<STR_LIT>" % port ) <EOL> while not result . ready ( ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> started_by_us = True <EOL> r = redis . StrictRedis ( host = "<STR_LIT:127.0.0.1>" , port = port , db = <NUM_LIT:0> ) <EOL> r . ping ( ) <EOL> if started_by_us : <EOL> pass <EOL> return True </s>
