-
Notifications
You must be signed in to change notification settings - Fork 555
/
DotcomRenderingService.scala
442 lines (393 loc) · 15.4 KB
/
DotcomRenderingService.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package renderers
import org.apache.pekko.actor.{ActorSystem => PekkoActorSystem}
import com.gu.contentapi.client.model.v1.{Block, Blocks, Content, Crossword}
import common.{DCRMetrics, GuLogging}
import concurrent.CircuitBreakerRegistry
import conf.Configuration
import conf.switches.Switches.CircuitBreakerDcrSwitch
import crosswords.CrosswordPageWithContent
import http.{HttpPreconnections, ResultWithPreconnectPreload}
import model.Cached.{RevalidatableResult, WithoutRevalidationResult}
import model.dotcomrendering._
import model.dotcomrendering.pageElements.EditionsCrosswordRenderingDataModel
import model.{
CacheTime,
Cached,
GalleryPage,
ImageContentPage,
InteractivePage,
LiveBlogPage,
MediaPage,
NoCache,
PageWithStoryPackage,
PressedPage,
RelatedContentItem,
SimplePage,
}
import play.api.libs.json.JsValue
import play.api.libs.ws.{WSClient, WSResponse}
import play.api.mvc.Results.{InternalServerError, NotFound}
import play.api.mvc.{RequestHeader, Result}
import play.twirl.api.Html
import services.newsletters.model.NewsletterResponseV2
import services.{IndexPage, NewsletterData}
import java.lang.System.currentTimeMillis
import java.net.ConnectException
import java.util.concurrent.TimeoutException
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.concurrent.duration._
// Introduced as CAPI error handling elsewhere would smother these otherwise
case class DCRLocalConnectException(message: String) extends ConnectException(message)
case class DCRTimeoutException(message: String) extends TimeoutException(message)
case class DCRRenderingException(message: String) extends IllegalStateException(message)
class DotcomRenderingService extends GuLogging with ResultWithPreconnectPreload {
private[this] val circuitBreaker = CircuitBreakerRegistry.withConfig(
name = "dotcom-rendering-client",
system = PekkoActorSystem("dotcom-rendering-client-circuit-breaker"),
maxFailures = Configuration.rendering.circuitBreakerMaxFailures,
callTimeout = Configuration.rendering.timeout.plus(200.millis),
resetTimeout = Configuration.rendering.timeout * 4,
)
private[this] def postWithoutHandler(
ws: WSClient,
payload: JsValue,
endpoint: String,
timeout: Duration = Configuration.rendering.timeout,
)(implicit request: RequestHeader): Future[WSResponse] = {
val start = currentTimeMillis()
val resp = ws
.url(endpoint)
.withRequestTimeout(timeout)
.addHttpHeaders("Content-Type" -> "application/json")
.post(payload)
resp.foreach(_ => {
DCRMetrics.DCRLatencyMetric.recordDuration(currentTimeMillis() - start)
DCRMetrics.DCRRequestCountMetric.increment()
})
resp.recoverWith({
case _: ConnectException if Configuration.environment.stage == "DEV" =>
val msg = s"""Connection refused to ${endpoint}.
|
|You are trying to access a Dotcom Rendering page via Frontend but it
|doesn't look like DCR is running locally on the expected port (3030).
|
|Note, for most use cases, we recommend developing directly on DCR.
|
|To get started with dotcom-rendering, see:
|
| https://github.com/guardian/dotcom-rendering""".stripMargin
Future.failed(DCRLocalConnectException(msg))
case t: TimeoutException => Future.failed(DCRTimeoutException(t.getMessage))
})
}
private[this] def post(
ws: WSClient,
payload: JsValue,
endpoint: String,
cacheTime: CacheTime,
timeout: Duration = Configuration.rendering.timeout,
)(implicit request: RequestHeader): Future[Result] = {
def handler(response: WSResponse): Result = {
response.status match {
case 200 =>
val cachedRequest = Cached(cacheTime)(RevalidatableResult.Ok(Html(response.body)))
.withHeaders("X-GU-Dotcomponents" -> "true")
response.header("Link") match {
case Some(linkValue) =>
cachedRequest
// Send both the prefetch header for offline reading, and the usual preconnect URLs
.withHeaders("Link" -> linkValue)
.withPreconnect(HttpPreconnections.defaultUrls)
// For any other requests, we return just the default link header with preconnect urls
case _ => cachedRequest.withPreconnect(HttpPreconnections.defaultUrls)
}
case 400 =>
// if DCR returns a 400 it's because *we* failed, so frontend should return a 500
NoCache(InternalServerError("Remote renderer validation error (400)"))
.withHeaders("X-GU-Dotcomponents" -> "true")
case 415 =>
// if DCR returns a 415 it's because we can't render a specific component, so page is not available
Cached(CacheTime.NotFound)(WithoutRevalidationResult(NotFound))
.withHeaders("X-GU-Dotcomponents" -> "true")
case _ =>
log.error(s"Request to DCR failed: status ${response.status}, path: ${request.path}, body: ${response.body}")
NoCache(
InternalServerError("Remote renderer error (500)")
.withHeaders("X-GU-Dotcomponents" -> "true"),
)
}
}
if (CircuitBreakerDcrSwitch.isSwitchedOn) {
circuitBreaker.withCircuitBreaker(postWithoutHandler(ws, payload, endpoint, timeout)).map(handler)
} else {
postWithoutHandler(ws, payload, endpoint, timeout).map(handler)
}
}
def getAMPArticle(
ws: WSClient,
page: PageWithStoryPackage,
blocks: Blocks,
pageType: PageType,
newsletter: Option[NewsletterData],
filterKeyEvents: Boolean = false,
)(implicit request: RequestHeader): Future[Result] =
baseArticleRequest("/AMPArticle", ws, page, blocks, pageType, filterKeyEvents, false, newsletter)
def getAppsArticle(
ws: WSClient,
page: PageWithStoryPackage,
blocks: Blocks,
pageType: PageType,
newsletter: Option[NewsletterData],
filterKeyEvents: Boolean = false,
forceLive: Boolean = false,
)(implicit request: RequestHeader): Future[Result] =
baseArticleRequest(
"/AppsArticle",
ws,
page,
blocks,
pageType,
filterKeyEvents,
forceLive,
newsletter,
)
def getArticle(
ws: WSClient,
page: PageWithStoryPackage,
blocks: Blocks,
pageType: PageType,
newsletter: Option[NewsletterData],
filterKeyEvents: Boolean = false,
forceLive: Boolean = false,
)(implicit request: RequestHeader): Future[Result] =
baseArticleRequest(
"/Article",
ws,
page,
blocks,
pageType,
filterKeyEvents,
forceLive,
newsletter,
)
private def baseArticleRequest(
path: String,
ws: WSClient,
page: PageWithStoryPackage,
blocks: Blocks,
pageType: PageType,
filterKeyEvents: Boolean,
forceLive: Boolean = false,
newsletter: Option[NewsletterData],
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = page match {
case liveblog: LiveBlogPage =>
DotcomRenderingDataModel.forLiveblog(
liveblog,
blocks,
request,
pageType,
filterKeyEvents,
forceLive,
newsletter,
)
case _ => DotcomRenderingDataModel.forArticle(page, blocks, request, pageType, newsletter)
}
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + path, page.metadata.cacheTime)
}
def getBlocks(
ws: WSClient,
page: LiveBlogPage,
blocks: Seq[Block],
)(implicit request: RequestHeader): Future[String] = {
val dataModel = DotcomBlocksRenderingDataModel(page, request, blocks)
val json = DotcomBlocksRenderingDataModel.toJson(dataModel)
postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/Blocks")
.flatMap(response => {
if (response.status == 200)
Future.successful(response.body)
else
Future.failed(
DCRRenderingException(
s"getBlocks request to DCR failed: status ${response.status}, path: ${request.path}, body: ${response.body}",
),
)
})
}
def getAppsBlocks(
ws: WSClient,
page: LiveBlogPage,
blocks: Seq[Block],
)(implicit request: RequestHeader): Future[String] = {
val dataModel = DotcomBlocksRenderingDataModel(page, request, blocks)
val json = DotcomBlocksRenderingDataModel.toJson(dataModel)
postWithoutHandler(ws, json, Configuration.rendering.articleBaseURL + "/AppsBlocks")
.flatMap(response => {
if (response.status == 200)
Future.successful(response.body)
else
Future.failed(
DCRRenderingException(
s"getBlocks request to DCR failed: status ${response.status}, path: ${request.path}, body: ${response.body}",
),
)
})
}
private def getTimeout: Duration = {
if (Configuration.environment.stage == "DEV")
Configuration.rendering.timeout * 5
else
Configuration.rendering.timeout
}
def getFront(
ws: WSClient,
page: PressedPage,
pageType: PageType,
mostViewed: Seq[RelatedContentItem],
mostCommented: Option[Content],
mostShared: Option[Content],
deeplyRead: Option[Seq[Trail]],
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomFrontsRenderingDataModel(
page,
request,
pageType,
mostViewed,
mostCommented,
mostShared,
deeplyRead,
)
val json = DotcomFrontsRenderingDataModel.toJson(dataModel)
val timeout = getTimeout
post(ws, json, Configuration.rendering.faciaBaseURL + "/Front", CacheTime.Facia, timeout)
}
def getTagPage(
ws: WSClient,
page: IndexPage,
pageType: PageType,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomTagPagesRenderingDataModel(
page,
request,
pageType,
)
val json = DotcomTagPagesRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.tagPageBaseURL + "/TagPage", CacheTime.Facia)
}
def getInteractive(
ws: WSClient,
page: InteractivePage,
blocks: Blocks,
pageType: PageType,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forInteractive(page, blocks, request, pageType)
val json = DotcomRenderingDataModel.toJson(dataModel)
// Nb. interactives have a longer timeout because some of them are very
// large unfortunately. E.g.
// https://www.theguardian.com/education/ng-interactive/2018/may/29/university-guide-2019-league-table-for-computer-science-information.
post(ws, json, Configuration.rendering.interactiveBaseURL + "/Interactive", page.metadata.cacheTime, 4.seconds)
}
def getAMPInteractive(
ws: WSClient,
page: InteractivePage,
blocks: Blocks,
pageType: PageType,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forInteractive(page, blocks, request, pageType)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.interactiveBaseURL + "/AMPInteractive", page.metadata.cacheTime)
}
def getAppsInteractive(
ws: WSClient,
page: InteractivePage,
blocks: Blocks,
pageType: PageType,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forInteractive(page, blocks, request, pageType)
val json = DotcomRenderingDataModel.toJson(dataModel)
// Nb. interactives have a longer timeout because some of them are very
// large unfortunately. E.g.
// https://www.theguardian.com/education/ng-interactive/2018/may/29/university-guide-2019-league-table-for-computer-science-information.
post(ws, json, Configuration.rendering.interactiveBaseURL + "/AppsInteractive", page.metadata.cacheTime, 4.seconds)
}
def getEmailNewsletters(
ws: WSClient,
newsletters: List[NewsletterResponseV2],
page: SimplePage,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomNewslettersPageRenderingDataModel.apply(page, newsletters, request)
val json = DotcomNewslettersPageRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.faciaBaseURL + "/EmailNewsletters", CacheTime.Facia)
}
def getImageContent(
ws: WSClient,
imageContent: ImageContentPage,
pageType: PageType,
mainBlock: Option[Block],
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forImageContent(imageContent, request, pageType, mainBlock)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/Article", CacheTime.Facia)
}
def getAppsImageContent(
ws: WSClient,
imageContent: ImageContentPage,
pageType: PageType,
mainBlock: Option[Block],
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forImageContent(imageContent, request, pageType, mainBlock)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/AppsArticle", CacheTime.Facia)
}
def getMedia(
ws: WSClient,
mediaPage: MediaPage,
pageType: PageType,
blocks: Blocks,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forMedia(mediaPage, request, pageType, blocks)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/Article", CacheTime.Facia)
}
def getAppsMedia(
ws: WSClient,
mediaPage: MediaPage,
pageType: PageType,
blocks: Blocks,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forMedia(mediaPage, request, pageType, blocks)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/AppsArticle", CacheTime.Facia)
}
def getGallery(
ws: WSClient,
gallery: GalleryPage,
pageType: PageType,
blocks: Blocks,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forGallery(gallery, request, pageType, blocks)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/Article", CacheTime.Facia)
}
def getCrossword(
ws: WSClient,
crosswordPage: CrosswordPageWithContent,
pageType: PageType,
)(implicit request: RequestHeader): Future[Result] = {
val dataModel = DotcomRenderingDataModel.forCrossword(crosswordPage, request, pageType)
val json = DotcomRenderingDataModel.toJson(dataModel)
post(ws, json, Configuration.rendering.articleBaseURL + "/Article", CacheTime.Facia)
}
def getEditionsCrossword(
ws: WSClient,
crosswords: EditionsCrosswordRenderingDataModel,
)(implicit request: RequestHeader): Future[Result] = {
val json = EditionsCrosswordRenderingDataModel.toJson(crosswords)
post(ws, json, Configuration.rendering.articleBaseURL + "/EditionsCrossword", CacheTime.Default)
}
}
object DotcomRenderingService {
def apply(): DotcomRenderingService = new DotcomRenderingService()
}