mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 06:56:44 +00:00
Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
parent
858b8f5189
commit
a242da381a
1 changed files with 19 additions and 25 deletions
|
|
@ -137,44 +137,38 @@ export async function deployRailwayService(opts: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suspende ou retoma um serviço Railway usando `sleepApplication`.
|
||||||
|
* Railway não aceita `numReplicas: 0` via serviceInstanceUpdate — o caminho
|
||||||
|
* suportado para pausar é `sleepApplication: true` (e `false` para acordar).
|
||||||
|
* `replicas <= 0` => sleep, `>= 1` => wake.
|
||||||
|
*/
|
||||||
export async function setRailwayReplicas(opts: {
|
export async function setRailwayReplicas(opts: {
|
||||||
token: string;
|
token: string;
|
||||||
serviceId: string;
|
serviceId: string;
|
||||||
environmentId: string;
|
environmentId: string;
|
||||||
replicas: number;
|
replicas: number;
|
||||||
}): Promise<void> {
|
}): Promise<void> {
|
||||||
|
const sleep = opts.replicas <= 0;
|
||||||
const mutation = `
|
const mutation = `
|
||||||
mutation ServiceInstanceUpdate($serviceId: String!, $environmentId: String!, $input: ServiceInstanceUpdateInput!) {
|
mutation ServiceInstanceUpdate($serviceId: String!, $environmentId: String!, $input: ServiceInstanceUpdateInput!) {
|
||||||
serviceInstanceUpdate(serviceId: $serviceId, environmentId: $environmentId, input: $input)
|
serviceInstanceUpdate(serviceId: $serviceId, environmentId: $environmentId, input: $input)
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
// Tenta numReplicas primeiro (formato atual da Railway API v2)
|
const res = await railwayQuery(
|
||||||
const tryWithKey = async (key: "numReplicas" | "replicas") => {
|
mutation,
|
||||||
return await railwayQuery(
|
{
|
||||||
mutation,
|
serviceId: opts.serviceId,
|
||||||
{
|
environmentId: opts.environmentId,
|
||||||
serviceId: opts.serviceId,
|
input: { sleepApplication: sleep },
|
||||||
environmentId: opts.environmentId,
|
},
|
||||||
input: { [key]: opts.replicas },
|
opts.token,
|
||||||
},
|
);
|
||||||
opts.token,
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
let res = await tryWithKey("numReplicas");
|
|
||||||
if (res.errors?.length) {
|
if (res.errors?.length) {
|
||||||
const msg = JSON.stringify(res.errors);
|
throw new Error(`setRailwayReplicas (sleepApplication=${sleep}) failed: ${JSON.stringify(res.errors)}`);
|
||||||
// Fallback para `replicas` se o schema reclamar do campo
|
|
||||||
if (/numReplicas/i.test(msg) && /(unknown|not.*found|invalid)/i.test(msg)) {
|
|
||||||
console.warn("setRailwayReplicas: numReplicas rejected, retrying with `replicas`");
|
|
||||||
res = await tryWithKey("replicas");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (res.errors?.length) {
|
|
||||||
throw new Error(`setRailwayReplicas failed: ${JSON.stringify(res.errors)}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Após escalar, força redeploy para aplicar a mudança imediatamente
|
// Força redeploy para aplicar imediatamente o novo estado de sleep
|
||||||
try {
|
try {
|
||||||
await deployRailwayService({
|
await deployRailwayService({
|
||||||
token: opts.token,
|
token: opts.token,
|
||||||
|
|
@ -182,7 +176,7 @@ export async function setRailwayReplicas(opts: {
|
||||||
environmentId: opts.environmentId,
|
environmentId: opts.environmentId,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn("setRailwayReplicas: redeploy after scale failed (non-fatal):", e);
|
console.warn("setRailwayReplicas: redeploy after sleep change failed (non-fatal):", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue