triggerdotdev / trigger.dev

Trigger.dev is the open source background jobs platform for TypeScript.

Home Page:https://trigger.dev/changelog

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

feat: support cloudfront timeouts

ryanleecode opened this issue · comments

Is your feature request related to a problem? Please describe.

A nextjs app hosted on cloudfront does work on with trigger.dev because it doesn't catch the 504 response codes.

https://open-next.js.org/

Describe the solution you'd like to see

diff --git a/apps/webapp/app/models/endpoint.server.ts b/apps/webapp/app/models/endpoint.server.ts
index e5c1c21..649f86a 100644
--- a/apps/webapp/app/models/endpoint.server.ts
+++ b/apps/webapp/app/models/endpoint.server.ts
@@ -27,6 +27,7 @@ export function detectResponseIsTimeout(rawBody: string, response?: Response) {
 
   return (
     isResponseVercelTimeout(response) ||
+    isResponseCloudfrontTimeout(response) ||
     isResponseDenoDeployTimeout(rawBody, response) ||
     isResponseCloudflareTimeout(rawBody, response)
   );
@@ -50,3 +51,7 @@ function isResponseVercelTimeout(response: Response) {
 function isResponseDenoDeployTimeout(rawBody: string, response: Response) {
   return response.status === 502 && rawBody.includes("TIME_LIMIT");
 }
+
+function isResponseCloudfrontTimeout(response: Response) {
+  return response.status === 504 && typeof response.headers.get("x-amz-cf-id") === "string";
+}

Describe alternate solutions

None

Additional information

Example Response from cloudfront

{
  "status": 504,
  "runId": "[REDACTED]",
  "endpoint": "https://[REDACTED]/api/trigger",
  "headers": {
    "connection": "keep-alive",
    "content-length": "1033",
    "content-type": "text/html",
    "date": "Wed, 12 Jun 2024 03:30:51 GMT",
    "server": "CloudFront",
    "via": "1.1 [REDACTED].cloudfront.net (CloudFront)",
    "x-amz-cf-id": "[REDCATED]",
    "x-amz-cf-pop": "[REDACTED]",
    "x-cache": "Error from cloudfront"
  },
  "rawBody": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<HTML><HEAD><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">\n<TITLE>ERROR: The request could not be satisfied</TITLE>\n</HEAD><BODY>\n<H1>504 ERROR</H1>\n<H2>The request could not be satisfied.</H2>\n<HR noshade size=\"1px\">\nCloudFront attempted to establish a connection with the origin, but either the attempt failed or the origin closed the connection.\nWe can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.\n<BR clear=\"all\">\nIf you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.\n<BR clear=\"all\">\n<HR noshade size=\"1px\">\n<PRE>\nGenerated by cloudfront (CloudFront)\nRequest ID: [REDACTED]\n</PRE>\n<ADDRESS>\n</ADDRESS>\n</BODY></HTML>",
  "timestamp": "2024-06-12T03:30:51.138Z",
  "name": "webapp",
  "message": "Endpoint responded with non-200 status code",
  "level": "debug"
}