@@ -86,14 +86,22 @@ def load_files_from_gridfs(result_data_store: gridfs.GridFS, result: Dict, do_re
8686
8787
8888class MongoResultSerializer (ABC ):
89+ instance = None
90+
91+ def __new__ (cls , * args , ** kwargs ):
92+ if not isinstance (cls .instance , cls ):
93+ cls .instance = object .__new__ (cls )
94+ return cls .instance
95+
8996 # This class is the interface between Mongo and the rest of the application
9097 def __init__ (self , database_name = "notebooker" , mongo_host = "localhost" , result_collection_name = "NOTEBOOK_OUTPUT" ):
9198 self .database_name = database_name
9299 self .mongo_host = mongo_host
93100 self .result_collection_name = result_collection_name
94- mongo_connection = self .get_mongo_database ()
95- self .library = mongo_connection [result_collection_name ]
96- self .result_data_store = gridfs .GridFS (mongo_connection , "notebook_data" )
101+
102+ mongo_database = self .get_mongo_database ()
103+ self .library = mongo_database [result_collection_name ]
104+ self .result_data_store = gridfs .GridFS (mongo_database , "notebook_data" )
97105
98106 def __init_subclass__ (cls , cli_options : click .Command = None , ** kwargs ):
99107 if cli_options is None :
@@ -104,6 +112,16 @@ def __init_subclass__(cls, cli_options: click.Command = None, **kwargs):
104112 cls .cli_options = cli_options
105113 super ().__init_subclass__ (** kwargs )
106114
115+ def enable_sharding (self ):
116+ conn = self .get_mongo_connection ()
117+ try :
118+ conn .admin .command ("enableSharding" , self .database_name )
119+ conn .admin .command ({"shardCollection" : f"{ self .database_name } .notebook_data.chunks" ,
120+ "key" : {"files_id" : 1 , "n" : 1 }})
121+ logger .info (f"Successfully sharded GridFS collection for { self .database_name } " )
122+ except pymongo .errors .OperationFailure :
123+ logger .error (f"Could not shard { self .database_name } . Continuing." )
124+
107125 def serializer_args_to_cmdline_args (self ) -> List [str ]:
108126 args = []
109127 for cli_arg in self .cli_options .params :
0 commit comments