Skip to content

Commit

Permalink
Merge pull request #2591 from ujh/add-retries
Browse files Browse the repository at this point in the history
Add retries for JavaScript requests
  • Loading branch information
ujh authored Dec 27, 2024
2 parents a2d2c6c + 6c93dd7 commit cc0dc3b
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/BotSignUps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const BotSignUps = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/bot-signups.json")
getRequest("/admins/graphs/bot-signups.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/CollectedInks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const CollectedInks = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/collected-inks.json")
getRequest("/admins/graphs/collected-inks.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/CollectedPens.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const CollectedPens = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/collected-pens.json")
getRequest("/admins/graphs/collected-pens.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/CurrentlyInked.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const CurrentlyInked = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/currently-inked.json")
getRequest("/admins/graphs/currently-inked.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/SignUps.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const SignUps = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/signups.json")
getRequest("/admins/graphs/signups.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/Spam.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const Spam = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/spam.json")
getRequest("/admins/graphs/spam.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/src/admin/graphs/UsageRecords.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

import { Spinner } from "../components/Spinner";
import { getRequest } from "../../fetch";

export const UsageRecords = () => {
const [data, setData] = useState(null);
useEffect(() => {
fetch("/admins/graphs/usage-records.json")
getRequest("/admins/graphs/usage-records.json")
.then((res) => res.json())
.then((json) => setData(json));
}, []);
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/src/admin/stats.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { getRequest } from "../fetch";

document.addEventListener("DOMContentLoaded", () => {
const elements = document.querySelectorAll(".stats");
Expand Down Expand Up @@ -31,7 +32,7 @@ const Stat = ({ id, arg }) => {
async function load() {
let url = `/admins/stats/${id}`;
if (arg) url += `?arg=${arg}`;
const response = await fetch(url);
const response = await getRequest(url);
const json = await response.json();
setData(json);
setLoading(false);
Expand All @@ -57,7 +58,7 @@ const ConditionalStat = ({ id, arg, href, template }) => {
async function load() {
let url = `/admins/stats/${id}`;
if (arg) url += `?arg=${arg}`;
const response = await fetch(url);
const response = await getRequest(url);
const json = await response.json();
setData(json);
setLoading(false);
Expand Down
12 changes: 11 additions & 1 deletion app/javascript/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export function putRequest(path, body) {
}

function request(path, method, body) {
return fetch(path, {
return req(path, method, body);
}

async function req(path, method, body, retries = 3) {
const response = await fetch(path, {
credentials: "same-origin",
method: method,
body: JSON.stringify(body),
Expand All @@ -27,6 +31,12 @@ function request(path, method, body) {
"X-CSRF-Token": csrfToken()
}
});
if (method === "GET" && response.status >= 500 && retries > 0) {
console.log("Retrying", path, method, body, retries);
return req(path, method, body, retries - 1);
} else {
return response;
}
}

const csrfToken = () => {
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/sentry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
config.dsn = ENV["SENTRY_DSN"]
config.enable_tracing = true
config.enabled_patches << :faraday

config.excluded_exceptions +=
YAML.load_file(Rails.root.join("config", "honeybadger.yml")).dig(
"exceptions",
"ignore"
)
end
6 changes: 3 additions & 3 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ NEW_RELIC_APPLICATION_LOGGING_ENABLED = "false"
NEW_RELIC_BROWSER_MONITORING_AUTO_INSTRUMENT = "false"
NEW_RELIC_LOG = "stdout"
RACK_ENV = "production"
RACK_TIMEOUT_SERVICE_TIMEOUT = "60"
RACK_TIMEOUT_WAIT_TIMEOUT = "0" # Disable the wait timeout
RACK_TIMEOUT_SERVICE_TIMEOUT = "25"
RACK_TIMEOUT_WAIT_TIMEOUT = "30"
RAILS_ENV = "production"
RAILS_LOG_TO_STDOUT = "enabled"
RAILS_MAX_THREADS = "5"
Expand All @@ -40,7 +40,7 @@ processes = ["web"]
force_https = true
auto_stop_machines = "suspend"
auto_start_machines = true
min_machines_running = 3
min_machines_running = 2
[http_service.concurrency]
type = "requests"
soft_limit = 4
Expand Down

0 comments on commit cc0dc3b

Please sign in to comment.