Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 422253a

Browse files
Add a shutdown method
1 parent 47cbd54 commit 422253a

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
## [Unreleased] - no_due_date
22

3+
- **Add a `shutdown` method to fix this issue: https://github.com/googlemaps/google-maps-services-java/issues/261**
4+
5+
## [1.0.0] - 2019-11-21
6+
37
- **First version of JRuby Scala Distances**

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ Now you can call the method `getDrivingDistance` like this :
5151
-> (distance) { Success({ distance: distance.length.value, duration: distance.duration.toMinutes }) }
5252
)
5353
```
54+
55+
56+
Then don't forget to add an `at_exit` in your ruby code to cleanup all threads at exits :
57+
58+
```ruby
59+
at_exit do
60+
scalaDistanceApi.shutdown if scalaDistanceApi.present?
61+
end
62+
```

src/main/scala/com/colisweb/jrubyscaladistances/JRubyScalaDistance.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,29 @@ import com.colisweb.distances.providers.google.{
1010
}
1111
import com.colisweb.distances.{DistanceApi, TravelMode, Types, _}
1212
import com.google.maps.OkHttpRequestHandler
13-
import eu.timepit.refined.api.Refined
14-
import eu.timepit.refined.auto._
1513
import org.slf4j.LoggerFactory
1614

1715
import scala.concurrent.ExecutionContext
18-
import scala.concurrent.duration.{Duration, FiniteDuration}
1916
import scala.util.Try
2017

2118
final class JRubyScalaDistance(googleApiConfig: GoogleApiConfiguration, redisConfig: RedisConfiguration) {
2219

2320
implicit val contextShift: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
2421

22+
val logger = LoggerFactory.getLogger(classOf[OkHttpRequestHandler])
23+
val loggingF = (message: String) => logger.debug(message.replaceAll("key=([^&]*)&", "key=REDACTED&"))
24+
25+
val googleGeoApiContext = GoogleGeoApiContext(
26+
googleApiConfig.apiKey,
27+
googleApiConfig.connectTimeout,
28+
googleApiConfig.readTimeout,
29+
googleApiConfig.queryRateLimit,
30+
loggingF
31+
)
32+
2533
val distanceApi: DistanceApi[IO, GoogleDistanceProviderError] = {
26-
val logger = LoggerFactory.getLogger(classOf[OkHttpRequestHandler])
27-
val loggingF = (message: String) => logger.debug(message.replaceAll("key=([^&]*)&", "key=REDACTED&"))
2834

29-
val distanceProvider = GoogleDistanceProvider[IO](
30-
GoogleGeoApiContext(
31-
googleApiConfig.apiKey,
32-
googleApiConfig.connectTimeout,
33-
googleApiConfig.readTimeout,
34-
googleApiConfig.queryRateLimit,
35-
loggingF
36-
)
37-
)
35+
val distanceProvider = GoogleDistanceProvider[IO](googleGeoApiContext)
3836

3937
val cache = RedisCache[IO](
4038
caches.RedisConfiguration(redisConfig.host, redisConfig.port),
@@ -52,17 +50,18 @@ final class JRubyScalaDistance(googleApiConfig: GoogleApiConfiguration, redisCon
5250
origin: LatLong,
5351
destination: LatLong,
5452
travelMode: TravelMode
55-
): Try[Types.Distance] =
53+
): Try[Types.Distance] =
5654
distanceApi
5755
.distance(origin, destination, List(travelMode))
5856
.unsafeRunSync()
5957
.getOrElse(travelMode, Left(new RuntimeException("Unknown travelMode exception happened")))
6058
.toTry
6159

62-
6360
def getDrivingDistance(
6461
origin: LatLong,
6562
destination: LatLong
6663
): Try[Types.Distance] = getDistance(origin, destination, TravelMode.Driving)
6764

65+
def shutdown(): Unit = googleGeoApiContext.geoApiContext.shutdown()
66+
6867
}

0 commit comments

Comments
 (0)