-
Notifications
You must be signed in to change notification settings - Fork 846
/
HttpEventActor.scala
53 lines (42 loc) · 1.61 KB
/
HttpEventActor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mesosphere.marathon.event.http
import akka.actor._
import akka.pattern.ask
import mesosphere.marathon.api.v2.json.Formats._
import mesosphere.marathon.event._
import mesosphere.marathon.event.http.SubscribersKeeperActor.GetSubscribers
import spray.client.pipelining.{sendReceive, _}
import spray.http.{HttpRequest, HttpResponse}
import spray.httpx.PlayJsonSupport
import scala.concurrent.Future
import scala.util.{Failure, Success}
class HttpEventActor(val subscribersKeeper: ActorRef) extends Actor with ActorLogging with PlayJsonSupport {
implicit val ec = HttpEventModule.executionContext
implicit val timeout = HttpEventModule.timeout
val pipeline: HttpRequest => Future[HttpResponse] = (
addHeader("Accept", "application/json")
~> sendReceive)
def receive: Receive = {
case event: MarathonEvent =>
broadcast(event)
case _ =>
log.warning("Message not understood!")
}
def broadcast(event: MarathonEvent): Unit = {
log.info("POSTing to all endpoints.")
(subscribersKeeper ? GetSubscribers).mapTo[EventSubscribers].foreach {
_.urls.foreach { post(_, event) }
}
}
def post(urlString: String, event: MarathonEvent): Unit = {
log.info("Sending POST to:" + urlString)
val request = Post(urlString, eventToJson(event))
val response = pipeline(request)
response.onComplete {
case Success(res) =>
if (res.status.isFailure)
log.warning(s"Post {} to {} resulted in error response {}", event, urlString, res.status)
case Failure(t) =>
log.warning("Failed to post {} to {} reason: {}", event, urlString, t)
}
}
}