Skip to content
Snippets Groups Projects
0-reset-session.py 2.25 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    import pika
    from utils.my_logging import logging
    import os, glob
    from pymongo import MongoClient
    
    if __name__ == '__main__':
    
        import yaml
    
        with open("config.yaml", 'r') as yamlfile:
            cfg = yaml.load(yamlfile)
    
        #print(cfg['rabbitmq'])
    
        queues_to_delete = []
    
        for key in cfg['rabbitmq'].keys():
            # print(key)
            if 'queue_name' in key:
                queues_to_delete.append( cfg['session']['id'] + '_' + cfg['rabbitmq'][key] )
    
        logging.debug(queues_to_delete)
    
    
        # exit(1)
    
        connection = pika.BlockingConnection(pika.ConnectionParameters(host=cfg['rabbitmq']['host']))
        channel = connection.channel()
    
        for queue in queues_to_delete:
            try:
                channel.queue_delete(queue=queue)
                logging.info('The queue named "%s" was deleted.' % queue)
            except:
                pass
    
        connection.close()
    
        # delete this session files in the working_directory
        for filename in glob.glob("%s/%s*" % (cfg['session']['working_directory'], cfg['session']['id'])):
    
            if 'json' not in filename:
                os.remove(filename)
    
    Alessandro Cerioni's avatar
    Alessandro Cerioni committed
    
        # delete MongoDB collections/records related to this session
    
        the_session_id = cfg['session']['id']
    
        mongo_client = MongoClient('mongodb://%s:%s@%s:%s/' % (cfg['mongo']['username'],
                                                               cfg['mongo']['password'],
                                                               cfg['mongo']['host'],
                                                               cfg['mongo']['port']))
    
        mongo_db = mongo_client[cfg['mongo']['data-db']]
    
        #collection = mongo_db['indexing-sessions']
        collection_name = 'indexing-session-' + the_session_id
        collection = mongo_db[ collection_name ]
        mongo_db[ 'indexing-session-' + the_session_id ]
        collection.drop()
        logging.info('The collection named "%s" was deleted.' % collection_name)
    
        mongo_db = mongo_client[cfg['mongo']['report-db']]
        collection = mongo_db['indexing-sessions']
        collection.delete_one({'_id': the_session_id})
        logging.info('The doc having "%s" as id was deleted from the reporting collection.' % the_session_id)
    
        # doc = dict()
        # doc['_id'] = cfg['session']['id']
        # doc['started_at'] = datetime.datetime.utcnow()
        # doc['configuration'] = cfg