Restart a service
mikecr123
PROOP

7 months ago

How can I restart a Railway service every 24 hours? Without using Docker, is there a way to make the "Restart" button only work every 24 hours?

$5 Bounty

8 Replies

eager4top
HOBBY

7 months ago

I think you will manually trigger the restart button as when you so desire to do so.


sim
FREE

7 months ago

What’s the reason for restarting it every 24 hours? Railway has a crons feature that could do the trick: https://docs.railway.com/reference/cron-jobs


thetaautoworld
HOBBY

7 months ago

the cron jobs does not allow to key in command (kill 1), so how do I schedule my service to restart every 24 hours ?


clashing
HOBBY

7 months ago

With the default settings, won't the service would run non-stop, with no need of manually scaling up the servers?


thetaautoworld
HOBBY

7 months ago

I need to restart my service for like every 24 hours, it consume a lot of memory, so I want to restart the service to free up the memory, how do I setup in cron jobs ? it doesn't allow command


clashing
HOBBY

7 months ago

Mike & Autoworld, you can do this to resolve your issue:

a. Create a simple Express server that uses Railway's Public API to restart a desired deployment.

b. Add that as a service in your Railway account, and schedule it as a CRON job to run every 12 hours (0 */12 * * *)

The express file should look like this:

import express from "express";

import cors from "cors";

import fetch from "node-fetch";

const app = express();

app.use(cors({ origin: "*" }));

const RAILWAY_API_URL = "https://backboard.railway.com/graphql/v2";

const RAILWAY_API_TOKEN = process.env.RAILWAY_API_TOKEN;

const DEPLOYMENT_ID = process.env.DEPLOYMENT_ID;

app.get("/restart", async (req, res) => {

if (!DEPLOYMENT_ID) {

return res.status(400).json({ error: "DEPLOYMENT_ID is not set" });

}

try {

const response = await fetch(RAILWAY_API_URL, {

method: "POST",

headers: {

"Content-Type": "application/json",

Authorization: Bearer ${RAILWAY_API_TOKEN},

},

body: JSON.stringify({

query: `

mutation {

deploymentRestart(id: "${DEPLOYMENT_ID}")

}

`,

}),

});

const data = await response.json();

res.json(data);

} catch (error) {

res.status(500).json({ error: error.message });

}

});

let port = process.env.PORT || 7890;

app.listen(port, () => {

console.log(`Server running on port ${port}`);

});

To obtain the deployment ID via the Railway dashboard, you can open the service for which you need to restart via the express server. Then, using the URL, you can retrieve the deployment ID. For instance, in my case, you can see that the deployment ID for the service named "htoi" is de***, which is right after the "service/" keyword in the URL

Note: The API token that you need for this to work is either a PERSONAL/TEAM token, which can only be generated by the paid-plan users (which you can).

Required resources link:

https://docs.railway.com/reference/public-api

https://railway.com/account/tokens

That is a great way to solve your issue, and I hope you can use that. Do mark it as the solution if it helped you smile emoji


clashing
HOBBY

6 months ago

Any update on this, mikecr123


clashing

Mike & Autoworld, you can do this to resolve your issue:a. Create a simple Express server that uses Railway's Public API to restart a desired deployment.b. Add that as a service in your Railway account, and schedule it as a CRON job to run every 12 hours (0 */12 * * *)The express file should look like this:import express from "express";import cors from "cors";import fetch from "node-fetch";const app = express();app.use(cors({ origin: "*" }));const RAILWAY_API_URL = "https://backboard.railway.com/graphql/v2";const RAILWAY_API_TOKEN = process.env.RAILWAY_API_TOKEN;const DEPLOYMENT_ID = process.env.DEPLOYMENT_ID;app.get("/restart", async (req, res) => {if (!DEPLOYMENT_ID) {return res.status(400).json({ error: "DEPLOYMENT_ID is not set" });}try {const response = await fetch(RAILWAY_API_URL, {method: "POST",headers: {"Content-Type": "application/json",Authorization: Bearer ${RAILWAY_API_TOKEN},},body: JSON.stringify({query: `mutation {deploymentRestart(id: "${DEPLOYMENT_ID}")}`,}),});const data = await response.json();res.json(data);} catch (error) {res.status(500).json({ error: error.message });}});let port = process.env.PORT || 7890;app.listen(port, () => {console.log(`Server running on port ${port}`);});To obtain the deployment ID via the Railway dashboard, you can open the service for which you need to restart via the express server. Then, using the URL, you can retrieve the deployment ID. For instance, in my case, you can see that the deployment ID for the service named "htoi" is de***, which is right after the "service/" keyword in the URLNote: The API token that you need for this to work is either a PERSONAL/TEAM token, which can only be generated by the paid-plan users (which you can).Required resources link:https://docs.railway.com/reference/public-apihttps://railway.com/account/tokensThat is a great way to solve your issue, and I hope you can use that. Do mark it as the solution if it helped you

clashing
HOBBY

6 months ago

Did that helped, mikecr123


Loading...