mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 10:56:44 +00:00
Added service ID fallback
X-Lovable-Edit-ID: edt-a6eb016d-cb4b-4203-9617-fc18dfba438b Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
commit
3c1323bf19
2 changed files with 52 additions and 6 deletions
|
|
@ -229,6 +229,31 @@ export async function setHermesSuspended(opts: {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Busca o ID de um serviço pelo nome dentro de um projeto. Retorna null se não existir. */
|
||||||
|
export async function findRailwayServiceByName(opts: {
|
||||||
|
token: string;
|
||||||
|
projectId: string;
|
||||||
|
name: string;
|
||||||
|
}): Promise<string | null> {
|
||||||
|
const query = `
|
||||||
|
query Project($id: String!) {
|
||||||
|
project(id: $id) {
|
||||||
|
services { edges { node { id name } } }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const res = await railwayQuery<{
|
||||||
|
project: { services: { edges: { node: { id: string; name: string } }[] } };
|
||||||
|
}>(query, { id: opts.projectId }, opts.token);
|
||||||
|
if (res.errors?.length) {
|
||||||
|
console.error("findRailwayServiceByName errors:", JSON.stringify(res.errors));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const edges = res.data?.project?.services?.edges ?? [];
|
||||||
|
const match = edges.find((e) => e.node.name === opts.name);
|
||||||
|
return match?.node.id ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
/** Busca env+project IDs do primeiro deployment de um serviço. Útil quando vps_pool_id está null. */
|
/** Busca env+project IDs do primeiro deployment de um serviço. Útil quando vps_pool_id está null. */
|
||||||
export async function getServiceContext(opts: {
|
export async function getServiceContext(opts: {
|
||||||
token: string;
|
token: string;
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import {
|
||||||
deployRailwayService,
|
deployRailwayService,
|
||||||
deleteTelegramWebhook,
|
deleteTelegramWebhook,
|
||||||
getServiceContext,
|
getServiceContext,
|
||||||
|
findRailwayServiceByName,
|
||||||
upsertRailwayVariableCollection,
|
upsertRailwayVariableCollection,
|
||||||
} from "../_shared/railway.ts";
|
} from "../_shared/railway.ts";
|
||||||
|
|
||||||
|
|
@ -241,12 +242,32 @@ Deno.serve(async (req) => {
|
||||||
let railwayServiceId: string;
|
let railwayServiceId: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
railwayServiceId = await createRailwayService({
|
try {
|
||||||
token: RAILWAY_API_TOKEN,
|
railwayServiceId = await createRailwayService({
|
||||||
projectId: pool.railway_project_id,
|
token: RAILWAY_API_TOKEN,
|
||||||
name: serviceName,
|
projectId: pool.railway_project_id,
|
||||||
});
|
name: serviceName,
|
||||||
console.log(`[provision-agent] serviço criado: ${railwayServiceId}`);
|
});
|
||||||
|
console.log(`[provision-agent] serviço criado: ${railwayServiceId}`);
|
||||||
|
} catch (createErr) {
|
||||||
|
const msg = createErr instanceof Error ? createErr.message : String(createErr);
|
||||||
|
// Recover from "service already exists" — provavelmente sobra de attempt anterior
|
||||||
|
if (msg.includes("already exists")) {
|
||||||
|
console.warn(`[provision-agent] serviço já existe, tentando recuperar ID por nome: ${serviceName}`);
|
||||||
|
const existingId = await findRailwayServiceByName({
|
||||||
|
token: RAILWAY_API_TOKEN,
|
||||||
|
projectId: pool.railway_project_id,
|
||||||
|
name: serviceName,
|
||||||
|
});
|
||||||
|
if (!existingId) {
|
||||||
|
throw new Error(`Service "${serviceName}" exists but could not be located via API`);
|
||||||
|
}
|
||||||
|
railwayServiceId = existingId;
|
||||||
|
console.log(`[provision-agent] serviço existente recuperado: ${railwayServiceId}`);
|
||||||
|
} else {
|
||||||
|
throw createErr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await configureRailwayService({
|
await configureRailwayService({
|
||||||
token: RAILWAY_API_TOKEN,
|
token: RAILWAY_API_TOKEN,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue