|
| 1 | +import type * as schema from "@ctrlplane/db/schema"; |
| 2 | +import type { Span } from "@ctrlplane/logger"; |
| 3 | +import type { WorkspaceEngine } from "@ctrlplane/workspace-engine-sdk"; |
| 4 | + |
| 5 | +import type { GoEventPayload, GoMessage } from "../events.js"; |
| 6 | +import { createSpanWrapper } from "../../span.js"; |
| 7 | +import { sendGoEvent } from "../client.js"; |
| 8 | +import { Event } from "../events.js"; |
| 9 | + |
| 10 | +const getOapiJobAgent = ( |
| 11 | + jobAgent: schema.JobAgent, |
| 12 | +): WorkspaceEngine["schemas"]["JobAgent"] => ({ |
| 13 | + id: jobAgent.id, |
| 14 | + workspaceId: jobAgent.workspaceId, |
| 15 | + name: jobAgent.name, |
| 16 | + type: jobAgent.type, |
| 17 | + config: jobAgent.config, |
| 18 | +}); |
| 19 | + |
| 20 | +const convertJobAgentToGoEvent = ( |
| 21 | + jobAgent: schema.JobAgent, |
| 22 | + eventType: keyof GoEventPayload, |
| 23 | +): GoMessage<keyof GoEventPayload> => ({ |
| 24 | + workspaceId: jobAgent.workspaceId, |
| 25 | + eventType, |
| 26 | + data: getOapiJobAgent(jobAgent), |
| 27 | + timestamp: Date.now(), |
| 28 | +}); |
| 29 | + |
| 30 | +export const dispatchJobAgentCreated = createSpanWrapper( |
| 31 | + "dispatchJobAgentCreated", |
| 32 | + async (span: Span, jobAgent: schema.JobAgent) => { |
| 33 | + span.setAttribute("job-agent.id", jobAgent.id); |
| 34 | + span.setAttribute("job-agent.workspaceId", jobAgent.workspaceId); |
| 35 | + span.setAttribute("job-agent.name", jobAgent.name); |
| 36 | + span.setAttribute("job-agent.type", jobAgent.type); |
| 37 | + span.setAttribute( |
| 38 | + "job-agent.config", |
| 39 | + JSON.stringify(jobAgent.config, null, 2), |
| 40 | + ); |
| 41 | + |
| 42 | + await sendGoEvent( |
| 43 | + convertJobAgentToGoEvent(jobAgent, Event.JobAgentCreated), |
| 44 | + ); |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +export const dispatchJobAgentUpdated = createSpanWrapper( |
| 49 | + "dispatchJobAgentUpdated", |
| 50 | + async (span: Span, jobAgent: schema.JobAgent) => { |
| 51 | + span.setAttribute("job-agent.id", jobAgent.id); |
| 52 | + span.setAttribute("job-agent.workspaceId", jobAgent.workspaceId); |
| 53 | + span.setAttribute("job-agent.name", jobAgent.name); |
| 54 | + span.setAttribute("job-agent.type", jobAgent.type); |
| 55 | + span.setAttribute( |
| 56 | + "job-agent.config", |
| 57 | + JSON.stringify(jobAgent.config, null, 2), |
| 58 | + ); |
| 59 | + |
| 60 | + await sendGoEvent( |
| 61 | + convertJobAgentToGoEvent(jobAgent, Event.JobAgentUpdated), |
| 62 | + ); |
| 63 | + }, |
| 64 | +); |
| 65 | + |
| 66 | +export const dispatchJobAgentDeleted = createSpanWrapper( |
| 67 | + "dispatchJobAgentDeleted", |
| 68 | + async (span: Span, jobAgent: schema.JobAgent) => { |
| 69 | + span.setAttribute("job-agent.id", jobAgent.id); |
| 70 | + span.setAttribute("job-agent.workspaceId", jobAgent.workspaceId); |
| 71 | + span.setAttribute("job-agent.name", jobAgent.name); |
| 72 | + span.setAttribute("job-agent.type", jobAgent.type); |
| 73 | + span.setAttribute( |
| 74 | + "job-agent.config", |
| 75 | + JSON.stringify(jobAgent.config, null, 2), |
| 76 | + ); |
| 77 | + |
| 78 | + await sendGoEvent( |
| 79 | + convertJobAgentToGoEvent(jobAgent, Event.JobAgentDeleted), |
| 80 | + ); |
| 81 | + }, |
| 82 | +); |
0 commit comments