From 8ff48a19789716482f9d1ab3827d999844282cde Mon Sep 17 00:00:00 2001 From: Richard van Heest Date: Tue, 7 Jan 2020 11:04:26 +0100 Subject: [PATCH] EASY-2475: configurable timeout for call to easy-deposit-agreement-generator (#65) --- .../assembly/dist/cfg/application.properties | 2 ++ .../easy/agreement/AgreementGenerator.scala | 21 ++++++++++++++----- .../dans/easy/agreement/Configuration.scala | 9 ++++++-- .../debug-config/application.properties | 2 ++ .../agreement/AgreementGeneratorSpec.scala | 3 ++- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/assembly/dist/cfg/application.properties b/src/main/assembly/dist/cfg/application.properties index 28fea82..a1b26b0 100644 --- a/src/main/assembly/dist/cfg/application.properties +++ b/src/main/assembly/dist/cfg/application.properties @@ -6,3 +6,5 @@ auth.ldap.user=cn=ldapadmin,dc=dans,dc=knaw,dc=nl auth.ldap.password=changeme daemon.http.port=20130 pdf-gen.url=http://localhost:20210/agreement +pdf-gen.conn-timeout-ms=5000 +pdf-gen.read-timeout-ms=5000 diff --git a/src/main/scala/nl/knaw/dans/easy/agreement/AgreementGenerator.scala b/src/main/scala/nl/knaw/dans/easy/agreement/AgreementGenerator.scala index 42085d1..03d41dc 100644 --- a/src/main/scala/nl/knaw/dans/easy/agreement/AgreementGenerator.scala +++ b/src/main/scala/nl/knaw/dans/easy/agreement/AgreementGenerator.scala @@ -17,6 +17,7 @@ package nl.knaw.dans.easy.agreement import java.net.URL +import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration import nl.knaw.dans.easy.agreement.datafetch.{ Dataset, EasyUser } import nl.knaw.dans.lib.logging.DebugEnhancedLogging import org.apache.commons.io.IOUtils @@ -29,7 +30,7 @@ import scalaj.http.{ BaseHttp, HttpResponse } import scala.io.Source import scala.util.{ Failure, Success, Try } -class AgreementGenerator(http: BaseHttp, url: URL) extends DebugEnhancedLogging { +class AgreementGenerator(http: BaseHttp, config: PdfGenConfiguration) extends DebugEnhancedLogging { private implicit val jsonFormats: Formats = DefaultFormats @@ -37,10 +38,14 @@ class AgreementGenerator(http: BaseHttp, url: URL) extends DebugEnhancedLogging val json = datasetToJSON(dataset, isSample) val jsonString = Serialization.write(json) debug(s"calling easy-deposit-agreement-generator with body: $jsonString") - val response = http(url.toString).postData(jsonString).header("content-type", "application/json").exec { - case (OK_200, _, is) => IOUtils.copyLarge(is, outputStreamProvider()) - case (_, _, is) => Source.fromInputStream(is).mkString - } + val response = http(config.url.toString) + .postData(jsonString) + .header("content-type", "application/json") + .timeout(config.connTimeout, config.readTimeout) + .exec { + case (OK_200, _, is) => IOUtils.copyLarge(is, outputStreamProvider()) + case (_, _, is) => Source.fromInputStream(is).mkString + } if (response.code == OK_200) Success(()) else Failure(GeneratorError(s"Could not generate agreement for dataset ${ dataset.datasetId }", HttpResponse(response.body.asInstanceOf[String], response.code, response.headers))) } @@ -69,3 +74,9 @@ class AgreementGenerator(http: BaseHttp, url: URL) extends DebugEnhancedLogging ("email" -> user.email) } } +object AgreementGenerator { + case class PdfGenConfiguration(url: URL, + connTimeout: Int, + readTimeout: Int, + ) +} diff --git a/src/main/scala/nl/knaw/dans/easy/agreement/Configuration.scala b/src/main/scala/nl/knaw/dans/easy/agreement/Configuration.scala index 156293b..61f1595 100644 --- a/src/main/scala/nl/knaw/dans/easy/agreement/Configuration.scala +++ b/src/main/scala/nl/knaw/dans/easy/agreement/Configuration.scala @@ -21,13 +21,14 @@ import better.files.File import better.files.File.root import com.yourmediashelf.fedora.client.{ FedoraClient, FedoraCredentials } import javax.naming.Context +import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration import org.apache.commons.configuration.PropertiesConfiguration case class Configuration(version: String, serverPort: Int, fedoraClient: FedoraClient, ldapEnv: LdapEnv, - pdfGenerator: URL, + pdfGenerator: PdfGenConfiguration, ) object Configuration { @@ -57,7 +58,11 @@ object Configuration { put(Context.SECURITY_CREDENTIALS, properties.getString("auth.ldap.password")) put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory") }, - pdfGenerator = new URL(properties.getString("pdf-gen.url")), + pdfGenerator = PdfGenConfiguration( + url = new URL(properties.getString("pdf-gen.url")), + connTimeout = properties.getInt("pdf-gen.conn-timeout-ms"), + readTimeout = properties.getInt("pdf-gen.read-timeout-ms"), + ) ) } } diff --git a/src/test/resources/debug-config/application.properties b/src/test/resources/debug-config/application.properties index ce81d44..56fe1ed 100644 --- a/src/test/resources/debug-config/application.properties +++ b/src/test/resources/debug-config/application.properties @@ -6,3 +6,5 @@ auth.ldap.user=cn=ldapadmin,dc=dans,dc=knaw,dc=nl auth.ldap.password=ldapadmin daemon.http.port=20130 pdf-gen.url=http://localhost:20210/agreement +pdf-gen.conn-timeout-ms=5000 +pdf-gen.read-timeout-ms=5000 diff --git a/src/test/scala/nl/knaw/dans/easy/agreement/AgreementGeneratorSpec.scala b/src/test/scala/nl/knaw/dans/easy/agreement/AgreementGeneratorSpec.scala index cc1b4e0..647eef8 100644 --- a/src/test/scala/nl/knaw/dans/easy/agreement/AgreementGeneratorSpec.scala +++ b/src/test/scala/nl/knaw/dans/easy/agreement/AgreementGeneratorSpec.scala @@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream import java.nio.charset.StandardCharsets import nl.knaw.dans.common.lang.dataset.AccessCategory +import nl.knaw.dans.easy.agreement.AgreementGenerator.PdfGenConfiguration import nl.knaw.dans.easy.agreement.datafetch.{ Dataset, EasyUser } import nl.knaw.dans.easy.agreement.fixture.{ FixedDateTime, TestSupportFixture } import nl.knaw.dans.pf.language.emd.Term.Name @@ -55,7 +56,7 @@ class AgreementGeneratorSpec extends TestSupportFixture with FixedDateTime with private val date = mock[EmdDate] private val rights = mock[EmdRights] - private val generator = new AgreementGenerator(Http, baseURL.url()) + private val generator = new AgreementGenerator(Http, PdfGenConfiguration(baseURL.url(), 5000, 5000)) override protected def afterAll(): Unit = { server.shutdown()