From b6adcc433e8738ad93effa4e30b9cda37f58a952 Mon Sep 17 00:00:00 2001 From: Jonathan Netley Date: Sun, 19 Apr 2020 22:18:44 +0200 Subject: [PATCH] database cleanup --- .../scala/Util/DataBuilder/Processing.scala | 38 ++++++++++--------- src/main/scala/Util/Database/Database.scala | 22 +++++++---- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/main/scala/Util/DataBuilder/Processing.scala b/src/main/scala/Util/DataBuilder/Processing.scala index e140eb5..3f45543 100644 --- a/src/main/scala/Util/DataBuilder/Processing.scala +++ b/src/main/scala/Util/DataBuilder/Processing.scala @@ -10,8 +10,6 @@ import org.bson.Document import scala.concurrent.Await import scala.concurrent.duration.Duration -import scala.reflect.io.File -import Util.Logging.log import Util.ErrorHandler._ import Util.Models.OrderedFile @@ -25,22 +23,26 @@ object Processing { */ def csvFiles(csvFiles: List[OrderedFile]) (implicit database: Database.type): Task[Either[Exception, Unit]] = { - Task.wander(csvFiles) { orderedFile => - println(s"Processing file ${orderedFile.index + 1} of ${csvFiles.length} file name: ${orderedFile.file.name}") - (for { - fileLines <- EitherT(FileHelper.extractCsvFileLines(orderedFile.file)) - headers = fileLines.headOption.map(_.split(',').toList) - lineItems = fileLines.drop(1) - collectionName = orderedFile.file.name.replace(".csv", "").toLowerCase - documentResult <- EitherT(buildMongoDocuments(headers, lineItems)) - db <- EitherT.right[Exception](database.getDatabase) - dbInsert <- EitherT.rightT[Task, Exception](db.getCollection[Document](collectionName).insertMany(documentResult)) - } yield { - println(s"Inserting into db: $dbInsert") - Await.result(dbInsert.toFuture(), Duration.Inf) - println(s"Done processing file ${orderedFile.index + 1}") - }).value - }.map { result => + val insertion = { + Task.wander(csvFiles) { orderedFile => + println(s"Processing file ${orderedFile.index + 1} of ${csvFiles.length} file name: ${orderedFile.file.name}") + (for { + fileLines <- EitherT(FileHelper.extractCsvFileLines(orderedFile.file)) + headers = fileLines.headOption.map(_.split(',').toList) + lineItems = fileLines.drop(1) + collectionName = orderedFile.file.name.replace(".csv", "").toLowerCase + documentResult <- EitherT(buildMongoDocuments(headers, lineItems)) + db <- EitherT(database.getDatabase) + dbInsert <- EitherT.rightT[Task, Exception](db.getCollection[Document](collectionName).insertMany(documentResult)) + } yield { + println(s"Inserting into db: $dbInsert") + Await.result(dbInsert.toFuture(), Duration.Inf) + println(s"Done processing file ${orderedFile.index + 1}") + }).value + } + } + + insertion.map { result => val (errors, _) = result.separate errors.headOption.map(error).toLeft(()) } diff --git a/src/main/scala/Util/Database/Database.scala b/src/main/scala/Util/Database/Database.scala index f2becbb..2b0559f 100644 --- a/src/main/scala/Util/Database/Database.scala +++ b/src/main/scala/Util/Database/Database.scala @@ -3,11 +3,14 @@ import com.mongodb.{MongoClientSettings, MongoCredential, ServerAddress} import monix.eval.Task import org.mongodb.scala.{MongoClient, MongoDatabase} import Util.ErrorHandler._ +import cats.implicits._ import scala.jdk.CollectionConverters._ case object Database { + private var mongoClient: Option[MongoClient] = None + /** builds mongo settings with auth * * @param authUser on the db @@ -54,20 +57,25 @@ case object Database { } } - private val databaseName: String = sys.env.get("mongo_db_name").fold(throw error("environment variables not set for db name"))(identity) - private val settings: MongoClientSettings.Builder = settingsBuilder().fold(throw error("environment variables not set for db"))(identity) - private val mongoClient: MongoClient = MongoClient(settings.build()) - private val database: MongoDatabase = mongoClient.getDatabase(databaseName) - /** Returns thee database instance * * @return */ - def getDatabase: Task[MongoDatabase] = Task(database) + def getDatabase: Task[Either[Exception, MongoDatabase]] = Task.eval { + for { + databaseName <- Either.fromOption(sys.env.get("mongo_db_name"), error("environment variables not set for db name")) + settings <- Either.fromOption(settingsBuilder(), error("environment variables not set for db")) + mongoClient = MongoClient(settings.build()).some + mClient <- Either.fromOption(mongoClient, error("mongo client could not be defined")) + } yield mClient.getDatabase(databaseName) + } /** Closes the mongo connection * */ - def close(): Unit = mongoClient.close() + def close(): Unit = mongoClient match { + case Some(db) => db.close() + case None => () + } }