R
Railway4w ago
Yash

Possible to restart a specific deployment via API?

Hi there, Is it possible to restart a specific deployment with an API call? This is to save memory and CPU and free those up when there is no volume of requests.
39 Replies
Percy
Percy4w ago
Project ID: N/A
Brody
Brody4w ago
does the cron template not fit your usecase for this?
Yash
Yash4w ago
It does, but it sometimes restarts even when there is an ongoing job, and that job is impacted want to restart when there is no ongoing job.
Brody
Brody4w ago
gotcha, do you know golang?
Yash
Yash4w ago
not right now, but i think i might be familiar
Brody
Brody4w ago
it's simple syntax, read the source code of the cron template, it's golang
Yash
Yash4w ago
ah okay, is there a repo where i can find this
Yash
Yash4w ago
GitHub
Build software better, together
GitHub is where people build software. More than 100 million people use GitHub to discover, fork, and contribute to over 420 million projects.
From An unknown user
From An unknown user
From An unknown user
Yash
Yash4w ago
const serviceInstanceRedeployMutation = mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) { serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId) } ; const getLatestDeploymentQuery = query Deployments($first: Int, $input: DeploymentListInput!) { deployments(input: $input, first: $first) { edges { node { id status } } } } ; could you please confirm if i'm running this correctly? i've noticed i can only restart the service once while i do get a 200, the services not really restart, is this something to do with deployment id? async function redeployService(serviceId: string): Promise<{ serviceId: string; status: string; error?: string }> { if (!RAILWAY_API_ENDPOINT) { throw new Error('RAILWAY_BACKEND_URL is not defined in the environment variables'); } try { const latestDeploymentId = await getLatestDeploymentID(serviceId); const response = await fetch(RAILWAY_API_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': Bearer ${process.env.RAILWAY_API_TOKEN}, }, body: JSON.stringify({ query: serviceInstanceRedeployMutation, variables: { environmentId: process.env.RAILWAY_ENVIRONMENT_ID, serviceId: serviceId, deploymentId: latestDeploymentId, }, }), }); if (!response.ok) { const errorText = await response.text(); throw new Error(HTTP error! status: ${response.status}, body: ${errorText}); } const data = await response.json(); if (data.errors) { throw new Error(JSON.stringify(data.errors)); } return { serviceId, status: 'redeployed' }; } catch (error: any) { console.error(Error redeploying service ${serviceId}:, error); return { serviceId, status: 'failed', error: error.message }; } }
Brody
Brody4w ago
you want to restart the service, not redeploy it
Yash
Yash4w ago
i can confirm i'm using correct details in the backend ah okay, so this one? mutation deploymentRestart($id: String!) { deploymentRestart(id: $id) }
Brody
Brody4w ago
yep
Yash
Yash4w ago
sigh, thanks man, i'll try with this
Brody
Brody4w ago
sounds good maybe at some point it will be worth it to invest some time into your code so you dont have to keep restarting it to save memory?
Want results from more Discord servers?
Add your server