Possible to restart a specific deployment via API?
whyashthakker
PROOP

2 years ago

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.

51 Replies

2 years ago

does the cron template not fit your usecase for this?


2 years ago

n/a


whyashthakker
PROOP

2 years ago

It does, but it sometimes restarts even when there is an ongoing job, and that job is impacted


whyashthakker
PROOP

2 years ago

want to restart when there is no ongoing job.


2 years ago

gotcha, do you know golang?


whyashthakker
PROOP

2 years ago

not right now, but i think i might be familiar


2 years ago

it's simple syntax, read the source code of the cron template, it's golang


whyashthakker
PROOP

2 years ago

ah okay, is there a repo where i can find this



2 years ago

yep! -


whyashthakker
PROOP

2 years 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


whyashthakker
PROOP

2 years ago

while i do get a 200, the services not really restart, is this something to do with deployment id?


whyashthakker
PROOP

2 years ago

async function redeployService(serviceId: string): Promise<{ serviceId: string; status: string; error?: string }> {
if (!RAILWAYAPIENDPOINT) {
throw new Error('RAILWAYBACKENDURL 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 };
}
}


2 years ago

you want to restart the service, not redeploy it


whyashthakker
PROOP

2 years ago

i can confirm i'm using correct details in the backend


whyashthakker
PROOP

2 years ago

ah okay, so this one?

mutation deploymentRestart($id: String!) {
deploymentRestart(id: $id)
}


2 years ago

yep


whyashthakker
PROOP

2 years ago

sigh, thanks man, i'll try with this


2 years ago

sounds good


2 years ago

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?


whyashthakker
PROOP

2 years ago

Yes, i've tried doing multiple things, but it's apparently an issue with a package i'm using


whyashthakker
PROOP

2 years ago

Also, same issue, it restarts the first time i run the code after making changes,

const getLatestDeploymentQuery = `
  query deployments($first: Int, $input: DeploymentListInput!) {
    deployments(first: $first, input: $input) {
      edges {
        node {
          id
          status
          createdAt
        }
      }
    }
  }
`;

const deploymentRestartMutation = `
  mutation deploymentRestart($id: String!) {
    deploymentRestart(id: $id)
  }
`;

i tried restarting again, but services don't restart

1276379507263012900


whyashthakker
PROOP

2 years ago

no errors, getting 200

1276379656202752000


whyashthakker
PROOP

2 years ago

there a limit on how many times i can restart a service per hour or maybe minutes?


2 years ago

no there is not


whyashthakker
PROOP

2 years ago

i guess there's something in the code then


whyashthakker
PROOP

2 years ago

i'll figure out, thanks for all the help!


2 years ago

no problem!


whyashthakker
PROOP

2 years ago

correctly gets response for deployments to restart.

but deployments not restarted

works fine when i call graphql api via postman. restarts fine as well

1276422369254051800
1276422369694716000


2 years ago

how are you sure you are getting the correct deployment Id


whyashthakker
PROOP

2 years ago

it would show an error if deployment ids are correct, i printed and manually verified these ids too


whyashthakker
PROOP

2 years ago

1276423357365092400
1276423357767880700


2 years ago

I just have the strangest feeling you are running redeploys for a service in some other project or environment


whyashthakker
PROOP

2 years ago

i have had that feeling too, but it looks like the ids are correct


whyashthakker
PROOP

2 years ago

i'm printing as i run those and verified with the ones i have in my dashboard


2 years ago

something is incorrect, otherwise it would work haha


whyashthakker
PROOP

2 years ago

next i'm fetching these dynamically


whyashthakker
PROOP

2 years ago

based on service ids


whyashthakker
PROOP

2 years ago

it would all fail if service ids were in correct


2 years ago

think about this logically, if my cron template can restart your service, that narrows it down to it being a code issue on your side


whyashthakker
PROOP

2 years ago

i'm guessing some sort of caching? i can tell you it does 100% what it's supposed to when i call it via Postman


2 years ago

wish I knew what the issue was though, sorry


2 years ago

railway doesn't cache API calls, this is a coding error as proven by my cron template working


whyashthakker
PROOP

2 years ago

;_;


2 years ago

I'm sorry I don't have an answer for you but this is 100% a code issue


whyashthakker
PROOP

2 years ago

yep pretty certain at this point, maybe nextjs caching


whyashthakker
PROOP

2 years ago

i'll verify


whyashthakker
PROOP

2 years ago

yep just cleared the .next folder and it worked lol

1276424494067617800


whyashthakker
PROOP

2 years ago

it's caching


whyashthakker
PROOP

2 years ago

fixed! thanks man, sorry for so many "and a lot of irrlevant" questions


2 years ago

awsome!


Loading...