<s> import time <EOL> from django . conf import settings <EOL> from redis import Redis <EOL> from rq import Queue <EOL> from . conf import get_redis_connection_params <EOL> redis_conn = Redis ( ** get_redis_connection_params ( ) ) <EOL> RALPH_EXTERNAL_SERVICES = getattr ( settings , '<STR_LIT>' , { } ) <EOL> class QueuedServiceError ( Exception ) : <EOL> pass <EOL> class ExternalService ( object ) : <EOL> def __init__ ( self , service_name ) : <EOL> """<STR_LIT>""" <EOL> service = RALPH_EXTERNAL_SERVICES . get ( service_name . upper ( ) ) <EOL> if not service : <EOL> raise ValueError ( '<STR_LIT>' . format ( service ) ) <EOL> self . method = service [ '<STR_LIT>' ] <EOL> self . queue = Queue ( <EOL> name = service [ '<STR_LIT:name>' ] , connection = redis_conn , async = True <EOL> ) <EOL> def run ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> job = self . queue . enqueue ( self . method , ** kwargs ) <EOL> if not job . is_queued : <EOL> raise QueuedServiceError <EOL> while job and not any ( [ job . is_finished , job . is_failed ] ) : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> return job . result </s>
