Ejemplo n.º 1
0
def serve():

    dse_username = os.getenv('KILLRVIDEO_DSE_USERNAME')
    dse_password = os.getenv('KILLRVIDEO_DSE_PASSWORD')
    dse_contact_points = os.getenv('KILLRVIDEO_DSE_CONTACT_POINTS', 'dse').split(',')
    service_port = os.getenv('KILLRVIDEO_SERVICE_PORT', '50101')

    file = open('config.json', 'r')
    config = json.load(file)

    default_consistency_level = config['DEFAULT_CONSISTENCY_LEVEL']

    # Initialize Cassandra Driver and Mapper
    load_balancing_policy = TokenAwarePolicy(DCAwareRoundRobinPolicy())
    profile = ExecutionProfile(consistency_level=ConsistencyLevel.name_to_value[default_consistency_level],
                               load_balancing_policy=load_balancing_policy)
    graph_profile = DseGraph.create_execution_profile('killrvideo_video_recommendations')

    auth_provider = None
    if dse_username:
        auth_provider = PlainTextAuthProvider(username=dse_username, password=dse_password)

    # Wait for Cassandra (DSE) to be up
    session = None
    while not session:
        try:
            session = Cluster(contact_points=dse_contact_points,
                              execution_profiles={EXEC_PROFILE_DEFAULT: profile, EXEC_PROFILE_GRAPH_DEFAULT: graph_profile},
                              auth_provider = auth_provider).connect("killrvideo")
        except (NoHostAvailable):
            logging.info('Waiting for Cassandra (DSE) to be available')
            time.sleep(10)

    # Additional retry loop to check if dummy keyspace exists
    while True:
        logging.info('Checking for schema to be created...')
        result = session.execute('SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name=\'kv_init_done\'')
        if result.one(): # any result indicates keyspace has been created
            break
        time.sleep(10)

    dse.cqlengine.connection.set_session(session)

    # Initialize GRPC Server
    grpc_server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    # Initialize Services (GRPC servicers with reference to GRPC Server and appropriate service reference
    CommentsServiceServicer(grpc_server, CommentsService(session=session))
    RatingsServiceServicer(grpc_server, RatingsService())
    SearchServiceServicer(grpc_server, SearchService(session=session))
    StatisticsServiceServicer(grpc_server, StatisticsService())
    SuggestedVideosServiceServicer(grpc_server, SuggestedVideosService(session=session))
    #UploadsServiceServicer(grpc_server, UploadsService())
    UserManagementServiceServicer(grpc_server, UserManagementService())
    VideoCatalogServiceServicer(grpc_server, VideoCatalogService(session=session))

    # Start GRPC Server
    grpc_server.add_insecure_port('[::]:' + service_port)
    grpc_server.start()

    # Keep application alive
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        grpc_server.stop(0)